linux - Find and execute the existing batch command -


i trying restart gui following bash script (under mint + cinnamon):

if ! type gnome-shell > /dev/null;     gnome-shell --replace elif ! type cinnamon > /dev/null;     cinnamon --replace fi 

i got error message, gnome-shell not exist. there way write script multi-platform?

what want is

type gnome-shell &> /dev/null 

the &> redirects both stdout , stderr (bash only). redirected stdout, therefore still error messages. you're interested in return value of type, not output.

also, negation doing there? call gnome-shell if not exist? in case checked return value $?, remember 0 true, 1 false in shells:

type gnome-shell echo $?   # prints '0', indicating success / true, or '1' if gnome-shell not exist 

the return value, or rather exit code / exit status, ($?) evaluated if statement.

a little bit nicer:

function cmdexists() {   type "$1" &> /dev/null }  function echoerr() {   echo "$1" 1>&2 }  if cmdexists gnome-shell;   gnome-shell --replace elif cmdexists cinnamon;   cinnamon --replace else   echoerr 'no shell found'   exit fi 

some more useful thoughts on related topics:

edit: exit codes

actually, every value except 0 false in shell. because programs use these values indicate different errors.

there exceptions. inside (( )) can use "normal" arithmetic... shell arithmetic


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -