Re: test or [ does not handle parentheses as stated in manpage
On 9/6/22 9:57 AM, Julian Gilbey wrote: On Tue, Sep 06, 2022 at 02:56:12PM +0100, Julian Gilbey wrote: Ah, thanks! May I suggest that adding this information as a note in the manpage would be very helpful? The man page says, in the `test' description: "Each operator and operand must be a separate argument." Yes, but "(" and ")" are not usually considered to be operators or operands. And I did have them separated, but didn't quote them when I did, so making a comment about that in the manpage would still be helpful. From QUOTING: "Each of the metacharacters listed above under DEFINITIONS has special meaning to the shell and must be quoted if it is to represent itself." -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: test or [ does not handle parentheses as stated in manpage
On 9/6/22 1:51 AM, Julian Gilbey wrote: On Tue, Sep 06, 2022 at 12:03:20AM +0200, Andreas Schwab wrote: On Sep 05 2022, Julian Gilbey wrote: neither did using \( instead of (, and neither did putting spaces around the parentheses. You need to do both. Ah, thanks! May I suggest that adding this information as a note in the manpage would be very helpful? The man page says, in the `test' description: "Each operator and operand must be a separate argument." -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
Re: test or [ does not handle parentheses as stated in manpage
On Tue, Sep 06, 2022 at 02:56:12PM +0100, Julian Gilbey wrote: > > > Ah, thanks! May I suggest that adding this information as a note in > > > the manpage would be very helpful? > > > > The man page says, in the `test' description: > > > > "Each operator and operand must be a separate argument." > > Yes, but "(" and ")" are not usually considered to be operators or > operands. And I did have them separated, but didn't quote them when I did, so making a comment about that in the manpage would still be helpful. Julian
Re: test or [ does not handle parentheses as stated in manpage
On Tue, Sep 06, 2022 at 09:25:36AM -0400, Chet Ramey wrote: > On 9/6/22 1:51 AM, Julian Gilbey wrote: > > On Tue, Sep 06, 2022 at 12:03:20AM +0200, Andreas Schwab wrote: > > > On Sep 05 2022, Julian Gilbey wrote: > > > > > > > neither did using \( > > > > instead of (, and neither did putting spaces around the parentheses. > > > > > > You need to do both. > > > > Ah, thanks! May I suggest that adding this information as a note in > > the manpage would be very helpful? > > The man page says, in the `test' description: > > "Each operator and operand must be a separate argument." Yes, but "(" and ")" are not usually considered to be operators or operands. Julian
Re: test or [ does not handle parentheses as stated in manpage
On Mon, Sep 05, 2022 at 11:04:41PM -0400, Dale R. Worley wrote: > Julian Gilbey writes: > > Upgrading to bash 5.2.0(1)-rc2 did not help, neither did using \( > > instead of (, and neither did putting spaces around the parentheses. > > It's ugly. The first point is that ( and ) are special characters and > if unquoted are isolated tokens that have special syntax. So in order > to get [ to see them as arguments, you have to quote ( and ). But the > quoted characters are ordinary characters and to make them be separate > arguments to [, you have to separate them from the adjacent arguments > with spaces. So this version works: > > if [ \( "$1" = "yes" -o "$1" = "YES" \) -a \( "$2" = "red" -o "$2" = > "RED" \) ] > then > echo "Yes, it's red" > else > echo "No, it's not red" > fi > > Dale Thanks Dale! I agree that it's ugly :) Best wishes, Julian
Re: test or [ does not handle parentheses as stated in manpage
On Mon, Sep 05, 2022 at 06:05:04PM -0400, Greg Wooledge wrote: > On Mon, Sep 05, 2022 at 09:55:29PM +0100, Julian Gilbey wrote: > > if [ ("$1" = "yes" -o "$1" = "YES") -a ("$2" = "red" -o "$2" = "RED") ] > > You're doing it wrong. The parentheses have to be quoted, and separate. > > [ "(" "$1" = yes" -o "$1" = YES" ")" -a ... ] > > I'd strongly recommend that you DON'T do this, for two reasons: > > 1) It's not compatible with the POSIX [ command. > > 2) Bash offers the [[ command instead, which is a lot more powerful. >With [[ you can do it this way: > >[[ ( $1 = yes || $1 = YES ) && ... ]] Oh, nice! Thanks, Julian
Re: test or [ does not handle parentheses as stated in manpage
On Tue, Sep 06, 2022 at 12:03:20AM +0200, Andreas Schwab wrote: > On Sep 05 2022, Julian Gilbey wrote: > > > neither did using \( > > instead of (, and neither did putting spaces around the parentheses. > > You need to do both. Ah, thanks! May I suggest that adding this information as a note in the manpage would be very helpful? Best wishes, Julian
Re: test or [ does not handle parentheses as stated in manpage
Julian Gilbey writes: > Upgrading to bash 5.2.0(1)-rc2 did not help, neither did using \( > instead of (, and neither did putting spaces around the parentheses. It's ugly. The first point is that ( and ) are special characters and if unquoted are isolated tokens that have special syntax. So in order to get [ to see them as arguments, you have to quote ( and ). But the quoted characters are ordinary characters and to make them be separate arguments to [, you have to separate them from the adjacent arguments with spaces. So this version works: if [ \( "$1" = "yes" -o "$1" = "YES" \) -a \( "$2" = "red" -o "$2" = "RED" \) ] then echo "Yes, it's red" else echo "No, it's not red" fi Dale
Re: test or [ does not handle parentheses as stated in manpage
On Mon, Sep 05, 2022 at 09:55:29PM +0100, Julian Gilbey wrote: > if [ ("$1" = "yes" -o "$1" = "YES") -a ("$2" = "red" -o "$2" = "RED") ] You're doing it wrong. The parentheses have to be quoted, and separate. [ "(" "$1" = yes" -o "$1" = YES" ")" -a ... ] I'd strongly recommend that you DON'T do this, for two reasons: 1) It's not compatible with the POSIX [ command. 2) Bash offers the [[ command instead, which is a lot more powerful. With [[ you can do it this way: [[ ( $1 = yes || $1 = YES ) && ... ]]
Re: test or [ does not handle parentheses as stated in manpage
On Sep 05 2022, Julian Gilbey wrote: > neither did using \( > instead of (, and neither did putting spaces around the parentheses. You need to do both. -- Andreas Schwab, sch...@linux-m68k.org GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1 "And now for something completely different."