Search code examples
linuxshellcommandgetopts

How can I create shell command in linux like this: hh.sh filename -e -c?


The options are following the filename rather than hh.sh. And the following code does not work.

while getopts ":ec" opt; do
  case $opt in
    e)
      eflag=1
      ;;
    c)
      cflag=1
      ;;
  esac
done
shift $(($OPTIND - 1))

Solution

  • what about something like this.

    filename=$1
    shift
    while getopts ":ec" opt; do
      case $opt in
        e)
          eflag=1
          ;;
        c)
          cflag=1
          ;;
      esac
    done
    shift $(($OPTIND - 1))
    
    echo $filename
    echo $eflag
    echo $cflag
    

    grab the filename, then shift and run getopts?