LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2010, 10:50 PM   #1
bdrockwell
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
if...elif...else BASH ladder problems


Our assignment is to take in two numbers and add them. While the inputs will always be numbers, we are to inform the user if they are doing it wrong.

My code:

Quote:
#!/bin/sh

if [[ -n "$3" ]]
then
echo too many inputs
elif [[ -z "$1"]]
then
echo you need to have two inputs
elif [[ -z "$2" ]]
then
echo you need to have two inputs
else
echo $1 + $2 = `expr $1 + $2`
fi
My error:

Quote:
[brockwel@darrow1-p hw5]$ problem1.sh
problem1.sh: line 6: syntax error in conditional expression
problem1.sh: line 7: syntax error near `then'
problem1.sh: line 7: `then'
The code worked fine before I tried to idiot-proof it. I'll probably end up nesting the if statements checking if the first two inputs are not null, but I'm more annoyed at why the script isn't working.

Help would be very appreciated.
 
Old 11-16-2010, 11:06 PM   #2
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
For every conditions, do it with one pair of open/close brackets only. and put space after '[', and before ']'
 
Old 11-16-2010, 11:34 PM   #3
bdrockwell
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the help
 
Old 11-16-2010, 11:38 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
I would suggest looking at the variable $# as it can potentially change 3 ifs into one. (see here)

I would then suggest looking at the (()) construct found here as a much cleaner way of doing the arithmetic

Lastly, if you use code tags instead of quote tags then your formatting will remain
 
  


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
Bash script if elif problem assyrian1 Linux - Software 2 12-09-2008 09:00 AM
BASH if/elif variable problem GNUJoshua Programming 6 07-15-2008 07:01 AM
Bash if, elif, else not working Blackout_08 Programming 7 06-09-2006 10:28 AM
bash scripting question - elif ?? xscousr Programming 2 08-12-2003 09:56 AM
elif??????????? embsupafly Programming 1 11-27-2002 03:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:38 PM.

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