unix - getopts not working in bash script -


#! bin/bash # code train.sh    while getopts "f:" flag              case $flag in               f)                echo "hi"                 startpoint = $optarg                ;;          esac     done      echo test range: $4     echo train range: $3      #path of experiment folder , data folder:     exp_dir="$1"     data_dir="$2"     echo experiment: $exp_dir     echo dataset: $data_dir     echo file: $startpoint   ran command > ./train.sh test1  test2 test3 test4  -f testf   

and got output

test range: test4 train range: test3 experiment: test1 dataset: test2 file: 

so getopts option not seem work reason can see nothing printed after file , echo "hi" command not executed in case statement. can please me this?

you need put options before naked parameters

./train.sh -f testf test1  test2 test3 test4 

with output

hi test range: test4 train range: test3 experiment: test1 dataset: test2 file: testf 

you need couple of changes too

while getopts "f:" flag      case $flag in          f)            echo "hi"             startpoint=$optarg            shift            ;;      esac      shift done 

to set environment variable , shift got option out of way


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -