LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2008, 08:37 AM   #1
mastertaf
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
Bash scripting problem


Hello,

this is my first post so i hope that am doint this right. I have a fairly simple problem, or it should be. I searched on google wasnt able to find an answer that i could understand. I am making a script to execute some common task on my linux servers and within the script i would like to identify the version of the os. I only use CentOS, and on red hat systems there is a file name /etc/redhat-release that contains the info i need. here is part of the code that is not working:

Code:
VERSION= cat /etc/redhat-release | awk '{print $3}'

if [ $VERSION > 5 ];then
        echo "Centos version 4"
else
        echo "Centos Version 5"
fi
I have, i think, identified part of the problem. The variable value is a string so i guess the logical test cannot work...and since i am not sure how to convert the value to an integer..

Thank you in advance.
 
Old 09-19-2008, 08:57 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Maybe try it using something like:

if [ $VERSION -gt 5 ]
or
if [[ $VERSION > 5 ]]

Some of the syntax of BASH is pretty inscrutable---the best tool I have found is to keep "ABS" on my computer and use it for searches. (Print it only if you really hate trees....)
http://tldp.org/LDP/abs/html/
 
Old 09-19-2008, 08:58 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
PS:
Everything is stored as strings--BASH treats strings as numbers depending on context
 
Old 09-19-2008, 09:03 AM   #4
mastertaf
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Bash scripting problem - SOLVED

Thank you pixellany your solution worked perfectly. it was simply a question of adding the extra [].

I will keep ABS on my copmuter from now on!

Code:
#!/bin/bash
VERSION=`cat /etc/redhat-release | awk '{print $3}'`
echo $VERSION
if [[ $VERSION < 5 ]];then
        echo "Centos version 4"
else
        echo "Centos Version 5"
fi
 
Old 09-19-2008, 09:04 AM   #5
234107
LQ Newbie
 
Registered: Sep 2008
Posts: 8

Rep: Reputation: 0
Quote:
Originally Posted by mastertaf View Post
Hello,

this is my first post so i hope that am doint this right. I have a fairly simple problem, or it should be. I searched on google wasnt able to find an answer that i could understand. I am making a script to execute some common task on my linux servers and within the script i would like to identify the version of the os. I only use CentOS, and on red hat systems there is a file name /etc/redhat-release that contains the info i need. here is part of the code that is not working:

Code:
VERSION= cat /etc/redhat-release | awk '{print $3}'

if [ $VERSION > 5 ];then
        echo "Centos version 4"
else
        echo "Centos Version 5"
fi
I have, i think, identified part of the problem. The variable value is a string so i guess the logical test cannot work...and since i am not sure how to convert the value to an integer..

Thank you in advance.
Try this...

Code:
VERSION=$(cat /etc/redhat-release | awk '{print $3}')

if [ $VERSION > 5 ];then
        echo "Centos version 4"
else
        echo "Centos Version 5"
fi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting Problem... utopian_penguin Linux - General 11 11-08-2006 01:39 PM
bash scripting problem sutley Linux - Software 1 12-17-2004 11:33 AM
bash scripting problem Sammy2ooo Linux - General 3 12-13-2004 12:31 PM
BASH scripting problem deadlock Programming 5 08-15-2003 04:33 AM
bash scripting problem raven Programming 7 03-10-2003 05:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration