Re: The usage of [[ (not with if)

2010-08-04 Thread Bob Proulx
Andreas Schwab wrote:
> Bob Proulx writes:
> > Neither of those produce any output.
> >
> >   $ printf '%d\n' ""
> >   0
> 
> Since the command substitution is not quoted the result of the expansion
> is subject to field splitting, thus expands to nothing at all instead of
> a single empty argument.

Ah, yes, you are right of course.  I should have said:

  $ printf '%d\n'
  0

Thanks!
Bob



Re: The usage of [[ (not with if)

2010-08-04 Thread Andreas Schwab
Bob Proulx  writes:

> Neither of those produce any output.
>
>   $ printf '%d\n' ""
>   0

Since the command substitution is not quoted the result of the expansion
is subject to field splitting, thus expands to nothing at all instead of
a single empty argument.

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



Re: The usage of [[ (not with if)

2010-08-04 Thread Bob Proulx
Peng Yu wrote:
> I have the following script and output. The man page says "Return  a
> status  of  0 or 1 depending on the evaluation of the conditional
> expression expression." Therefore, I thought that the two printf
> statements should print 1 and 0 respectively. But both of them print
> 0. I'm wondering what [[ should return if it is not used with a if
> statement.

The problem is that you have confused the character output to stdout
with the exit code status return of the program.

> $ cat main.sh
> #!/usr/bin/env bash
> 
> printf '%d\n' `[[ 10 -gt 1 ]]`
> printf '%d\n' `[[ 1 -gt 10 ]]`

The backticks invoke the command, save the character output to stdout,
replace the backticks with that output.  The exit code is not used in
your example.  The output of those is nothing.  When printf evaluates
nothing as an integer it resolves to 0 and therefore 0 is output.

Look at this:

  $ [[ 10 -gt 1 ]]
  $ [[ 1 -gt 10 ]]

Neither of those produce any output.

  $ printf '%d\n' ""
  0

To print the exit code you would need something like this:

  $ [[ 10 -gt 1 ]] ; echo $?
  0
  $ [[ 1 -gt 10 ]] ; echo $?
  1

Bob



Re: The usage of [[ (not with if)

2010-08-04 Thread Davide Brini
On Wednesday 04 Aug 2010 16:03:25 Peng Yu wrote:
 
> I have the following script and output. The man page says "Return  a
> status  of  0 or 1 depending on the evaluation of the conditional
> expression expression." Therefore, I thought that the two printf
> statements should print 1 and 0 respectively. But both of them print
> 0. I'm wondering what [[ should return if it is not used with a if
> statement.
> 
> $ cat main.sh
> #!/usr/bin/env bash
> 
> printf '%d\n' `[[ 10 -gt 1 ]]`
> printf '%d\n' `[[ 1 -gt 10 ]]`
> if [[ 10 -gt 1 ]]
> then
>   echo xxx
> fi
> $ ./main.sh
> 0
> 0
> xxx

You're confusing "return value of the command" with "output of the command".

-- 
D.



The usage of [[ (not with if)

2010-08-04 Thread Peng Yu
Hello All,

I have the following script and output. The man page says "Return  a
status  of  0 or 1 depending on the evaluation of the conditional
expression expression." Therefore, I thought that the two printf
statements should print 1 and 0 respectively. But both of them print
0. I'm wondering what [[ should return if it is not used with a if
statement.

$ cat main.sh
#!/usr/bin/env bash

printf '%d\n' `[[ 10 -gt 1 ]]`
printf '%d\n' `[[ 1 -gt 10 ]]`
if [[ 10 -gt 1 ]]
then
  echo xxx
fi
$ ./main.sh
0
0
xxx

-- 
Regards,
Peng