UNIX command question with comparisons of values

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.
toookiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
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
savoneCommented:
#!/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
toookiAuthor Commented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

savoneCommented:
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
toookiAuthor Commented:
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
savoneCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
toookiAuthor Commented:
Thanks a lot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.