Comparison of two variables with IF loop | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Comparison of two variables with IF loop

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-21-2013
saurau saurau is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 20 June 2014, 4:03 AM EDT
Posts: 23
Thanks: 4
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 01-21-2013
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 13 December 2014, 6:28 PM EST
Location: Zürich
Posts: 8,489
Thanks: 333
Thanked 1,050 Times in 916 Posts
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")

The Following User Says Thank You to Scott For This Useful Post:
saurau (01-21-2013)
Sponsored Links
    #3  
Old 01-21-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 19 December 2014, 1:14 AM EST
Location: Amsterdam
Posts: 9,690
Thanks: 300
Thanked 2,491 Times in 2,230 Posts
And a single = instead of ==
The Following User Says Thank You to Scrutinizer For This Useful Post:
saurau (01-21-2013)
    #4  
Old 01-21-2013
saurau saurau is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 20 June 2014, 4:03 AM EDT
Posts: 23
Thanks: 4
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Scott View Post
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
string comparison not working inside while loop sudvishw Shell Programming and Scripting 4 12-31-2010 01:02 PM
String comparison not working inside while loop sudvishw Shell Programming and Scripting 3 12-30-2010 07:49 AM
Comparison and For Loop Taking Too Long hanie123 UNIX for Advanced & Expert Users 1 07-10-2009 08:50 AM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 05:59 PM
Help with if loop (string comparison) psynaps3 Shell Programming and Scripting 4 07-07-2006 03:36 AM



All times are GMT -4. The time now is 03:15 AM.