unix - Can't assign a value to a variable in a case statement in bash -


i ran problem while trying assign value variable inside case statement, here code:

#!/bin/bash  while getopts ":m:n:::" opt;   case $opt in     n)        echo "-n triggered, parameter: $optarg " >&2       case $optarg in          t)             echo threads             r=threads             ;;         p)             echo processes             r="something"             ;;         esac       ;;     m)       echo "-m triggered, parameter: $optarg" >&2       ;;     \?)       echo "invalid option: -$optarg" >&2       exit 1       ;;     :)       echo "option -$optarg requires argument." >&2       exit 1       ;;   esac done  echo $r echo no thread/processes: $2 p/t: $4 if: $5  of: $6 

i'd use variable $r later, can't. when try print using echo (as in script), not return thing. i've been trying spot mistake couldn't. there similar post suggested remove blank spaces before , after =, can see, there no blank spaces in mine.

here console when run it:

$ ./friendfind -n 2 -m p in out -n triggered, parameter: 2  -m triggered, parameter: p no thread/processes: 2 p/t: p if: in of: out 

the purpose of script run c file option run threads or processes, asks number of processes/threads want use, if want tu use processes or threads , input , output file.

i think aiming @ this:

#!/bin/bash # print usage message: usage() {   echo "usage: $0 [-n n] [-t|-p] input output" >> /dev/stdout } # set default values n_threads=1   use_threads=1 while getopts "n:pth" opt;   case $opt in     n) n_threads=$optarg;;     t) use_threads=1;;     p) use_threads=0;;     h) usage; exit 0;;     *) usage; exit 1;;   esac done # rid of scanned options shift $((optind-1)) if (($# != 2)); usage; exit 1; fi if ((use_threads));   echo "using $n_threads threads. if: $1; of: $2"   # ... else   echo "using $n_threads processes. if: $1; of: $2"   # ... fi 

here's example invocations, including couple of errors:

$ ./ff -p foo bar using 1 processes. if: foo; of: bar $ ./ff foo bar using 1 threads. if: foo; of: bar $ ./ff -n 7 foo bar using 7 threads. if: foo; of: bar $ ./ff -n 7 -p foo bar using 7 processes. if: foo; of: bar $ ./ff -p -n7 foo bar using 7 processes. if: foo; of: bar $ ./ff -q -n7 foo bar ./ff: illegal option -- q usage: ./ff [-n n] [-t|-p] input output # note: error message here more informative. # exercise left reader $ ./ff -n 7 foo usage: ./ff [-n n] [-t|-p] input output 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -