Votre message contient en fait 2 questions.
-
Le
-eL'indicateur indique au script de quitter en cas d'erreur. Plus de drapeauxS'il y a une erreur, il se fermera immédiatement.
-
Le
$?est l'état de sortie de la dernière commande. Sous Linux, un état de sortie de0signifie que la commande a réussi. Tout autre statut signifierait qu'une erreur s'est produite.
Pour appliquer ces réponses à votre script :
egrep "^username" /etc/passwd >/dev/null
chercherait le username dans le /etc/passwd fichier.
-
S'il le trouve, le statut de sortie
$?sera égal à0. -
S'il ne le trouve pas, le statut de sortie sera autre chose (pas
0). Ici, vous voudrez exécuter leecho "doesn't exist"partie du code.
Malheureusement il y a une erreur dans votre script, et vous exécuterez ce code si l'utilisateur existe - changer la ligne en
if [ $? -ne 0 ]
pour bien comprendre la logique.
Cependant si l'utilisateur n'existe pas, egrep renverra un code d'erreur, et en raison du -e option, le shell quittera immédiatement après cette ligne, vous n'atteindrez donc jamais cette partie du code.
Tous les commutateurs de ligne de commande bash sont documentés dans man bash .
-e Exit immediately if a pipeline (which may consist of a
single simple command), a subshell command enclosed in
parentheses, or one of the commands executed as part of
a command list enclosed by braces (see SHELL GRAMMAR
above) exits with a non-zero status. The shell does not
exit if the command that fails is part of the command
list immediately following a while or until keyword,
part of the test following the if or elif reserved
words, part of any command executed in a && or || list
except the command following the final && or ||, any
command in a pipeline but the last, or if the command's
return value is being inverted with !. A trap on ERR,
if set, is executed before the shell exits. This option
applies to the shell environment and each subshell envi-
ronment separately (see COMMAND EXECUTION ENVIRONMENT
above), and may cause subshells to exit before executing
all the commands in the subshell.