LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   errors in if statement (https://www.linuxquestions.org/questions/programming-9/errors-in-if-statement-4175589561/)

new_user3085 09-16-2016 08:52 AM

errors in if statement
 
Hello Sirs,

I'm unable to figure out where I'm going wrong in the if statement of my script. Could you please take a look and help me identify the what I'm doing wrong and in fixing the errors. Thanks in advance.

Code:

if ( "$1" == "-u" || "$1" == "-U" )        #line 17
then
echo "perform option 1"
elif ( "$1" == "-i" || "$1" == "-I" )      #line 20
then
echo "perform option 2"
elif ( "$1" == "-ui" || "$1" == "-UI" )    #line 23
then
echo "perform option 3"
else
echo "No argument. Valid optiosn are {-u|-U}, {-i|-I}, {-ui|-UI}"
fi

Below are the errors I'm getting
Code:

+ '' == -u
install_scrpt: line 17: : command not found
+ '' == -U
install_scrpt: line 17: : command not found
+ '' == -i
install_scrpt: line 20: : command not found
+ '' == -I
install_scrpt: line 20: : command not found
+ '' == -ui
install_scrpt: line 23: : command not found
+ '' == -UI
install_scrpt: line 23: : command not found


HMW 09-16-2016 09:42 AM

Try to replace the '()' with '[[]]'.
Like so:
Code:

if [[ "foo" == "foo" ]]; then echo "bar"; fi
bar

The '()' are used for arithmetic. Like this:
Code:

if (( 1 > 0 )); then echo "true"; fi
true

Note that you need double '((' to make it work.

Best regards,
HMW

robertjinx 09-16-2016 11:52 AM

This works and looks a bit better:

Code:

opt="$1"

case "$opt" in
        -u|-U)
                echo "Option -u|-U"
        ;;
        -i|-I)
                echo "Option -i|-I"
        ;;
        -ui|-UI)
                echo "Option -ui|-UI"
        ;;
esac


grail 09-16-2016 12:36 PM

Maybe you could look at the following seeing as you are trying to process options :- http://www.linuxquestions.org/questi...andling-34675/

AnanthaP 09-17-2016 08:06 AM

Remove spaces on both the LHS and RHS of the ==.

OK

grail 09-17-2016 08:32 AM

Quote:

Originally Posted by AnanthaP (Post 5606466)
Remove spaces on both the LHS and RHS of the ==.

OK

This is not required

pan64 09-17-2016 08:42 AM

( string ) means you want to execute the string (inside) in a new shell.
As you can see the string is '' == -u (or similar) which cannot be executed. You need to change () to [[]] or use switch/case.

Jjanel 09-20-2016 04:03 AM

C.. prog.
 
Hi new_user....! Welcome!
From the parentheses, I'm guessing you have *programming* experience [?]
In csh, a C-language-like shell a few pages down, in here, your code would *almost* work
(except csh uses "else if" and "endif", so the "elif/fi" makes it obvious you mean [ba]sh)

Anyway... Enjoy... Best Wishes!

new_user3085 09-29-2016 09:46 AM

Appreciate your help. Thanks for all your replies!!


All times are GMT -5. The time now is 04:45 AM.