Il tuo post contiene in realtà 2 domande.
-
Il
-eflag ordina allo script di uscire in caso di errore. Più flagSe c'è un errore uscirà immediatamente.
-
Il
$?è lo stato di uscita dell'ultimo comando. In Linux uno stato di uscita di0significa che il comando ha avuto successo. Qualsiasi altro stato significherebbe che si è verificato un errore.
Per applicare queste risposte al tuo script:
egrep "^username" /etc/passwd >/dev/null
cercherebbe il username nel /etc/passwd file.
-
Se lo trova allora lo stato di uscita
$?sarà uguale a0. -
Se non lo trova, lo stato di uscita sarà qualcos'altro (non
0). Qui, vorrai eseguireecho "doesn't exist"parte del codice.
Purtroppo c'è un errore nel tuo script e dovresti eseguire quel codice se l'utente esiste - cambia la riga in
if [ $? -ne 0 ]
per ottenere la logica giusta.
Tuttavia se l'utente non esiste, egrep restituirà un codice di errore e a causa del -e opzione la shell uscirà immediatamente dopo quella riga, quindi non raggiungeresti mai quella parte del codice.
Tutte le opzioni della riga di comando di bash sono documentate in 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.