[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Cannot form valid test expressions that involve brackets as string c
From: |
Greg Wooledge |
Subject: |
Re: Cannot form valid test expressions that involve brackets as string comparison targets |
Date: |
Thu, 8 Oct 2009 08:03:01 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Wed, Oct 07, 2009 at 08:07:19PM +1030, Lyall Pearce wrote:
> Description:
> Cannot form expressions which involve left or right brackets.
Parentheses?
> Repeat-By:
>
> basePic="(2008-04)"
> if [ "${basePic:0:1}" = '(' -a "${basePic:4:1}" = ')' ]
> then
> echo "Got brackets"
> fi
if [ "${basePic:0:1}" = "(" ] && [ "${basePic:(-1):1}" = ")" ]; then
echo "Got parentheses"
fi
When using [ instead of [[, I would strongly recommend never using -a
or -o inside it. POSIX has strict rules about exactly how to treat
a [ command with a specific number of arguments, and often the most
common cases are technically illegal. The workaround is to use multiple
[ commands strung together with && and || as needed.
Or, the other workaround would be to use [[, since you're already using
${string:start:length} which is non-POSIX syntax.
if [[ "${basePic:0:1}" = "(" && "${basePic:(-1):1}" = ")" ]]; then
echo "Got parentheses"
fi
Both of those worked for me in bash 4.0.10.