Search code examples
bashdash-shell

If IP is pingable then do something (convert bash command to dash)


I have a command that should open a program after reboot if a specific IP is reachable. I use Debian so sh is linked to dash. The code that i execute after system start is:

sh -c "sleep 10 && if ping -c 1 stackoverflow.com &> /dev/null; then gedit; fi"

But with this code gedit will be opened in every case.

If i try in a bash console

sleep 10 && if ping -c 1 stackoverflow.com &> /dev/null; then gedit; fi

then it works correctly. So how can i convert the bash command correctly to dash?


Solution

  • sh doesn't have &>. You either need to use bash -c ... or redirect one descriptor at a time:

    sh -c "sleep 1 && if ping -c 1 stackoverflow.com > /dev/null 2>&1; then notify-send hi; fi"