1

I'm working on a bash script and ran into an issue. I'm pushing what I know from using normal if/fi statements and trying to use them to set variables related to picking up the OS version, such as RH 7, Debian 7, Ubuntu, etc. The problem I'm having is that it's just not working like I thought it would. I run each part by hand and it works just fine, but when I put everything in a script, nothing.

Is there a better way to do this? Thank you in advance

value1=$(cat /etc/redhat-release | awk '{ print $4 }' | cut -c 1)
value2=$(cat /etc/redhat-release | awk '{ print $3 }' | cut -c 1)
value3=$(lsb_release -sr)
value4=$(cat /etc/debian_version)
if [[ $value1 -eq 7 ]] && [[ -e /etc/redhat-release ]]; then
        OS=RedHat
        VER=RH7
elif [[ $value2 -eq 6 ]] && [[ -e /etc/redhat-release ]]; then
        OS=RedHat
        VER=RH6
elif [[ $value3 = 14.04 ]]; then
        OS=Ubuntu
        VER=UB1404
elif [[ -e /etc/debian_version ]] && [[ $value4 -eq 7 ]]; then
        OS=Debian
        VER=DEB7
fi
five0va
  • 31
  • 2
  • 2
    There are already programs which do this and much more, for instance `facter`. Why are you trying to do this? – Michael Hampton Apr 29 '15 at 22:21
  • not bash, but i use this pretty often: `python -m platform` will provide output: `Linux-3.2.0-4-amd64-x86_64-with-debian-7.3` – Navern Apr 29 '15 at 22:31
  • This is for servers that are managed by Puppet. We have Puppet and I'm aware of pulling this info via facter, but not everything is or will be managed this way. Just giving us options. – five0va Apr 29 '15 at 23:03
  • 1
    If the server has the Puppet agent on it, it will already have facter. And you can even install facter independently of the Puppet agent, for things that (for whatever reason) aren't managed by puppet; virtually all Linux distributions ship the open source version of facter. So I don't see any reason why you can't use it. – Michael Hampton Apr 29 '15 at 23:56

1 Answers1

0

I think your problem might be some issues with comparison of floats and strings.
AFAIK, test can't deal with float values, so your test for $value3 = 14.04 will give you an syntax error: invalid arithmetic operator (error token is ".04").

You could just treat the numbers as String like this:
elif [[ "$value3" == "14.04" ]]; then
Also notice the quotes around $value3, which is also necessary to treat the 7.8, which was returned in my case of debian 7.8, as string.

If you'd like to compare the main version number only, you can also strip the part after the point:
value3=$(lsb_release -sr | cut -d. -f1)
This will split the string returned by lsb_release -sr at the . which we defined as delimiter with the -d. parameter and then select the first column of the result with -f1.
This also works with value4:
value4=$(cat /etc/debian_version | cut -d. -f1)
This cuts the output of my /etc/debian_version which was 7.8 before and 7 after adding the cut.

You also might want to supress warnings for non-existent files. I don't have a redhat system available, so I redirected the error messages from the first 2 commands to /dev/null like this:
value1=$(cat /etc/redhat-release 2> /dev/null | awk '{ print $4 }' | cut -c 1 )

Let me know if you need any other help. Also please add some outputs and maybe even expected outputs of your script so we can see where your problem is.

wullxz
  • 1,073
  • 2
  • 16
  • 29