samedi 4 décembre 2021

Bash script mandatory option

I try to write a script with option mandatory

c, p and e options are mandatory. c can be any words p can be any words e can be only two values : aa or bb with help option

usage() {
 echo "Usage: Script -c <cons> -p <pri> -e <ert>"
  echo "options:"
                echo "-h      show brief help"

  1>&2; exit 1;
}

Help() {
  echo 'HELP'
}

while getopts "h?p:c:e:" args; do
case $args in
    h|\?)
        Help;
        exit;;
    p) pri=${OPTARG};;
    c) cons=${OPTARG};;
    e) ert=${OPTARG};;
  esac
done

if [[ -z "$pri" ||  -z "$cons" ]] || [[ ! ( $ert == 'aa' || $ert == 'bb' ) ]]; then usage; fi

echo "$pri"
echo "$cons"
echo "$ert"

I don't understant logical operation.

if statement must be false, like that I can echo the three strings, if statement is true, I print the usage function.

if [[ 1 ||  1 ]] && [[ ! ( 1 || 0 ) ]]; then usage; fi
     (1 + 1 ) && !(1 + 0)
       1 && 0
        0

       false, don't used usage function = correct


  if [[ 1 ||  1 ]] && [[ ! ( 0 || 0 ) ]]; then usage; fi
     (1 + 1 ) && !(0 + 0)
       1 && 1
        1

       it should use usage function, but it does't do

when I used e option equal cc, I don't get usage function, but the echo below. So I don't understand why

Aucun commentaire:

Enregistrer un commentaire