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 ]]
1
  • Do not destroy the question by rewriting it so that it only contains the answer. Commented Mar 19, 2013 at 10:52

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

5 Comments

test.sh: line 5: syntax error near '6.0'' test.sh: line 5: if [[ $os = 'CentOs' && $version >= '6.0' && $arch = '64' ]]'
My guess at the lack of string '<=' and '>=': other than for version numbers, you rarely, if ever, need these conditions.
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)
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?
I added output of bash -x yourscript.sh in main post
-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?

1 Comment

Version numbers that have multiple fields (e.g. 6.0.1) cannot be treated as floating point numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.