comparing variables in an if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing variables in an if statement
# 1  
Old 03-03-2012
comparing variables in an if statement

Code:
#!/bin/bash
#timetest
TIMENOW="$(date)"
T1=12:00:00
echo $TIMENOW >timenow
cat timenow |cut -f4 -d' ' >time1
T2=$(sed -n "${1}p" time1)
echo "T1 = " $T1
echo "T2 = " $T2
if[ $T1 -gt $T2 ] then
  echo $T1 
 else
  echo $T2 
fi

I thought scripting was simple! So why does this script result in:
Code:
T1 =  12:00:00
T2 =  16:04:43
/home/admin/user/bin/timetest: line 10: if[ 12:00:00 -gt 16:04:43 ]: command not found
12:00:00
/home/admin/user/bin/timetest: line 12: syntax error near unexpected token `else'
/home/admin/user/bin/timetest: line 12: ` else'


Last edited by Scott; 03-04-2012 at 04:13 PM.. Reason: Please use code tags
# 2  
Old 03-03-2012
You are getting the command not found message because you need a space between the 'if' and the open bracket:

Code:
if [ $T1 -gt $T2 ] then

You will also likely receive an error once you fix that as '-gt' implies integer comparison, and your variables contain non-numeric chaaracters (colons). You might consider something like this:

Code:
t1="12000000"
t2="$(date "+%H%M%S")"
if [ $t1 -gt $t2 ]
then
    echo true
else
    echo false
fi

Both timestamps are created without colons and can be compared as integers.
# 3  
Old 03-04-2012
Thank you for the replies. The placement of 'then' was important as was the space after 'if' ........Thank you, Agama! However I still needed further modification. Here is the script that works:
Code:
#!/bin/bash
#timetest
TIMENOW="$(date)"
T1=12:00:00
echo $TIMENOW >timenow
cat timenow |cut -f4 -d' ' >time1
T2=$(sed -n "${1}p" time1)
echo "T1 = " $T1
echo "T2 = " $T2
if [[ $T1 < $T2 ]] #or [ $T1 \< $T2 ] for comparing ascii strings
 then
  echo $T1 
 else
  echo $T2 
fi


Last edited by Scott; 03-04-2012 at 04:13 PM.. Reason: Code tags
# 4  
Old 03-04-2012
Good. I still think you are going through more work than needed to get T2. All that effort of cutting, echoing and tmp files isn't needed:

Code:
T2=$( date "+%H%M%S" )    # without colons
T2=$( date "+%X" )              # hr:min:sec

# 5  
Old 03-04-2012
Thank you! I was concentrating so hard on the 'if' I neglected the more obvious......Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two variables

I have a script like this. Just couldn't get the comparison part work. Any thought? thanks, #!/usr/bin/ksh -x STEP=`echo $(basename $0 .ksh) | tr "" ""` log=/skip.log while read LINE do if then echo `date`: STEP $STEP skipped by user >> $log exit 0 fi done < $1 echo... (0 Replies)
Discussion started by: ghostmic
0 Replies

2. Shell Programming and Scripting

Comparing multiple variable in if statement

Hi there this script is an atempt to define which instances of Jboss relate to its PID by the date and timestamp I am using calc to test with. On our system the only way you can tell which instance relates to a particular instance is by looking at the start up time and date in a log. The... (9 Replies)
Discussion started by: nathan.harris
9 Replies

3. Shell Programming and Scripting

awk comparing variables

Is there a way to compare variables in a 'awk'? I've been trying for a while and can't figure it out. I'm guessing its not possible :/ VAR=Bob awk '$3 == $VAR { print $1 }' file.txt Regards Jikuu (4 Replies)
Discussion started by: Jikuu
4 Replies

4. Shell Programming and Scripting

Comparing multiple variables

Hi! I've come up with a ksh-script that produces one or more lists of hosts. At the and of the script, I would like to print only those hosts that exists in all the lists. Ex. HOSTS="host1 host2 host3 host11" HOSTS="host1 host2 host4" HOSTS="host2 host11" HOSTS="host2 host5 host6 host7... (1 Reply)
Discussion started by: Bugenhagen
1 Replies

5. Shell Programming and Scripting

Comparing variables in awk

I'm writing a shellscript that monitors the price of a watch. If the prices changes, it should email me. The body of the email will show the old price and the new price. However when I compare the two awk variables(oldprice and newprice) it always says they're not the same. The shellscript goes out... (2 Replies)
Discussion started by: Shinsuio
2 Replies

6. UNIX for Dummies Questions & Answers

comparing variables

im trying to compare ipaddresses. i loop through an array to see if the ip is already is in the array and if it is it should set a flag and then i wont add it to the array. but its just adding all the ipaddresses to the array if ] then ... (3 Replies)
Discussion started by: magnia
3 Replies

7. Shell Programming and Scripting

Comparing Variables in Perl

Hi. I have three arrays. @a=('AB','CD','EF'); @b=('AB,'DG',HK'); @c=('DD','TT','MM'); I want to compare the elements of the first two array and if they match then so some substition. I tried using the if statement using the scalar value of the array but its not giving me any output. ... (7 Replies)
Discussion started by: kamitsin
7 Replies

8. Shell Programming and Scripting

Grabbing variables and comparing

I have two computers with dynamic IP addresses and am using dyndns so that they are identifiable as the same computer even if their IPs change (we'll call them host1.dyndns.com and host2.dyndns.com). I also have a remote server which I would like to store my computers' IP addresses on. There is a... (9 Replies)
Discussion started by: kerpm
9 Replies

9. Shell Programming and Scripting

Comparing two variables

Script #!/bin/sh hardware=PC os=WindowsNET for i in `cat newservers` do x=`sudo /opt/openv/netbackup/bin/admincmd/bpplclients |grep $i |head -40 |grep $i|awk '{print $3;exit}'` if then echo "$i is already added" else echo "Need to add" fi done O/p in debug mode bash-2.05$... (3 Replies)
Discussion started by: rajip23
3 Replies

10. UNIX for Dummies Questions & Answers

comparing variables

I have searched and found a few threads that have dealt with this, but the examples I've tried haven't seemed to help. I am monitoring our database log for high checkpoints. I can parse out the checkpoint value which can be anywhere from zero into a 3 digit number. I set a variable to be the... (3 Replies)
Discussion started by: MizzGail
3 Replies
Login or Register to Ask a Question