LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-09-2010, 03:22 PM   #1
digity
Member
 
Registered: Apr 2005
Posts: 105

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, 03:41 PM   #2
digity
Member
 
Registered: Apr 2005
Posts: 105

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, 05:27 PM   #3
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
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, 05:36 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 05:39 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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, 05:44 PM   #6
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Quote:
These are much easier to read than man pages (of any format)
Nice links. Bookmarked
 
Old 02-09-2010, 05:53 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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, 08:05 PM   #8
digity
Member
 
Registered: Apr 2005
Posts: 105

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, 10:11 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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



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


All times are GMT -5. The time now is 04:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration