Solved

Basic bash scripting

Posted on 2012-12-24
2
185 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWK: Pytthagoras bp script Part deux 22 42
parallel rsync issues with Ubuntu 1 38
Interactive Powershell script to reset password age in AD 4 53
Linux 3 30
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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.:
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…

679 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