0

I am new to this parse argument shell script.

I have created script something like this-

 while getopts ":o:s:t:" opt; do
  case "$opt" in
    o) option=$OPTARG ;;
    s) sample=$OPTARG ;;
    t) object=$OPTARG ;;
  esac
done
#shift $((OPTIND -1))

if [[ $option = 1 ]] || [[ $object -eq TABLE ]]; then
    echo "Hello All"
else
   if [[ $option = 2 ]] || [[ $object -eq VIEW ]]; then
    echo "Hello Me"

fi

fi

Now I am running scrip something like this -

./v4_2.sh -o 1 -s OT - t TABLE

So the output I am getting is -

Hello All

Even if I am running the script with different parameter like this -

./v4_2.sh -o 2 -s OT - t VIEW

Then also I am getting same output that is

Hello All

But, here output I am expecting is - Hello me

Can someone help me like where I am lagging?!

Thanks in advance.

saurabh704
  • 63
  • 6
  • Could you be more specific about how the advice in [How do I parse command line arguments in bash?](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) didn't work for you? Without knowing exactly why it didn't help (how you tried to apply it, and what bugs you encountered), it's hard to know we won't write something with the exact same problems. – Charles Duffy Oct 17 '20 at 18:52
  • BTW, `[[ "-o" == "1" ]]` will never be true, because the string `"-o"` and the string `"1"` are two different strings. But if you follow the practices given in that existing answer and have a `case $1 in -o) arg_o=$2; shift;;`, then you can check whether `[[ $arg_o = 1 ]]` later. – Charles Duffy Oct 17 '20 at 18:56
  • This will be done with getopts, see https://stackoverflow.com/questions/16483119/an-example-of-how-to-use-getopts-in-bash – Oliver Gaida Oct 17 '20 at 21:12
  • Thank you @CharlesDuffy for your reply. Now I have changes the question based upon what I have tried and what output I am getting. Can you please help me now!. Thanks in Advance. – saurabh704 Oct 18 '20 at 06:36
  • Thank you @OliverGaida for your reply. I have tried and getting new issue. can you please help me with this. Thanks in Advance! – saurabh704 Oct 18 '20 at 06:37
  • for comparing numbers you can use: `-eq` for equal, for comparing strings, you use `==` . Change this! – Oliver Gaida Oct 18 '20 at 06:51
  • your are wellcome. One more tip. You find the explanations for all these operators in the manpage of bash: `man bash`. – Oliver Gaida Oct 18 '20 at 08:22
  • Look at https://ideone.com/Qf2kji, tracing your code -- the only reasons it doesn't work is the use of `-eq` instead of `=`, and the space between the dash and the letter in `- t VIEW`. – Charles Duffy Oct 18 '20 at 14:10

0 Answers0