Getopts in bash, the script worked before that, and now I'm puzzled

So, I have a couple of getopts in my bash script. Here is an example of a worker.

FOUND=
SEARCH=
COUNT=0
while getopts "ips:flenkc" OPTION
do
case $OPTION in
        i)
                FOUND=1
                let "COUNT++"
                ;;
        p)
                FOUND=2
                let "COUNT++"
                ;;
        s)
                FOUND=3
                SEARCH=$OPTARG
                let "COUNT++"
                ;;
esac
done

Later, a case statement that checks to see if count = 1 (which means that the call uses only one of the following: i, p, and s). Not important, except that it defines the main action.

Now the thing in question. This has worked before, and now it is not. The goal is to make sure that if someone wants to enter data, they can do it with the following bash command.

./programname -i -f Mary -l Sue -e smary@email.com -n 555-555-5555

, -i, -f, -l, -e -n ( , , ). , : , . bash, , , .

if [ $FOUND == "1" ]
then
        echo "You have chosen to insert things."
        FIRST=
        LAST=
        EMAIL=
        NUMBER=
        while getopts "if:l:e:n:" OPTION
        do
        case $OPTION in
                f)
                        FIRST=$OPTARG
                        ;;
                l)
                        LAST=$OPTARG
                        ;;
                e)
                        EMAIL=$OPTARG
                        ;;
                n)
                        NUMBER=$OPTARG
                        ;;
        esac
        done

        if [[ -z $FIRST ]] || [[ -z $LAST ]] || [[ -z $EMAIL ]] || [[ -z $NUMBER ]]
            echo "Error!!! Some input is missing!!!"
            usage // display usage
        exit 1
        fi
        echo -e $FIRST"\t"$LAST"\t"$EMAIL"\t"$NUMBER >> contacts
fi

, FIRST, LAST, EMAIL NUMBER ( , , ).

getopts? , ... !

+4
1

: , script getopts , getopts ; reset OPTIND 1 getopts, .

, :

  • if [[ -z ... then.
  • // usage . , POSIX, # char.
  • bash script, [[ ... ]] ( [ ... ]) / (( ... )) .
    • , [ ... == ... ], POSIX - [ ... ] - Bash - - == (POSIX =).
    • [ ... ], , , .
  • [[ ... ]] OR - [[ ... || ... || ... ]].
  • , .
  • stderr, >&2.
  • echo -e .

shellcheck.net.

, :

#!/usr/bin/env bash

# ... code that sets $found

# If you've already processed args. with getopts above,
# you must reset OPTIND to process them again.
OPTIND=1

if (( found == 1 )) # found is numeric, use arithmetic expression to compare
then
        echo "You have chosen to insert things."
        first= last= email= number= # don't use all-uppercase var. names
        while getopts "if:l:e:n:" option
        do
          case $option in
                f)
                        first=$OPTARG
                        ;;
                l)
                        last=$OPTARG
                        ;;
                e)
                        email=$OPTARG
                        ;;
                n)
                        number=$OPTARG
                        ;;
          esac
        done

        if [[ -z $first || -z $last || -z $email || -z $number ]]; then
            echo "Error!!! Some input is missing!!!" >&2
            usage # display usage
            exit 1
        fi
        echo -e "$first\t$last\t$email\t$number" >> contacts
fi
+5

Source: https://habr.com/ru/post/1614896/


All Articles