LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2016, 08:52 AM   #1
new_user3085
LQ Newbie
 
Registered: Sep 2016
Posts: 21

Rep: Reputation: Disabled
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
 
Old 09-16-2016, 09:42 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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
 
2 members found this post helpful.
Old 09-16-2016, 11:52 AM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
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
 
1 members found this post helpful.
Old 09-16-2016, 12:36 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Maybe you could look at the following seeing as you are trying to process options :- http://www.linuxquestions.org/questi...andling-34675/
 
1 members found this post helpful.
Old 09-17-2016, 08:06 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Remove spaces on both the LHS and RHS of the ==.

OK
 
1 members found this post helpful.
Old 09-17-2016, 08:32 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by AnanthaP View Post
Remove spaces on both the LHS and RHS of the ==.

OK
This is not required
 
2 members found this post helpful.
Old 09-17-2016, 08:42 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,847

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
( 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.
 
1 members found this post helpful.
Old 09-20-2016, 04:03 AM   #8
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
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!
 
1 members found this post helpful.
Old 09-29-2016, 09:46 AM   #9
new_user3085
LQ Newbie
 
Registered: Sep 2016
Posts: 21

Original Poster
Rep: Reputation: Disabled
Appreciate your help. Thanks for all your replies!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running mysql from ssh & query statement has a Text Header in the SELECT statement? djlerman Linux - Server 6 11-19-2013 06:33 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration