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 > obtain the result of a command (success or failure) directly in a if instruction
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

obtain the result of a command (success or failure) directly in a if instruction

Réponse
 
LinkBack Outils de la discussion
Vieux 05/11/2006, 15h39   #1
Tribulations Parallèles
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut obtain the result of a command (success or failure) directly in a if instruction

Hi,

Usually, to test the return of a command (success or failure), I use a
sequence of two instructions: the instruction, and then the if test:

$ touch bar
$ ls bar
$ if [ "$?" = 0 ];then echo success;fi
success

I would like to run the command between the brackets: is this possible?
The following command does not seem to work:

$ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi

Thanks in advance

Julien
--
"Allez, Monsieur, allez, et la foi vous viendra." (D'Alembert).
  Réponse avec citation
Vieux 05/11/2006, 15h53   #2
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: obtain the result of a command (success or failure) directly in a if instruction

> I would like to run the command between the brackets: is this possible?
> The following command does not seem to work:
>
> $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi


ls bar >/dev/null 2>&1 && echo success || echo failure


Regards
Dimitre


  Réponse avec citation
Vieux 05/11/2006, 15h59   #3
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: obtain the result of a command (success or failure) directly in a if instruction


>> I would like to run the command between the brackets: is this possible?
>> The following command does not seem to work:
>>
>> $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi

>
> ls bar >/dev/null 2>&1 && echo success || echo failure


If you just want to check if the file exists:

[ -e bar ] && echo success || echo failure


Regards
Dimitre


  Réponse avec citation
Vieux 05/11/2006, 16h00   #4
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: obtain the result of a command (success or failure) directly in a if instruction

On Sun, 05 Nov 2006 16:39:22 +0100, Tribulations Parallèles
<Tribulations@Paralleles.invalid> wrote:
> Hi,
>
> Usually, to test the return of a command (success or failure), I use a
> sequence of two instructions: the instruction, and then the if test:
>
> $ touch bar
> $ ls bar
> $ if [ "$?" = 0 ];then echo success;fi
> success
>
> I would like to run the command between the brackets: is this possible?
> The following command does not seem to work:
>
> $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi
>

You don't need brackets. The left bracket itself is a command, a
synonym for "test". (when it's called '[' it requires a ']')

if ls bar 1>/dev/null 2>&1; then echo success; fi

You could also write
if [ -e bar ]; then echo success; fi

By the way, `ls bar 1>/dev/null 2>&1` results in an empty string, which
evaluates as false inside [ ].


--
When asked by an anthropologist what the Indians called America before
the white men came, an Indian said simply "Ours."
-- Vine Deloria, Jr.
  Réponse avec citation
Vieux 05/11/2006, 16h08   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: obtain the result of a command (success or failure) directly in a if instruction

2006-11-05, 16:39(+01), Tribulations Parallèles:
> Hi,
>
> Usually, to test the return of a command (success or failure), I use a
> sequence of two instructions: the instruction, and then the if test:
>
> $ touch bar
> $ ls bar
> $ if [ "$?" = 0 ];then echo success;fi
> success
>
> I would like to run the command between the brackets: is this possible?
> The following command does not seem to work:
>
> $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi

[...]

if ls -d bar > /dev/null 2>&1; then echo success; fi

if relies on the exit status of the list of commands inbetween
"if" and "then" to decide whether to run the "then" command list
or the "else" command list.

"[" is just another command. "[ "$?" -eq 0 ]" is a noop as it
just asks the "[" command to replicate the exit status (at least
the meaning of the exit status) from the previously run command.

IOW, in

cmd
if [ "$?" -eq 0 ]; [ "$?" -eq 0 ]; [ "$?" -eq 0 ]; then...

All the "[" commands are useless as they just perpetrate $?. It
should be:

if cmd; then

[ "$?" -eq 0 ] sets $? to 0 if $? was 0 and $? to 1 if it wasn't
0. Another way to perpetrate $?, could be (exit "$?"), that is
start a subshell and make it exit with the $? exit status.
Again, not very useful.

--
Stéphane
  Réponse avec citation
Vieux 05/11/2006, 16h21   #6
Tribulations Parallèles
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: obtain the result of a command (success or failure) directly in a if instruction

Radoulov, Dimitre wrote:

>> I would like to run the command between the brackets: is this possible?
>> The following command does not seem to work:
>>
>> $ if [ `ls bar 1>/dev/null 2>&1` ];then echo success;fi

>
> ls bar >/dev/null 2>&1 && echo success || echo failure


Thanks a lot, it works. But is it possible to keep the ls instruction in a
if instruction?

Indeed, if there is a lot of instructions to execute in case of success, I
am compelled to use parenthesis:

ls bar 1>/dev/null 2>&1 && (
echo success
echo foo
echo coco
) ||
echo failure

It works, but I am used to using "if" instruction, so I just want to know if
it could be possible to keep the classical construction.

Julien

--
"Allez, Monsieur, allez, et la foi vous viendra." (D'Alembert).
  Réponse avec citation
Vieux 05/11/2006, 16h26   #7
Radoulov, Dimitre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: obtain the result of a command (success or failure) directly in a if instruction


[...]
> Indeed, if there is a lot of instructions to execute in case of success, I
> am compelled to use parenthesis:
>
> ls bar 1>/dev/null 2>&1 && (
> echo success
> echo foo
> echo coco
> ) ||
> echo failure
>
> It works, but I am used to using "if" instruction, so I just want to know
> if
> it could be possible to keep the classical construction.

[...]

if ls bar > /dev/null 2>&1 ; then
echo success
echo foo
echo coco
fi


Regards
Dimitre



  Réponse avec citation
Vieux 05/11/2006, 16h42   #8
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: obtain the result of a command (success or failure) directly in a if instruction

2006-11-5, 11:00(-05), Bill Marcum:
[...]
> You don't need brackets. The left bracket itself is a command, a
> synonym for "test". (when it's called '[' it requires a ']')
>
> if ls bar 1>/dev/null 2>&1; then echo success; fi
>
> You could also write
> if [ -e bar ]; then echo success; fi


It should be noted though that the two commands above are not
equivalent if "bar" is a directory or a symbolic link whose
target we can't determine the existence.

> By the way, `ls bar 1>/dev/null 2>&1` results in an empty string, which
> evaluates as false inside [ ].

[...]

Actually, as it is not quoted, it results in no argument at all
(which is not the same thing as an empty argument) but in any
case, "[" returns false for both [ ] and [ "" ].

--
Stéphane
  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 12h37.


É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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17128 seconds with 16 queries