linux - How do I control the user defined commands when executing a shell script in bash? -


i creating shell script has own commands (or options). in simpler terms, command line executing script (in terminal): ./myscript.sh -o:1,2,3,4 or ./myscript.sh -e:2,3,4. here code (for option part) looks far:

myscript.sh

for   case "$i" in     "-o:"*|"--only:"*)         # check if 1 not included (if exit)         ;;      "-e:"*|"--except:"*)         # check if 1 excluded (if exit)         ;;      *)       echo -e "invalid option\ntry './audit.sh --help' help"       exit       ;;   esac done 

explanation:

the purpose of code sum values user passes through ./myscript.sh -o:1,2,3,4 or subtract values ./myscript.sh -e:2,3,4. o "only" option adds numbers passed script option. e "exclude" option excludes values user defines , subtracts 100.

i have restriction number 1 must there (whether adding or subtracting). can't exclude number 1. here examples of illegal user input commands (./myscript.sh -e:1,2,3,4) , (./myscript.sh -o:2,3,4).

how check if number 1 included in only option , number 1 not excluded exclude option? thanks.

edit: method want approach using awk. suggestions?

  1. get value check
  2. add commas front , end
  3. check of ,1, substring

implementation:

i="--only:3,4,5"  value=${i#*:}           # strip prefix colon  if [[ ,$value, == *,1,* ]]  # check if ,1, substring   echo "you have 1 in list." else   echo "you're missing 1"   exit 1 fi 

to invert check checking exclusion, can use !=.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -