BASH: getopts with default parameters value

前端 未结 2 1421
有刺的猬
有刺的猬 2021-01-27 18:47

I\'ve got another bash-script problem I simply couldn\'t solve. It\'s my simplified script showing the problem:

while getopts \"r:\" opt; do
case $opt in

  r)
          


        
2条回答
  •  Happy的楠姐
    2021-01-27 19:14

    This works for me:

    #!/bin/bash
    
    while getopts "r" opt; do
    case $opt in
    
      r)
        fold=/dev
        dir=${2:-$fold}
    
         echo "asdasd"
      ;;
    esac
    done
    

    Remove the colon (:) in the getopts argument. This caused getopt to expect an argument. (see here for more information about getopt)

提交回复
热议问题