mercredi 31 août 2016

Bash IF exit code redirect errors

I'm writing a bash script right now that has a statement to check if a directory contains stuff.

Right now, the code is

if [ "$(ls -A $APPLICATIONS)"  ]; then
    do something
else
    do something else
fi

However, when the folder is indeed empty, I would like the error from ls to be suppressed. Normally, it would say "No such file or directory," but I want to redirect that to /dev/null.

I've tried doing

if [ "$(ls -A $APPLICATIONS &> /dev/null)"  ]; then

That does redirect everything to /dev/null like I wanted, but it also causes the directory to not be found (essentially causing exit code 1).

What would be the right way to do this?

Aucun commentaire:

Enregistrer un commentaire