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 04-05-2003, 03:27 PM   #1
Wynd
Member
 
Registered: Jul 2001
Distribution: Slackware 12
Posts: 511

Rep: Reputation: 32
Bash 'or'


How can I use an 'or' statement in the bash language? I have one integer variable and one string variable, if that makes a difference. All the stuff I try gets me expression expected errors. This is my code, basically:
Code:
if [ "$a" -eq 0 ] || [ "$b" -eq "" ]
then
    echo "Error 1"
elif [ "$c" -eq 0 ] || [ "$d" -eq "" ]
then
    echo "Error 2"
fi
 
Old 04-05-2003, 03:35 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if you check on the man page for tet you'll see that you want the -o option for a logical or.
 
Old 04-07-2003, 02:53 AM   #3
revrendi
LQ Newbie
 
Registered: Mar 2003
Location: California, USA
Distribution: Linux from Scratch
Posts: 9

Rep: Reputation: 0
That's the man page for test. When bash evaluates an expression inside brackets it's the same thing as using test. So your expression translated should be something like:

if [ "$a" -eq 0 -o "$b" -eq "" ]
then
echo "Error"
.....

But your error is probably with the:

"$b" -eq ""

The empty quotes aren't a valid argument to the -eq operation, which expects an integer. What you probably want to do is something more like:

if [ "$a" -eq 0 -o -z "$b" ]
then
echo "Error 1"
elif [ "$c" -eq 0 -o -z "$d" ]
then
echo "Error 2"
fi

And make sure that both $a and $c are defined, otherwise you have the same issue. -- Rev
 
  


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
$LINENO can't be modified in bash 3.0, while it can be in bash 2.05b Darwish Linux - Software 1 11-07-2005 02:57 PM
BASH help snorky Linux - General 3 03-18-2005 07:27 PM
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 07:53 AM
More on BASH please ;) Pyus Linux - Newbie 8 10-27-2004 02:29 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

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

All times are GMT -5. The time now is 02:34 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