Comparing two strings in Bash
Clash Royale CLAN TAG#URR8PPP
up vote
23
down vote
favorite
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?
bash shell-script test
add a comment |Â
up vote
23
down vote
favorite
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?
bash shell-script test
9
-eq
is true for integers, you want to test for a string or regex (==
or=~
): mywiki.wooledge.org/BashGuide/TestsAndConditionals
â jasonwryan
Jul 17 '14 at 18:11
Thanksjasonwryan
I'll take a look at this resource!
â DemiSheep
Jul 17 '14 at 18:21
add a comment |Â
up vote
23
down vote
favorite
up vote
23
down vote
favorite
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?
bash shell-script test
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?
bash shell-script test
edited Jul 18 '14 at 10:16
professorfish
1032
1032
asked Jul 17 '14 at 18:07
DemiSheep
4864821
4864821
9
-eq
is true for integers, you want to test for a string or regex (==
or=~
): mywiki.wooledge.org/BashGuide/TestsAndConditionals
â jasonwryan
Jul 17 '14 at 18:11
Thanksjasonwryan
I'll take a look at this resource!
â DemiSheep
Jul 17 '14 at 18:21
add a comment |Â
9
-eq
is true for integers, you want to test for a string or regex (==
or=~
): mywiki.wooledge.org/BashGuide/TestsAndConditionals
â jasonwryan
Jul 17 '14 at 18:11
Thanksjasonwryan
I'll take a look at this resource!
â DemiSheep
Jul 17 '14 at 18:21
9
9
-eq
is true for integers, you want to test for a string or regex (==
or =~
): mywiki.wooledge.org/BashGuide/TestsAndConditionalsâ jasonwryan
Jul 17 '14 at 18:11
-eq
is true for integers, you want to test for a string or regex (==
or =~
): mywiki.wooledge.org/BashGuide/TestsAndConditionalsâ jasonwryan
Jul 17 '14 at 18:11
Thanks
jasonwryan
I'll take a look at this resource!â DemiSheep
Jul 17 '14 at 18:21
Thanks
jasonwryan
I'll take a look at this resource!â DemiSheep
Jul 17 '14 at 18:21
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
32
down vote
accepted
-eq
is an arithmetic operator, which compares two numbers.
Use =
(portable/standard sh
), =~
or ==
instead.
Also use quotes, because if $PACKAGENAME
contains a whitespace or wildcard character, 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
.
1
Ah! Thank you! It worked! I am obviously a rookie at this. I'm grateful for your help!
â DemiSheep
Jul 17 '14 at 18:16
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
Jul 17 '14 at 18:17
1
Thankspolym
I'll take a look, thanks for the resource! I'll open that tab up next to my VI guide. :)
â DemiSheep
Jul 17 '14 at 18:18
4
Within double brackets, no word splitting is done, so[[ $PACKAGENAME == "kakadu..." ]]
is OK.
â glenn jackman
Jul 17 '14 at 19:12
4
@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
Jul 17 '14 at 22:12
 |Â
show 2 more comments
up vote
7
down vote
Replace -eq
with ==
so your if block would be this:-
if [ $PACKAGENAME == kakadu-v6_4-00902C ]; then
echo "successfully entered if block!!"
fi
5
Don't forget quoting! Have a look here, why: mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D
â polym
Jul 17 '14 at 18:23
@polym hey thanks, but I just gave the minimalist version that was working ;) :D .
â beginer
Jul 18 '14 at 5:34
add a comment |Â
up vote
4
down vote
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() ( i= : $2?^MERR: not enough parameters! #$#>=2 || quit w/ err ^M == r
z() return $(($#1>0)) ; #return 1 if $#1>0 else 0
until z "$2+?" $((i=i+1)) #until $2 is not set...
do ! z "$1" && z "$1#"$2"" || #$1 != '' && $1 - $2 == '' or...
exit $((i$1:++1)) #exit $? == failed arg count
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 the argument number which first fails the check.
In your case it can be used like:
###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() #save $?; ! exclusive
DEMO:
i= s=string
aeqecho $s #1
aeqecho $s $s #2
aeqecho "$s $s" #3
aeqecho "$s $s" "$s string" #4
aeqecho "$s1" $s string #5
aeqecho "" "" "" #6
aeqecho "" "$s" $s #7
aeqecho 1 "$s#$s1" $((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:
ERR: not enough parameters!
2 : yay
ERR: not enough parameters!
4 : yay
5 : shite - arg2 failed
6 : shite - arg1 failed
7 : shite - arg1 failed
8 : yay
9 : shite - arg13 failed
10 : yay
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
32
down vote
accepted
-eq
is an arithmetic operator, which compares two numbers.
Use =
(portable/standard sh
), =~
or ==
instead.
Also use quotes, because if $PACKAGENAME
contains a whitespace or wildcard character, 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
.
1
Ah! Thank you! It worked! I am obviously a rookie at this. I'm grateful for your help!
â DemiSheep
Jul 17 '14 at 18:16
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
Jul 17 '14 at 18:17
1
Thankspolym
I'll take a look, thanks for the resource! I'll open that tab up next to my VI guide. :)
â DemiSheep
Jul 17 '14 at 18:18
4
Within double brackets, no word splitting is done, so[[ $PACKAGENAME == "kakadu..." ]]
is OK.
â glenn jackman
Jul 17 '14 at 19:12
4
@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
Jul 17 '14 at 22:12
 |Â
show 2 more comments
up vote
32
down vote
accepted
-eq
is an arithmetic operator, which compares two numbers.
Use =
(portable/standard sh
), =~
or ==
instead.
Also use quotes, because if $PACKAGENAME
contains a whitespace or wildcard character, 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
.
1
Ah! Thank you! It worked! I am obviously a rookie at this. I'm grateful for your help!
â DemiSheep
Jul 17 '14 at 18:16
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
Jul 17 '14 at 18:17
1
Thankspolym
I'll take a look, thanks for the resource! I'll open that tab up next to my VI guide. :)
â DemiSheep
Jul 17 '14 at 18:18
4
Within double brackets, no word splitting is done, so[[ $PACKAGENAME == "kakadu..." ]]
is OK.
â glenn jackman
Jul 17 '14 at 19:12
4
@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
Jul 17 '14 at 22:12
 |Â
show 2 more comments
up vote
32
down vote
accepted
up vote
32
down vote
accepted
-eq
is an arithmetic operator, which compares two numbers.
Use =
(portable/standard sh
), =~
or ==
instead.
Also use quotes, because if $PACKAGENAME
contains a whitespace or wildcard character, 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
.
-eq
is an arithmetic operator, which compares two numbers.
Use =
(portable/standard sh
), =~
or ==
instead.
Also use quotes, because if $PACKAGENAME
contains a whitespace or wildcard character, 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
.
edited Aug 11 '14 at 7:30
Stéphane Chazelas
283k53521855
283k53521855
answered Jul 17 '14 at 18:11
polym
6,29643155
6,29643155
1
Ah! Thank you! It worked! I am obviously a rookie at this. I'm grateful for your help!
â DemiSheep
Jul 17 '14 at 18:16
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
Jul 17 '14 at 18:17
1
Thankspolym
I'll take a look, thanks for the resource! I'll open that tab up next to my VI guide. :)
â DemiSheep
Jul 17 '14 at 18:18
4
Within double brackets, no word splitting is done, so[[ $PACKAGENAME == "kakadu..." ]]
is OK.
â glenn jackman
Jul 17 '14 at 19:12
4
@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
Jul 17 '14 at 22:12
 |Â
show 2 more comments
1
Ah! Thank you! It worked! I am obviously a rookie at this. I'm grateful for your help!
â DemiSheep
Jul 17 '14 at 18:16
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
Jul 17 '14 at 18:17
1
Thankspolym
I'll take a look, thanks for the resource! I'll open that tab up next to my VI guide. :)
â DemiSheep
Jul 17 '14 at 18:18
4
Within double brackets, no word splitting is done, so[[ $PACKAGENAME == "kakadu..." ]]
is OK.
â glenn jackman
Jul 17 '14 at 19:12
4
@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
Jul 17 '14 at 22:12
1
1
Ah! Thank you! It worked! I am obviously a rookie at this. I'm grateful for your help!
â DemiSheep
Jul 17 '14 at 18:16
Ah! Thank you! It worked! I am obviously a rookie at this. I'm grateful for your help!
â DemiSheep
Jul 17 '14 at 18:16
2
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
Jul 17 '14 at 18:17
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
Jul 17 '14 at 18:17
1
1
Thanks
polym
I'll take a look, thanks for the resource! I'll open that tab up next to my VI guide. :)â DemiSheep
Jul 17 '14 at 18:18
Thanks
polym
I'll take a look, thanks for the resource! I'll open that tab up next to my VI guide. :)â DemiSheep
Jul 17 '14 at 18:18
4
4
Within double brackets, no word splitting is done, so
[[ $PACKAGENAME == "kakadu..." ]]
is OK.â glenn jackman
Jul 17 '14 at 19:12
Within double brackets, no word splitting is done, so
[[ $PACKAGENAME == "kakadu..." ]]
is OK.â glenn jackman
Jul 17 '14 at 19:12
4
4
@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
Jul 17 '14 at 22:12
@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
Jul 17 '14 at 22:12
 |Â
show 2 more comments
up vote
7
down vote
Replace -eq
with ==
so your if block would be this:-
if [ $PACKAGENAME == kakadu-v6_4-00902C ]; then
echo "successfully entered if block!!"
fi
5
Don't forget quoting! Have a look here, why: mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D
â polym
Jul 17 '14 at 18:23
@polym hey thanks, but I just gave the minimalist version that was working ;) :D .
â beginer
Jul 18 '14 at 5:34
add a comment |Â
up vote
7
down vote
Replace -eq
with ==
so your if block would be this:-
if [ $PACKAGENAME == kakadu-v6_4-00902C ]; then
echo "successfully entered if block!!"
fi
5
Don't forget quoting! Have a look here, why: mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D
â polym
Jul 17 '14 at 18:23
@polym hey thanks, but I just gave the minimalist version that was working ;) :D .
â beginer
Jul 18 '14 at 5:34
add a comment |Â
up vote
7
down vote
up vote
7
down vote
Replace -eq
with ==
so your if block would be this:-
if [ $PACKAGENAME == kakadu-v6_4-00902C ]; then
echo "successfully entered if block!!"
fi
Replace -eq
with ==
so your if block would be this:-
if [ $PACKAGENAME == kakadu-v6_4-00902C ]; then
echo "successfully entered if block!!"
fi
answered Jul 17 '14 at 18:17
beginer
1,9181016
1,9181016
5
Don't forget quoting! Have a look here, why: mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D
â polym
Jul 17 '14 at 18:23
@polym hey thanks, but I just gave the minimalist version that was working ;) :D .
â beginer
Jul 18 '14 at 5:34
add a comment |Â
5
Don't forget quoting! Have a look here, why: mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D
â polym
Jul 17 '14 at 18:23
@polym hey thanks, but I just gave the minimalist version that was working ;) :D .
â beginer
Jul 18 '14 at 5:34
5
5
Don't forget quoting! Have a look here, why: mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D
â polym
Jul 17 '14 at 18:23
Don't forget quoting! Have a look here, why: mywiki.wooledge.org/BashPitfalls#A.5B_.24foo_.3D_.22bar.22_.5D
â polym
Jul 17 '14 at 18:23
@polym hey thanks, but I just gave the minimalist version that was working ;) :D .
â beginer
Jul 18 '14 at 5:34
@polym hey thanks, but I just gave the minimalist version that was working ;) :D .
â beginer
Jul 18 '14 at 5:34
add a comment |Â
up vote
4
down vote
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() ( i= : $2?^MERR: not enough parameters! #$#>=2 || quit w/ err ^M == r
z() return $(($#1>0)) ; #return 1 if $#1>0 else 0
until z "$2+?" $((i=i+1)) #until $2 is not set...
do ! z "$1" && z "$1#"$2"" || #$1 != '' && $1 - $2 == '' or...
exit $((i$1:++1)) #exit $? == failed arg count
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 the argument number which first fails the check.
In your case it can be used like:
###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() #save $?; ! exclusive
DEMO:
i= s=string
aeqecho $s #1
aeqecho $s $s #2
aeqecho "$s $s" #3
aeqecho "$s $s" "$s string" #4
aeqecho "$s1" $s string #5
aeqecho "" "" "" #6
aeqecho "" "$s" $s #7
aeqecho 1 "$s#$s1" $((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:
ERR: not enough parameters!
2 : yay
ERR: not enough parameters!
4 : yay
5 : shite - arg2 failed
6 : shite - arg1 failed
7 : shite - arg1 failed
8 : yay
9 : shite - arg13 failed
10 : yay
add a comment |Â
up vote
4
down vote
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() ( i= : $2?^MERR: not enough parameters! #$#>=2 || quit w/ err ^M == r
z() return $(($#1>0)) ; #return 1 if $#1>0 else 0
until z "$2+?" $((i=i+1)) #until $2 is not set...
do ! z "$1" && z "$1#"$2"" || #$1 != '' && $1 - $2 == '' or...
exit $((i$1:++1)) #exit $? == failed arg count
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 the argument number which first fails the check.
In your case it can be used like:
###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() #save $?; ! exclusive
DEMO:
i= s=string
aeqecho $s #1
aeqecho $s $s #2
aeqecho "$s $s" #3
aeqecho "$s $s" "$s string" #4
aeqecho "$s1" $s string #5
aeqecho "" "" "" #6
aeqecho "" "$s" $s #7
aeqecho 1 "$s#$s1" $((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:
ERR: not enough parameters!
2 : yay
ERR: not enough parameters!
4 : yay
5 : shite - arg2 failed
6 : shite - arg1 failed
7 : shite - arg1 failed
8 : yay
9 : shite - arg13 failed
10 : yay
add a comment |Â
up vote
4
down vote
up vote
4
down vote
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() ( i= : $2?^MERR: not enough parameters! #$#>=2 || quit w/ err ^M == r
z() return $(($#1>0)) ; #return 1 if $#1>0 else 0
until z "$2+?" $((i=i+1)) #until $2 is not set...
do ! z "$1" && z "$1#"$2"" || #$1 != '' && $1 - $2 == '' or...
exit $((i$1:++1)) #exit $? == failed arg count
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 the argument number which first fails the check.
In your case it can be used like:
###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() #save $?; ! exclusive
DEMO:
i= s=string
aeqecho $s #1
aeqecho $s $s #2
aeqecho "$s $s" #3
aeqecho "$s $s" "$s string" #4
aeqecho "$s1" $s string #5
aeqecho "" "" "" #6
aeqecho "" "$s" $s #7
aeqecho 1 "$s#$s1" $((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:
ERR: not enough parameters!
2 : yay
ERR: not enough parameters!
4 : yay
5 : shite - arg2 failed
6 : shite - arg1 failed
7 : shite - arg1 failed
8 : yay
9 : shite - arg13 failed
10 : yay
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() ( i= : $2?^MERR: not enough parameters! #$#>=2 || quit w/ err ^M == r
z() return $(($#1>0)) ; #return 1 if $#1>0 else 0
until z "$2+?" $((i=i+1)) #until $2 is not set...
do ! z "$1" && z "$1#"$2"" || #$1 != '' && $1 - $2 == '' or...
exit $((i$1:++1)) #exit $? == failed arg count
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 the argument number which first fails the check.
In your case it can be used like:
###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() #save $?; ! exclusive
DEMO:
i= s=string
aeqecho $s #1
aeqecho $s $s #2
aeqecho "$s $s" #3
aeqecho "$s $s" "$s string" #4
aeqecho "$s1" $s string #5
aeqecho "" "" "" #6
aeqecho "" "$s" $s #7
aeqecho 1 "$s#$s1" $((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:
ERR: not enough parameters!
2 : yay
ERR: not enough parameters!
4 : yay
5 : shite - arg2 failed
6 : shite - arg1 failed
7 : shite - arg1 failed
8 : yay
9 : shite - arg13 failed
10 : yay
edited Jul 21 '14 at 21:13
answered Jul 17 '14 at 18:24
mikeserv
44.2k564150
44.2k564150
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f145160%2fcomparing-two-strings-in-bash%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
9
-eq
is true for integers, you want to test for a string or regex (==
or=~
): mywiki.wooledge.org/BashGuide/TestsAndConditionalsâ jasonwryan
Jul 17 '14 at 18:11
Thanks
jasonwryan
I'll take a look at this resource!â DemiSheep
Jul 17 '14 at 18:21