Solved

Basic bash scripting

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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