?
Solved

UNIX command question with comparisons of values

Posted on 2014-08-06
7
Medium Priority
?
455 Views
Last Modified: 2014-08-06
I need to compare the output of a shell command with the content of a file in UNIX.

The command is like:
/home/user1/Python/bin/bq --format prettyjson show mytab1 | grep "lastModifiedTime" | awk -F"\"" '{print $4}'
The output of the above command is for example 1407289110416

Now the content of the UNIx file is for example:
$cat last_update.log
1407289110112

How do I store the output of the above command (bq command) in a variable say var1 and then the content of the file last_update.log in var2 and compare var1 with var2. If var1 < var2 then I exit the program (do no thing). If var1 > var2, I replace the content of last_update.log by a command like this:
echo `date +%s` > last_update.log

And execute a separate command like 'mycommand do something'

Could you please assist me to do the above comparison?

Thanks a lot.
0
Comment
Question by:toooki
  • 3
  • 3
7 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 40243331
You don't say which shell you are running, but you might try
var1=`/home/user1/Python/bin/bq --format prettyjson show mytab1 | grep "lastModifiedTime" | awk -F"\"" '{print $4}'`
var2=`cat last_update.log`
0
 
LVL 23

Expert Comment

by:savone
ID: 40243392
#!/bin/bash
# Create Variables
var1=`/home/user1/Python/bin/bq --format prettyjson show mytab1 | grep "lastModifiedTime" | awk -F"\"" '{print $4}'`
var2=`cat last_update.log`

#Compare Variables
if [ "$var1" > "$var2" ]; then
         echo `date +%s` > last_update.log
         # Place another command below here to execute

fi
0
 

Author Comment

by:toooki
ID: 40244240
Many thanks! But I still have problem with the comparisons.

If I add in script:
echo $var1
echo $var2

When it shows:
1407343187 (value of var1)
1407344104 (value of var2)

if [ "$v1" < "$v2" ]; then
#I also tried if [ $v1 > $v2 ]; then
echo "going to refresh"
fi

The code under if/fi is always executed (it prints "going to refresh"). (The comparison is always true regardless of the > or < sign)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 23

Expert Comment

by:savone
ID: 40244324
because you are using v1 instead of var1 in the if statement.

change:
if [ "$v1" < "$v2" ]; then

to
if [ "$var1" < "$var2" ]; then

Or change it to whatever the name of your variables are. In your case it looks like your using var1 and var2.
0
 

Author Comment

by:toooki
ID: 40244613
Thank you. But I think that was my typo.

I tried this code below:
===
#!/bin/bash
v1=1407343187
v2=1407344104

echo $v1
echo $v2


if [ "$v1" < "$v2" ]; then
echo "going to refresh as v1 < v2"
fi


if [ "$v1" > "$v2" ]; then
echo "going to refresh as v1 > v2"
fi
===

And it prints both lines.

1407343187
1407344104
going to refresh as v1 < v2
going to refresh as v1 > v2
0
 
LVL 23

Accepted Solution

by:
savone earned 2000 total points
ID: 40244732
I am sorry, I gave you the wrong operators...  try this...


#!/bin/bash
v1=1407343187
v2=1407344104

echo $v1
echo $v2

if [ "$v1" -lt "$v2" ]; then
echo 'going to refresh as v1 < v2'
fi

if [ "$v1" -gt "$v2" ]; then
echo 'going to refresh as v1 > v2'
fi

Open in new window

0
 

Author Comment

by:toooki
ID: 40245081
Thanks a lot!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question