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 > reversing the if condition in posix shell
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

reversing the if condition in posix shell

Réponse
 
LinkBack Outils de la discussion
Vieux 01/11/2006, 11h50   #1
Yakov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut reversing the if condition in posix shell

Am I right that 'if ! command; then ' form is not posix (the ! part?) ?

If so, then what are nice ways to reverse the condition in if ? Let's
assume the command after if is not 'test' otherwise it's easy
to reverse (test ! ...). I know several ways but none of them satisfy
me:

1. mytestcmd; if test $? != 0; then body; fi
sometimes i don't like that it adds extra line
2. if mytestcmd; then : nothing; else body; fi
adds 2-3 extra lines
3. mytestcmd || body
sometimes fine, sometimes I just prefer the 'if' form
4. if ! mytestcmd; then body; fi
i suspect this is not posix ????
5. if test `mytestcmd; echo $?` != 0; then body; fi
invokes extra subshell

Yakov

  Réponse avec citation
Vieux 01/11/2006, 11h59   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: reversing the if condition in posix shell

2006-11-1, 03:50(-08), Yakov:
> Am I right that 'if ! command; then ' form is not posix (the ! part?) ?


Yes it is POSIX. It is not Bourne, and early versions of ksh
didn't have it. I'm not sure about initial versions of ash.

All the standard shs or modern Unices have it. (note that
Solaris standard sh is not /bin/sh).

> If so, then what are nice ways to reverse the condition in if ? Let's
> assume the command after if is not 'test' otherwise it's easy
> to reverse (test ! ...). I know several ways but none of them satisfy
> me:
>
> 1. mytestcmd; if test $? != 0; then body; fi
> sometimes i don't like that it adds extra line
> 2. if mytestcmd; then : nothing; else body; fi
> adds 2-3 extra lines
> 3. mytestcmd || body
> sometimes fine, sometimes I just prefer the 'if' form
> 4. if ! mytestcmd; then body; fi
> i suspect this is not posix ????
> 5. if test `mytestcmd; echo $?` != 0; then body; fi
> invokes extra subshell

[...]



not() {
"$@"
[ "$?" -ne 0 ]
}

But beware that it works diffently than "!" in the sense that it
only works for simple commands.

not foo | bar

only reverses foo status.

! foo | bar

reverses the "foo | bar" status.

--
Stéphane
  Réponse avec citation
Vieux 01/11/2006, 16h23   #3
Sven Mascheck
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: reversing the if condition in posix shell

Stephane CHAZELAS wrote:

> I'm not sure about initial versions of ash.


It came with 44BSDalpha, that is,
when ash started aiming at POSIX.

> ! foo | bar
> reverses the "foo | bar" status.


That's why calling it "negation of pipeline"
(a command is only a special case of a pipeline)
is the both correct and to the point.
  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 14h11.


É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,11896 seconds with 11 queries