PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > using if
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

using if

Réponse
 
LinkBack Outils de la discussion
Vieux 11/09/2007, 08h38   #1
moonhk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut using if

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

  Réponse avec citation
Vieux 11/09/2007, 10h31   #2
Ivan Gotovchits
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using if

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.
  Réponse avec citation
Vieux 11/09/2007, 10h41   #3
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using if

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
  Réponse avec citation
Vieux 12/09/2007, 03h59   #4
moonhk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using if

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.

  Réponse avec citation
Vieux 12/09/2007, 08h28   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using if

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
  Réponse avec citation
Vieux 12/09/2007, 20h15   #6
Anderson J. S.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using if

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

  Réponse avec citation
Vieux 26/09/2007, 19h14   #7
fabdeb
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: using if

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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h44.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16966 seconds with 15 queries