if-statement troubles


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if-statement troubles
# 1  
Old 08-06-2009
if-statement troubles

I try to compare the day and month of someones birthday with the day and month of today, so my little bash script can send a mail to the person that has its birthday that day.

The first line of the file birthdays looks like this:
firstname,lastname,01/01/1990,....

The variable birthday's value is: 01/01
The variable today's value is: 06/08

Yet when i compare them in the if statement you see below, he still executes the commands in the if-block as if the two values were the same.

I tried to change my if statement in a million ways but my output always is "Happy birthday".

Can someone try to explain why this if statement doesnt work?

Code:
cat birthdays | while read line; do
birthday=`echo $line | sed 's/^.*,.*,\(..\/..\)\/.*$/\1/g'`
today=`date '+%d/%m'`
if [ $today=$birthday ]
then
echo "Happy birthday"
echo ""
fi



---------- Post updated at 01:36 PM ---------- Previous update was at 01:35 PM ----------

and of course my code ends with "done"
# 2  
Old 08-06-2009
I don't know what shell you're using but in Bash that if statement needs to look like this:

Code:
if [ $today == $birthday ]

# 3  
Old 08-06-2009
Easy enough, but it worked. My book said one = should be enough
# 4  
Old 08-06-2009
You are right, doc.arne. One '=' is enough. In fact, some borne shells don't work with two.

But you need spaces around the equal sign in a comparison.
Code:
if [ $today = $birthday ]

Also, I don't think you want the g flag at the end of the sed expression, in case there happen to be two dates on the line. Although I'm not sure what would happen since you are only printing \1.
Code:
birthday=`echo $line | sed 's/^.*,.*,\(..\/..\)\/.*$/\1/g'`

# 5  
Old 08-07-2009
Test (=[) is builtin command = not bracket. Usually two mistakes using test command:
  • variable is empty => no argument => error
  • no argument separator between arguments - it's commandline, not programlanguage testing with brackets.

Solution: make always string = string length 0 is something = good argument and remember argument delimeters. Ex. in this case test command need 4 arguments: value operator value ]

If you use test command using test (not [ ), then you don't need last argument ].
Yes little funny, but command is exactly the same.

Generic solution for shells (sh, bsh, ksh, bash, zsh, ...):
Code:
[  "$today"  =  "$birthday"  ] 
#or
test  "$today"  =  "$birthday"

more testing

---------- Post updated at 07:27 PM ---------- Previous update was at 07:23 PM ----------

There are also some other test commands, [[ and ((. Syntax are little different as [ / test.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case Statement Troubles

Hi, I'm attempting to create case statement in a ksh script that does the following: Run a uname command against the box and use that value for $HOSTNAME object. Then, if hostname has AIX in it, then use the lsldap command to try to bind, then print $HOSTNAME:yes or $HOSTNAME:no, depending on... (7 Replies)
Discussion started by: tekster2
7 Replies

2. BSD

PF troubles on OpenBSD 5.0

I am setting up a system as an ADSL gateway. ADSL is working fine. PF is not forwarding for some reason. # ifconfig lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33196 priority: 0 groups: lo inet6... (0 Replies)
Discussion started by: John Tate
0 Replies

3. Shell Programming and Scripting

for loop troubles

What I have here is a pretty textbook recursive function. Its purpose right now is simply to display all folders that don't contain folders. It works fine in all instances I can think of... except one. If there is a folder with a space in its name, the thing goes Kablooie. AFAIK the problem comes... (5 Replies)
Discussion started by: divisionbyzero
5 Replies

4. UNIX for Dummies Questions & Answers

Cron troubles

I am aware this question has been answered time and again. I feel I have tried everything I have seen on the net and really need help to get this working. Same old story. Shell script, working from command but not from cron. I need my script to take values from a .properties file. Tried... (2 Replies)
Discussion started by: airalpha
2 Replies

5. Shell Programming and Scripting

Encoding troubles

Hello All I have a set of files, each one containing some lines that follows that regex: regex='disabled\,.*\,\".*\"'and here is what file says about each files: file <random file> <random file> ASCII text, with CRLF line terminatorsSo, as an example, here is what a file ("Daffy Duck - The... (3 Replies)
Discussion started by: tukuyomi
3 Replies

6. UNIX for Dummies Questions & Answers

ssh2 troubles

I'm trying to set up a secure and trusted connection between 2 boxes running solaris using ssh2. I've run ssh-keygen2 on the local box and on the remote box, created the identification file ( IdKey id_dsa_2048_a ) on the local machine and copied across the public key file from the local to... (5 Replies)
Discussion started by: PaulC
5 Replies

7. UNIX for Dummies Questions & Answers

Password Troubles

I'm very new to UNIX (I just started working with Terminal 2 days ago) and I don't know the system very well. I'm having trouble whenever I am asked for a password. I simply... can't type. I press keys on the keyboard but no characters appear on the screen. For example, when I log onto... (5 Replies)
Discussion started by: alexmiller
5 Replies

8. Programming

Troubles with HPUX

Hello I created an application in c language for HP-UX operative system,and it runs on a 32 bits PARISC processor. My problem is that I have to run this same application but now in a 64 bits Parisc processor. But I am not able to compile the application with the 64 bit server, and I only could use... (1 Reply)
Discussion started by: masterboy6666
1 Replies

9. UNIX for Dummies Questions & Answers

compariosn troubles...

Hi Guys, I am trying to compare using if, but keep getting some strange results. if ; then keeps creating the file 1 if ; then does not work at all if ; then does not work if ; then does not work if ; then does not work eihter. I am using a ksh, on Solaris (9 Replies)
Discussion started by: jagannatha
9 Replies

10. Programming

compiling troubles

i keep getting the following error with the code segment below when i try to compile the program. The code is from 'defs.h' parse error before '(' parse error before ')' stray '\' in program this is the code segment and the error is on the second line of the segment #define... (1 Reply)
Discussion started by: token
1 Replies
Login or Register to Ask a Question