LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack Search this Thread
Old 02-09-2010, 02:22 PM   #1
digity
Member
 
Registered: Apr 2005
Posts: 99

Rep: Reputation: 15
Or statement isn't working in script


I'm trying to write a shell script with an OR statement and I believe the syntax is correct yet its not working properly - no matter what the value is it always response Yes! (even though the value is 09). Here's the script:

Code:
#!/bin/sh

DOM=`date +%d`

echo "Today's date of the month is the $DOM."

if [ $DOM="07" ] || [ $DOM="14" ] || [ $DOM="21" ] || [ $DOM="28" ]; then	
	echo "Yes!"	
else
	echo "No!"
fi
I've already tried using double equal signs (== instead of =), but I get the same results.

Any ideas?
 
Old 02-09-2010, 02:41 PM   #2
digity
Member
 
Registered: Apr 2005
Posts: 99

Original Poster
Rep: Reputation: 15
SOLVED! Apparently the spacing is very important. The needs to be a space surrounding the equal signs. This works:

Code:
#!/bin/sh

DOM=`date +%d`

echo "Today's date is the $DOM."

if [ $DOM = "07" ] || [ $DOM = "14" ] || [ $DOM = "21" ] || [ $DOM = "28" ]; then
	echo "Yes!"
else
	echo "No!"
fi
 
Old 02-09-2010, 04:27 PM   #3
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 5,995

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
SOLVED!
Thanks for posting your solution. From your post #1:
Quote:
Any ideas?
You need to read man bash. But that's a PITA. man files are much easier to read when converted PDF's (especially as they are searchable).
You might like to see here
Have fun.
 
Old 02-09-2010, 04:36 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,962

Rep: Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345
Quote:
Originally Posted by digity View Post
Apparently the spacing is very important.
That's right. The question is why these expressions evaluated as TRUE. The answer is that they were interpreted as single and alone strings which are evaluated always as TRUE. E.g.
Code:
[ 09=07 ]     # this is TRUE
[ a ]         # this is TRUE as well
 
Old 02-09-2010, 04:39 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,696

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
These are much easier to read than man pages (of any format)

http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/


BTW http://www.tldp.org/LDP/abs/html/tes...ml#DBLBRACKETS
 
Old 02-09-2010, 04:44 PM   #6
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 5,995

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
These are much easier to read than man pages (of any format)
Nice links. Bookmarked
 
Old 02-09-2010, 04:53 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,696

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
Thx; my preferred version of man pages (and they are the definitive info for cmds) is http://linux.die.net/man/
 
Old 02-09-2010, 07:05 PM   #8
digity
Member
 
Registered: Apr 2005
Posts: 99

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by tredegar View Post
You need to read man bash.
I'm confused - why do I need to read that?
 
Old 02-09-2010, 09:11 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: back to Arch
Posts: 16,640

Rep: Reputation: 423Reputation: 423Reputation: 423Reputation: 423Reputation: 423
Quote:
Originally Posted by digity View Post
I'm confused - why do I need to read that?
That gets you the built in manual (man) for bash. But some of the other links here are better.

Add one---at tldp, get the Bash Guide for Beginners
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If-statement in Shell Script SeraphimNL Linux - Newbie 4 01-11-2010 12:30 AM
Using apt-get in if statement in bash script jax8 Programming 4 03-29-2009 10:23 PM
for statement in bourne shell script bujecas Linux - General 3 07-17-2006 07:32 AM
problem with if statement in bash script crewblunts Programming 3 03-11-2006 12:38 PM
Script and If Statement bspicer Linux - General 2 04-15-2002 06:02 AM


All times are GMT -5. The time now is 11:59 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration