Solved

Basic bash scripting

Posted on 2012-12-24
2
183 Views
Last Modified: 2013-01-26
Getting into the basics of bash scripting, one thing I'm looking at is numerical comparisons.

I have the following code:

#!/bin/sh

current=`cat /root/VERSION.OLD`
new=`cat /root/VERSION`



if [["$current" -lt "$new"]]; then
echo "current is less than new"
echo "current is " $current
echo "new is " $new


else
echo "current is newer than new"
echo "current is " $current
echo "new is " $new
fi

Output is:

[root@dev ~]# sh rvtest
rvtest: line 8: [[201212241235: command not found
current is newer than new
current is  201212241235
new is  201212241236

Why the error? It's also not doing the comparison.

Line 8 is: if [["$current" -lt "$new"]]; then
0
Comment
Question by:rvcw
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 38719406
if [["$current" -lt "$new"]]; then
should be
if [[ "$current" -lt "$new" ]]; then
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 250 total points
ID: 38719446
As Ozo said, there should be spaces before ]] and after [[.

With [[ ]] you should use < instead of -lt in the Borne style.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now