LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 02-09-2010, 02: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, 02: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, 04:27 PM   #3
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
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
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
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
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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: 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, 09:11 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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



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 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

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

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