1

I created this small bash script to download repos for me. However if statement are not evaluating it properly....I tried many different variation but it just don't work.

os=$(lsb_release -si)
version=$(lsb_release -sr)
arch = `getconf LONG_BIT`
if [[$os = 'CentOs' && $version >= '6.0' && $arch = '64']]
then
    echo "Works fine"
    echo $version 
    echo $os 
fi

Just as as info: Both echo $os & echo $version work fine OUTSIDE if statements.

Update : Output of bash -x test.sh

++ lsb_release -si
+ os=CentOS
++ lsb_release -sr
+ version=6.4
++ getconf LONG_BIT
+ arch=64
+ [[ CentOS = \C\e\n\t\O\s ]]
Werulz
  • 751
  • 1
  • 6
  • 7

2 Answers2

3

This:

arch = `getconf LONG_BIT`

should be:

arch=`getconf LONG_BIT`

or better:

arch=$(getconf LONG_BIT)

You can't use spaces around assignments in bash. What you wrote executed a command called arch with arguments = and the output from getconf LONG_BIT (presumably 32 or 64).

You also need to keep command names (such as [[) separate from the arguments, in general. So, you need to change this:

if [[$os = 'CentOs' && $version >= '6.0' && $arch = '64']]

so it has spaces around the 'command' name (after the [[ and before the ]]):

if [[ $os = 'CentOs' && $version >= '6.0' && $arch = '64' ]]

I'd probably enclose the variables in double quotes, but it isn't 100% necessary. Then there's the problem that [[ does not support the <= or >= operators (anyone know why not?). So, you have to use negated logic:

if [[ $os = 'CentOs' && ! ($version < '6.0') && $arch = '64' ]]

Be careful. Attach the ! to the ($version < '6.0') and you get 'event not found' (if I wanted a sea shell, I'd go to the sea shore; I wish they'd leave historical C shell notations out of bash).

os=$(lsb_release -si)
version=$(lsb_release -sr)
arch=$(getconf LONG_BIT)
if [[ $os = 'CentOs' && ! ($version < '6.0') && $arch = '64' ]]
then
    echo "Works fine"
    echo "Version $version; OS $os; Arch $arch"
fi
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • test.sh: line 5: syntax error near `'6.0'' test.sh: line 5: `if [[ $os = 'CentOs' && $version >= '6.0' && $arch = '64' ]]' – Werulz Mar 19 '13 at 10:36
  • My guess at the lack of string '<=' and '>=': other than for version numbers, you rarely, if ever, need these conditions. – chepner Mar 19 '13 at 12:02
  • I copied the snippet you gave me above and i remove version so that i works but i still get no output(no error message) – Werulz Mar 19 '13 at 13:09
  • I suggest that you add the code from the version you are running to the question, and show the output of `bash -x yourscript.sh` too. Given: `os=CentOs; version=6.0; arch=64; if [[ $os = 'Centos' && ! ($version < '6.0') && $arch = '64' ]]; then echo Hi; fi`, I get 'Hi' echoed. What about you? – Jonathan Leffler Mar 19 '13 at 14:40
  • I added output of `bash -x yourscript.sh` in main post – Werulz Mar 19 '13 at 18:29
-1

To compare floating point numbers in Bash try the following:

- if [[$os = 'CentOs' && $version >= '6.0' && $arch = '64']]
+ if [[ $os = 'CentOs' ]] && [[ "$(echo "if (${version} >= 6.0 1 else 0" | bc)" -eq 1 ]] && [[ $arch = '64' ]]

See: How to compare two decimal numbers in bash/awk?

Community
  • 1
  • 1
wyr
  • 1
  • Version numbers that have multiple fields (e.g. 6.0.1) cannot be treated as floating point numbers. – chepner Mar 19 '13 at 12:36