문제

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
도움이 되었습니까?

해결책

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 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top