Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have the following if block in my bash script:

if [ ${PACKAGENAME} -eq kakadu-v6_4-00902C ]; then
  echo "successfully entered if block!!"
fi

The script execution is not entering my if block even though $PACKAGENAME is equal to kakadu-v6_4-00902C. What am I doing wrong?

share|improve this question
7  
-eq is true for integers, you want to test for a string or regex (== or =~): mywiki.wooledge.org/BashGuide/TestsAndConditionals –  jasonwryan yesterday
    
Thanks jasonwryan I'll take a look at this resource! –  DemiSheep yesterday
add comment

3 Answers 3

up vote 13 down vote accepted

-eq is an arithmetic operator, which compares two numbers.

Use ==, =~ or = instead.

Also use quotes, because if ${PACKAGENAME} contains a whitespace, then it will be split into multiple arguments, which causes to make [ see more arguments than desired. See here a list of common bash pitfalls.

if [ "${PACKAGENAME}" == 'kakadu-v6_4-00902C' ]; then
    echo "successfully entered if block!!"
fi

See man bash, search (/) for CONDITIONAL EXPRESSIONS.

share|improve this answer
1  
Ah! Thank you! It worked! I am obviously a rookie at this. I'm grateful for your help! –  DemiSheep yesterday
2  
Have you had a look at tldp.org/LDP/Bash-Beginners-Guide/html ? It's a very nice bash guide and will help you with examples and exams :). –  polym yesterday
1  
Thanks polym I'll take a look, thanks for the resource! I'll open that tab up next to my VI guide. :) –  DemiSheep yesterday
2  
Within double brackets, no word splitting is done, so [[ $PACKAGENAME == "kakadu..." ]] is OK. –  glenn jackman yesterday
3  
@glennjackman Beware however that even within double brackets, you need double quotes around variable expansions on the right-hand side of =, == and !=, because that side is a pattern, not a string. For example, foo='*'; [[ whatever = $foo ]] is true. –  Gilles yesterday
show 1 more comment

Replace -eq with == so your if block would be this:-

if [ ${PACKAGENAME} == kakadu-v6_4-00902C ]; then

        echo "successfully entered if block!!"

fi
share|improve this answer
4  
Don't forget quoting! Have a look here, why: mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D –  polym yesterday
    
@polym hey thanks, but I just gave the minimalist version that was working ;) :D . –  beginer 22 hours ago
add comment

Another way is to negate them:

: ${PACKAGENAME:?'$PACKAGENAME variable is empty!'} #emits error and exits
[ -z "${PACKAGENAME#kakadu-v6_4-00902C}" ] || { #if var - str not empty do block
    echo '$PACKAGENAME is not kakadu-v6_4-00902C' 
    exit 1
} >&2

The above block first tests if "$PACKAGENAME" has any value at all, and, if not it exits with error and echoes ?'this'} to stderr. If its parent shell still exists then the test has passed, and it next tests if removing your 'kakadu...' string from the variable results in an -z empty string. If it does not, then it again emits an error and exits the shell. If your shell still exists at this point anything after the block is executed, otherwise it is not.

Probably this sort of thing is best implemented in a function. Like:

argeq() ( unset ERR
    : ${2-${ERR?not enough parameters!}} #quit w/ err if not at least 2 args
    z() { return $((${#1}>0)) ; } #return 1 if 1st arg's len > 0 else 0
    until z "${2+?}" #until $2 is not set...
    do  ! z "$1" && z "${1#"$2"}" || exit #$1 != '' && $1 - $2 == '' or exit $?
    shift ; done #shift away one param ; continue loop
)

With that function you can provide as many arguments as your system will allow. If you provide fewer than 2 it will return 1 and emit a message to stderr. If you provide 2 or more arguments it will treat all as strings and return 0 if all are identical and not null else it will return 1.

In your case it can be used like:

{   
    PACKAGENAME='kakadu-v6_4-00902C'
    argeq "$PACKAGENAME" kakadu-v6_4-00902C &&
        echo "kakadu-v6_4-00902C == $PACKAGENAME" ||
        echo failure
    ! argeq "${PACKAGENAME#*-}" kakadu-v6_4-00902C &&
        echo "kakadu-v6_4-00902C != ${PACKAGENAME#*-}" ||
        echo failure
}

###OUTPUT###
kakadu-v6_4-00902C == kakadu-v6_4-00902C
kakadu-v6_4-00902C != v6_4-00902C

To demonstrate further I'll write another function:

aeqecho() { i=$((i+1))
    argeq "$@" && 
    echo "$i : yay" || 
    echo "$i : shite"
}

DEMO:

{  i= s=string
   aeqecho $s #1
   aeqecho $s $s #2
   aeqecho "$s $s" #3
   aeqecho "$s $s" "${s} string" #4
   aeqecho "${s}1" $s string #5
   aeqecho "" "" "" #6
   aeqecho "" "$s" $s #7
   aeqecho 1 "${s#$s}1" $((2-1)) #8                     
   aeqecho $s $s $s $s $s $s $s $s $s $s $s $s stng #9  
   aeqecho $s $s $s $s $s $s $s $s $s $s $s $s string #10
}  

OUTPUT:

argeq: line 2: ERR: not enough parameters!
1 : shite
2 : yay
argeq: line 2: ERR: not enough parameters!
3 : shite
4 : yay
5 : shite
6 : shite
7 : shite
8 : yay
9 : shite
10 : yay
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.