Comparison of two variables with IF loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparison of two variables with IF loop
# 1  
Old 01-21-2013
Comparison of two variables with IF loop

Hello,
My script is like :
Code:
#!/bin/sh -x
TODAY=$(echo `date +"%b%d"`)
FILE_DATE=$(ls -l test.sh | awk '{print $6$7}')
echo "file date=$FILE_DATE"
echo "date=$TODAY"
if ["$TODAY" == "$FILE_DATE"]];then
echo "file is correct"
else
echo "file is incorrect"
fi

and The Output is :
Code:
$ ./test.sh
+ + date +%b%d
+ echo Jan21
TODAY=Jan21
+ + ls -l test.sh
+ awk {print $6$7}
FILE_DATE=Jan21
+ echo file date=Jan21
file date=Jan21
+ echo date=Jan21
date=Jan21
+ [Jan21 == Jan21]]
./test.sh[6]: [Jan21:  not found
+ echo file is incorrect
file is incorrect

Please let me know where i am doing wrong.

Regards,
saurau
# 2  
Old 01-21-2013
Your if statement has one opening [ and two closing ]]. You also need a space after [ and before ].

There's also an unnecessary use of echo and backticks:
Code:
TODAY=$(echo `date +"%b%d"`)

Could be:
Code:
TODAY=$(date +"%b%d")

This User Gave Thanks to Scott For This Post:
# 3  
Old 01-21-2013
And a single = instead of ==
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-21-2013
Quote:
Originally Posted by Scott
Your if statement has one opening [ and two closing ]]. You also need a space after [ and before ].

There's also an unnecessary use of echo and backticks:
Code:
TODAY=$(echo `date +"%b%d"`)

Could be:
Code:
TODAY=$(date +"%b%d")

Thanks, It worked Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with variables in loop

Hello, please assist: users="test1 test2" keytest1="abcd" keytest2="dbcd" for i in $users do echo "$key${i}" > fileout done So, my objective is to take the current user (ie test1) in loop and echo its associated keyname (ie keytest1) variable to a file. The echo... (2 Replies)
Discussion started by: motdman
2 Replies

2. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

3. Shell Programming and Scripting

While loop for comparison

hi all, i have two files: test1 contains : one two three four five (on separate lines) test2 contains: one ten nine (on separate lines) I want to compare two files, test1 against test2. if any word from test1 does not match test2 display it. can someone help me with a while loop on this. ... (4 Replies)
Discussion started by: sbk785
4 Replies

4. Shell Programming and Scripting

for loop with 2 variables

i am having a file contants as below my requirement is for file in `awk -F "," '{print $8,$9}'` <temp.txt echo "$file" echo "$file">test.txt a=`awk -F "," '{print $1}' `<test.txt b=`awk -F "," '{print $2}' `<test.txt but script reads , i want both the vales for further... (5 Replies)
Discussion started by: sagar_1986
5 Replies

5. Shell Programming and Scripting

string comparison not working inside while loop

The string comparison highlighted below is not working fine. Please help: while read line do # Get File name by deleting everything that preceedes and follows Filename as printed in cvs status' output f_name=`echo $line | sed -e 's/^File://' -e 's/ *Status:.*//' | awk '{print $NF}'` ... (4 Replies)
Discussion started by: sudvishw
4 Replies

6. Shell Programming and Scripting

String comparison not working inside while loop

Hi, In the code included below, the string comparision is not working fine. Please help while (( find_out >= i )) do file=`head -$i f.out|tail -1` dir=`dirname $file` cd $dir Status="" if ; then Status=`cvs -Q status... (3 Replies)
Discussion started by: sudvishw
3 Replies

7. UNIX for Advanced & Expert Users

Comparison and For Loop Taking Too Long

I'd like to 1. Check and compare the 10,000 pnt files contains single record from the /$ROOTDIR/scp/inbox/string1 directory against 39 bad pnt files from the /$ROOTDIR/output/tma/pnt/bad/string1 directory based on the fam_id column value start at position 38 to 47 from the record below. Here is... (1 Reply)
Discussion started by: hanie123
1 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

Help with if loop (string comparison)

Hi Can someone please tell me what is wrong with this (ksh).. if + ]] then echo ${COMP_TEMP} fi What i need here is, say if the variable is a 1 or 2 digit number, then execute the if loop. Basically the variable can either be 1-30 or some other character sequence say '?', '&&'... (4 Replies)
Discussion started by: psynaps3
4 Replies
Login or Register to Ask a Question