if [ ! $1 ] || [ ! $2 ]; then
    echo "At least one of the elements is missing"
    echo "Usage: opened_files.sh <output type> <process search pattern>"
    exit 1
fi

case $1 in
    "hardlimit")
        ps -eo pid,args | grep -v grep | grep -v bash | grep -E "$2" | awk '{print $1}' | xargs -Ipid bash -c "grep 'Max open files' /proc/pid/limits 2>/dev/null || sudo grep 'Max open files' /proc/pid/limits" | awk '{print $5}'
    ;;

    "total")
        ps -eo pid,args | grep -v grep | grep -v bash | grep -E "$2" | awk '{print $1}' | xargs -Ipid bash -c 'ls /proc/pid/fd 2>/dev/null || sudo ls /proc/pid/fd' | wc -l
    ;;

    *)
        echo "Unknown option: $1. Possible values: hardlimit, total"
        exit 1
    ;;
esac