Pergunta

I have written below code for using long options with getopts, but it doesn't work (arguments have no effect on values of the variables). What is the correct syntax?

while getopts "c:(mode)d:(file1)e:(file2)" opt; do
  case $opt in
  -c|--mode)
      mode=$OPTARG
      ;;  
  -d|--file1)
      file1=$OPTARG
      ;;  
  -e|--file2)
      file2=$OPTARG
      ;;  
  esac
done
Foi útil?

Solução

I found that the code in question is for ksh and not bash. For getopts we can't use long options. I ended up manually parsing arguments as below

while test -n "$1"; do
    case "$1" in
      -c|--mode)
          mode=$2
          shift 2
          ;;  
      -d|--file1)
          file1=$2
          shift 2
          ;;  
      -e|--file2)
          file2=$2
          shift 2
          ;;  
    esac
done 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top