using if
am 11.09.2007 09:38:24 von moonhk
Hi all
What is different between
if [ "${rtn}" = "0" and "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ; then
if [ "${rtn}" = "0" ] && [ "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ;
then
grep AAA ${shell}/shell_if.txt > /dev/null 2>&1
rtn=$?
rtn2=2
rtn3=3
echo $rtn
if [ "${rtn}" = "0" and "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ; then
echo A
echo B
echo C
else
echo "else"
echo rtn=$rtn
echo rtn2=$rtn2
echo rtn3=$rtn3
fi
Re: using if
am 11.09.2007 11:31:40 von Ivan Gotovchits
moonhk wrote:
> Hi all
>
>
> What is different between
>
> if [ "${rtn}" = "0" and "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ; then
>
> if [ "${rtn}" = "0" ] && [ "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ;
> then
>
>
> grep AAA ${shell}/shell_if.txt > /dev/null 2>&1
> rtn=$?
> rtn2=2
> rtn3=3
> echo $rtn
> if [ "${rtn}" = "0" and "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ; then
> echo A
> echo B
> echo C
> else
> echo "else"
> echo rtn=$rtn
> echo rtn2=$rtn2
> echo rtn3=$rtn3
> fi
IMHO, no difference.
Re: using if
am 11.09.2007 11:41:00 von Stephane CHAZELAS
2007-09-11, 00:38(-07), moonhk:
> Hi all
>
>
> What is different between
>
> if [ "${rtn}" = "0" and "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ; then
>
> if [ "${rtn}" = "0" ] && [ "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ;
> then
[...]
I don't know of any shell or "[" implementation that accepts
"and" as a "[" operator.
In any case, "[" is unreliable when passed more than 3 operands,
so the second form should be prefered. Though, I'd probably
write it:
[ "$rtn" -eq 0 ] && [ "$rtn2" -eq 2 ] && [ "$rtn3" -eq 3 ]
And note that it's not the "if" syntax, but the "[" command
syntax here we're discussing here. What's between "if" and
"then" is just a list of commands.
--
Stéphane
Re: using if
am 12.09.2007 04:59:06 von moonhk
On 9 11 , 5 41 , Stephane CHAZELAS wrote:
> 2007-09-11, 00:38(-07), moonhk:> Hi all
>
> > What is different between
>
> > if [ "${rtn}" =3D "0" and "${rtn2}" =3D "2" ] && [ ${rtn3} =3D 3 ] ; t=
hen
>
> > if [ "${rtn}" =3D "0" ] && [ "${rtn2}" =3D "2" ] && [ ${rtn3} =3D 3 ]=
;
> > then
>
> [...]
>
> I don't know of any shell or "[" implementation that accepts
> "and" as a "[" operator.
>
> In any case, "[" is unreliable when passed more than 3 operands,
> so the second form should be prefered. Though, I'd probably
> write it:
>
> [ "$rtn" -eq 0 ] && [ "$rtn2" -eq 2 ] && [ "$rtn3" -eq 3 ]
>
> And note that it's not the "if" syntax, but the "[" command
> syntax here we're discussing here. What's between "if" and
> "then" is just a list of commands.
>
> --
> St=E9phane
Thank
I will check using [] && [] & [] when more than 2 operands.
Re: using if
am 12.09.2007 09:28:17 von Stephane CHAZELAS
2007-09-11, 19:59(-07), moonhk:
> On 9 11 , 5 41 , Stephane CHAZELAS wrote:
>> 2007-09-11, 00:38(-07), moonhk:> Hi all
>>
>> > What is different between
>>
>> > if [ "${rtn}" = "0" and "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ; then
>>
>> > if [ "${rtn}" = "0" ] && [ "${rtn2}" = "2" ] && [ ${rtn3} = 3 ] ;
>> > then
[...]
>> In any case, "[" is unreliable when passed more than 3 operands,
>> so the second form should be prefered. Though, I'd probably
>> write it:
[...]
> I will check using [] && [] & [] when more than 2 operands.
What I meant is that for instance
[ "${rtn}" = "0" -a "${rtn2}" = "2" ]
That is where the "[" command is passed in this case 7 arguments
(beside the [ and ] ones)
is unreliable.
For instance if $rtn is "!", many "[" implementations will
output an error.
[ "${rtn}" = "0" ] && [ "${rtn2}" = "2" ]
is OK with POSIX shells whatever the value of $rtn or $rtn2.
--
Stéphane
Re: using if
am 12.09.2007 21:15:48 von anjoel.s
Hello for all !
,... Well,... I guess :) just think :),...
"if" <- is a command
"[" <- is alias for "test"
"test" <- is a command
"${rtn}" = "0" and "${rtn2}" <- is parameter for command test
test or [ <- is parameter for command if,...
'if' statement is like eval,.. because run all parameter with
'execv()' an C/C++ function then check this
The default system return is '0' or not zero for executions,.. '0' for
'if' statement is like true or '1',... :)
well now if I to do : '#if [ $a = $b and $b = $c ] && [ $c = $d ]'
"$a = $b and $b = $c" <- is parameter for test command ,.. then
command test parse the 'and' statement,.... and return '0' or '!0' for
shell ,.. ( you can get it from $? ) ,.. the system later will do:
$? && [$c = $d] ,.. look this,.. "&&" will be checked from system and
not by command ,...
here is the difference ,.. [] && [] ,.. the system will run the
test,..
and [ $a and $b ] the command will run the test
--
___________________
Anderson J. de Souza
- Networking and Security -
[ - Professional Consulting - The best firewall - ]
http://anjoel.s.googlepages.com - anjoel.s@gmail.com
Phone: +55 (54) 9115.13.15 - Sip: 1-747-006-0374
Re: using if
am 26.09.2007 20:14:53 von fabdeb
On Sep 12, 9:15 pm, "Anderson J. S." wrote:
> Hello for all !
>
> ,... Well,... I guess :) just think :),...
>
> "if" <- is a command
> "[" <- is alias for "test"
> "test" <- is a command
> "${rtn}" = "0" and "${rtn2}" <- is parameter for command test
> test or [ <- is parameter for command if,...
>
> 'if' statement is like eval,.. because run all parameter with
> 'execv()' an C/C++ function then check this
>
> The default system return is '0' or not zero for executions,.. '0' for
> 'if' statement is like true or '1',... :)
>
> well now if I to do : '#if [ $a = $b and $b = $c ] && [ $c = $d ]'
> "$a = $b and $b = $c" <- is parameter for test command ,.. then
> command test parse the 'and' statement,.... and return '0' or '!0' for
> shell ,.. ( you can get it from $? ) ,.. the system later will do:
> $? && [$c = $d] ,.. look this,.. "&&" will be checked from system and
> not by command ,...
>
> here is the difference ,.. [] && [] ,.. the system will run the
> test,..
> and [ $a and $b ] the command will run the test
>
> --
> ___________________
> Anderson J. de Souza
> - Networking and Security -
>
> [ - Professional Consulting - The best firewall - ]http://anjoel.s.googlepages.com- anjoe...@gmail.com
> Phone: +55 (54) 9115.13.15 - Sip: 1-747-006-0374
cmd a && cmd b .
cmd b will be executed only if cmd a is finished without error.
a and b is if condition a and condition the command will be executed.