www.emsdn.com
Class Profile: Home »» Unix/Linux [Unix/Linux] under "Unix/Linux" »»» conditional expressions [] vs.

conditional expressions [] vs.


With bash, I see that using [[ ]] instead of [ ] with conditional
expressions will let you match a pattern on the right side:
myvar=aabbcc
if [ "$myvar" == *bb* ]; then
echo "we have a match"
fi
if [[ "$myvar" == *bb* ]]; then
echo "we have a match"
fi
This shows that only the second form, [[ ]], does a pattern match.
Question:
Are there other reasons to prefer [[ ]] over [ ] for use with
conditional expressions? And vice versa, are there reasons to prefer
[ ]?


No. 1# | By Developer Tags User at [2008-5-10] | size: 695 bytes

2005-09-03, John Kelly wrote:
With bash, I see that using [[ ]] instead of [ ] with conditional
expressions will let you match a pattern on the right side:

myvar=aabbcc

if [ "$myvar" == *bb* ]; then
echo "we have a match"
fi

if [[ "$myvar" == *bb* ]]; then
echo "we have a match"
fi
--
This shows that only the second form, [[ ]], does a pattern match.

Question:

Are there other reasons to prefer [[ ]] over [ ] for use with
conditional expressions? And vice versa, are there reasons to prefer
[ ]?

I have never used the [[]] form, because it is not portable. If
you need pattern matching, you can use a case statement.

No. 1# | By Developer Tags User at [2008-5-10] | size: 1250 bytes

John Kelly wrote:

With bash, I see that using [[ ]] instead of [ ] with conditional
expressions will let you match a pattern on the right side:
--
myvar=aabbcc

if [ "$myvar" == *bb* ]; then
echo "we have a match"
fi

if [[ "$myvar" == *bb* ]]; then
echo "we have a match"
fi

I am not sure how this evaluates to true in your case:

[bmynars@bman:/home/bmynars]$
=[[ $myvar = *bb ]] && echo K || echo Failed
K
[bmynars@bman:/home/bmynars]$
=[[ $myvar = bb ]] && echo K || echo Failed
Failed

BTW, you do not have to use "quotes" when double square brackets are used
(another advantage of using double square brackets). Also, if Korn shell
and BASH shells are in question, the statement about "double square
brackets" not being portable is not quite true. [[ ]] will do mostly
what 'case' would. But again, we would get in the real of "esthetics" and
one's coding preferences.
--
This shows that only the second form, [[ ]], does a pattern match.

,[ ]
| Question:
`

Are there other reasons to prefer [[ ]] over [ ] for use with
conditional expressions? And vice versa, are there reasons to prefer
,[ ]
| [ ]?
`

No. 1# | By Developer Tags User at [2008-5-10] | size: 1418 bytes

2005-09-03, bmynars@gmail.com wrote:
John Kelly wrote:
>
>With bash, I see that using [[ ]] instead of [ ] with conditional
>expressions will let you match a pattern on the right side:
>>
>>

>myvar=aabbcc
>>

>if [ "$myvar" == *bb* ]; then
>echo "we have a match"
>fi
>>

>if [[ "$myvar" == *bb* ]]; then
>echo "we have a match"
>fi
>

I am not sure how this evaluates to true in your case:

[bmynars@bman:/home/bmynars]$
=[[ $myvar = *bb ]] && echo K || echo Failed
K
[bmynars@bman:/home/bmynars]$
=[[ $myvar = bb ]] && echo K || echo Failed
Failed
--
BTW, you do not have to use "quotes" when double square brackets are used
(another advantage of using double square brackets). Also, if Korn shell
and BASH shells are in question, the statement about "double square
brackets" not being portable is not quite true. [[ ]] will do mostly
what 'case' would. But again, we would get in the real of "esthetics" and
one's coding preferences.

For me it is not a questions of esthetics, but of standards. If
the double brackets were part of an accepted standard, I might
prefer them to a case statement in many instances; but they are
not.

No. 1# | By Developer Tags User at [2008-5-10] | size: 871 bytes

bmynars@gmail.com writes:
>John Kelly wrote:
>
>if [[ "$myvar" == *bb* ]]; then
>echo "we have a match"
>fi
>
>I am not sure how this evaluates to true in your case:
>

[bmynars@bman:/home/bmynars]$
=[[ $myvar = *bb ]] && echo K || echo Failed
K
[bmynars@bman:/home/bmynars]$
=[[ $myvar = bb ]] && echo K || echo Failed
Failed
>
>
>BTW, you do not have to use "quotes" when double square brackets are used
>(another advantage of using double square brackets).
>


If I understand the Bolsky/Korn ksh book correctly, it's useful
to use double-quotes around a string in the right-hand part of
a comparison when it contains metacharacters but you want it
evaluated as a string rather than a pattern.
-Greg

No. 1# | By Developer Tags User at [2008-5-10] | size: 212 bytes

John Kelly <jakelly@shtc.netwrote:
Are there other reasons to prefer [[ ]] over [ ] for use with
conditional expressions? And vice versa, are there reasons to prefer
man bash
man ksh
:-)

No. 1# | By Developer Tags User at [2008-5-10] | size: 1163 bytes

Sat, 3 Sep 2005 13:27:28 -0400, "Chris F.A. Johnson"
<cfajohnson@gmail.comwrote:

I have never used the [[]] form, because it is not portable.

I needed a bash eqivalent of dirname, to be used for determining the
absolute pathname of the current script. For the "dirname" portion of
the bash code below, I could not think of anything better than using
[[ ]]

If anyone knows a better way

#!/bin/bash

quit () {
echo "${0/}: $*" >&2
exit 5
}

set -e -u

if [[ ${0-} != */* ]]; then
my0dir='.'
elif [[ ${0%/*} == '' ]]; then
my0dir='/'
else
my0dir=${0%/*}
fi
test -n "$my0dir" || quit "my own directory has null value"
test -d $my0dir || quit "my own \"$my0dir\" directory not found"
if ! pushd $my0dir &/dev/null; then
quit "my own \"$my0dir\" directory not accessible"
else
mydirpath=$(pwd)
test -n "$mydirpath" || quit "my own directory \$(pwd) returned null value"
test -d $mydirpath || quit "my own \"$mydirpath\" directory path not found"
popd /dev/null
fi

echo "mydirpath=$mydirpath"

No. 1# | By Developer Tags User at [2008-5-10] | size: 948 bytes

04.09.2005, John Kelly <jakelly@shtc.netwrote:
Sat, 3 Sep 2005 13:27:28 -0400, "Chris F.A. Johnson"
><cfajohnson@gmail.comwrote:
>
>I have never used the [[]] form, because it is not portable.
>

I needed a bash eqivalent of dirname, to be used for determining the
absolute pathname of the current script. For the "dirname" portion of
the bash code below, I could not think of anything better than using
[[ ]]

If anyone knows a better way

#v+
shell_dirname() {
while [ "x$1" != "x${1%/}" ]; do set -- "${1%/}"; done
case "$1" in
/* | "") set -- "${1%/*}"; [ -z "$1" ] && set / ;;
*) set "$PWD/${1%/*}" ;;
esac
echo "$1"
}
#v-
Fully SUS-compatible ($1 for echo always begins with "/"), entirely in
shell (unless echo is not a builtin).

I used something similar in my set of scripts for dealing with Slackware
packages ().



Unix/Linux Hot!

Unix/Linux New!


Copyright © 2008 www.emsdn.com • All rights reserved • CMS Theme by www.emsdn.com - 0.266