|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 9 11 , 5 41 , Stephane CHAZELAS <this.addr...@is.invalid> 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 > > [...] > > 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 Thank I will check using [] && [] & [] when more than 2 operands. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
2007-09-11, 19:59(-07), moonhk:
> On 9 11 , 5 41 , Stephane CHAZELAS <this.addr...@is.invalid> 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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Sep 12, 9:15 pm, "Anderson J. S." <anjoe...@gmail.com> 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. |
|
![]() |
| Outils de la discussion | |
|
|