I have an sshfs mount from a remote server that breaks every once in a while due to connectivity issues. To fix this, I have written a script to check if a file exists on the mount, and if not to email me AND reconnect with sshfs.

I am not sure whether I have the right syntax on the second statement after else. Not sure if I need brackets to separate them or a semicolon to end the first statement? Here is the code:

if [ -f "$file" ]


echo "$file found." > /dev/null 2>&1

/usr/sbin/sendmail -t alert-email@foo.com </etc/alert.txt

/bin/sh /etc/fix_mount.sh

After the else, I am first sending the warning message to let us know it happened and then executing a second bash script to reconnect sshfs. I know that /etc/fix_mount.sh works on its own, but I'm afraid the else won't execute properly because of my syntax. Brackets? Semicolon? Is it already OK?

closed as unclear what you're asking by Ipor Sircer, schily, msp9011, Alexander, Thomas Aug 23 '18 at 9:42

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    I can see nothing wrong with your syntax. Is the script working? – Kusalananda Aug 22 '18 at 14:43
  • 2
    Your syntax is fine. Spend more time with bash and you'll be great. I want to address something else though, are you aware of the ServerAliveInterval option to ssh? It makes it ping the server every so often, and depending on the type of network failure may be helpful. (You would use something like sshfs -o ssh_command "ssh -o ServerAliveInterval=30" ... (I think) to use this.) Beyond this, sshfs also has -o reconnect to automatically reconnect, this may be useful as well. – i336_ Aug 22 '18 at 14:54
  • I have had the second else statement live for only 24 hours, most likely the connection to the remote mount will not break for months (it's a pretty rare occurence) and I don't want to "test" breaking it because it takes an important website offline. I thought the syntax might already be OK, so if you thinks so too, that's good enough for me! Thanks so much! – P-Didz Aug 22 '18 at 14:55

Your syntax is fine.

The syntax for an if/then/else statement includes key words: if, then, else, and fi (let's not forget elif). The commands after the then, else, and elif statements are allowed to be a list of commands (one or more commands).

  • The part after if can also be a list of commands. if read x; [ "$x" = "" ]; then echo "empty string"; fi is fine. It's probably more useful with while, though. – ilkkachu Aug 22 '18 at 17:02

Looks OK to me, although I would have written it something like:



if [ ! -f "$check_file" ]; then 
    /usr/sbin/sendmail -t alert-email@foo.com </etc/alert.txt
    /bin/sh /etc/fix_mount.sh

I see no reason for the first echo statement since you are directing it to /dev/null.

And it makes more sense to act if the file is not present. Hence the logical not ! operator.

Also, I would avoid using file as a variable name since it is a GNU/Linux program.

However, as your script stands, it should work.

Not the answer you're looking for? Browse other questions tagged or ask your own question.