2020-12-29

bash script options parser fails

I have a bashscript.sh that I $ chmod +x bashscript.sh and move it to $ mv bashscript.sh ~/.local/bin/ in order for it to be executable like a system command.

I'd like to be able to invoke it with

bashscript [<-w|-working|--working>[=|:]] <y|yes|n|no> 

And return usage/help/error (call it whatever we want) if the call isn't respected.

To do so, I wrote this parsing part:

usage(){
  echo "you're wrong."
  exit 1
}
[[ $# -lt 1 ]] && usage
options=$(getopt -o y,n,h,w: -l yes,no,help,working: -- "$@")
set -- $options
while true; do
  case "$1" in
    -h|--help|*) usage
      shift;;
    y|yes)
      #do something
      shift;;
    n|no)
      #..
      shift;;
    -w|-working|--working)
      shift;;
    --) #this is just syntax
      shift
      break;;
  esac
done

But when I test it doesn't work as intended*, would you know why/have a sample that handles my option possibilites?

*edit : I always trigger the usage display

edit 2 : removed the spaces around the "=" of options as @costaparas3 pointed out, thank you, still stuck to usage() though



from Recent Questions - Stack Overflow https://ift.tt/3mWEgng
https://ift.tt/eA8V8J

No comments:

Post a Comment