i remember some scripts that test variables by the following way:

if [ "X$1" == "X" ]

obviously if $1 is null then the statement is true X==X, and if the variable not null, the statement is false, so

if [ "X$1" == "X" ]
then
echo "arg is null"
else
echo "arg is $1"
fi

will work.


dbrett wrote:

Now I am really confused!

This works:
#!/bin/bash

if [ -z $1 ]; then
echo ''
echo "format is $0 'math equation'"
echo "i.e. $0 (2+2)*3"
echo ''
exit
fi

echo $1 | /usr/bin/bc -l

If you notice the only change I made was go with the better test for a
blank variable.
At least I though.

The problem is with the if condition. I don't understand why, but here
are the conditions. Pay close attention of the spaces around the '[]'

The following work when argument is NOT given

if [$1 == '']
if [ $1 == '']
if [ -z == '' ]

The above, all give error when an argument is added

Any other combination fail when and argumet is NOT given, with the
exception of Bret's:

if [ -z $1 ]

Like I said I don't understand it, but it works.

david

P.S. I agree the '2&>/dev/null is strange






On 11 Dec 2002, Bret Hughes wrote:


On Wed, 2002-12-11 at 12:05, dbrett wrote:

I have a bash to math calucations. It works but also complains about the
last line, even though it works.
Any ideas how to fix the problem or at least not see the error message and
still work.

#!/bin/bash

if [$1 == '']; then
echo ''
echo "format is $0 'math equation'"
echo "i.e. $0 (2+2)*3"
echo ''
exit
fi

echo $1 | /usr/bin/bc -l &2>/dev/null


only way I can get it to work is to quote the equation and fix the
syntax of the test statement.

#!/bin/bash
if [ -z $1 ]; then # spaces inside the [ ] -z test for null string
echo ''
echo "format is $0 'math equation'"
echo "i.e. $0 (2+2)*3"
echo ''
exit
fi

echo $1 | /usr/bin/bc -l 2>/dev/null

Roberts suggestion of dropping the &2> in favor of 2> fixed a really
funky newline thing I will have to figure out some day :)
[bhughes@bretsony bhughes]$ bc.test.sh
format is ./bc.test.sh 'math equation'
i.e. ./bc.test.sh (2+2)*3

[bhughes@bretsony bhughes]$ bc.test.sh (2+2)*3
bash: syntax error near unexpected token `(2+2)'
[bhughes@bretsony bhughes]$ [bhughes@bretsony bhughes]$ bc.test.sh '(2+2)*3'
12

HTH

Bret



--
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list






--
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to