Skip to content

Instantly share code, notes, and snippets.

@Ethorbit
Last active November 20, 2022 14:53
Show Gist options
  • Save Ethorbit/5221816bfbae13ec26cace0660db74df to your computer and use it in GitHub Desktop.
Save Ethorbit/5221816bfbae13ec26cace0660db74df to your computer and use it in GitHub Desktop.
Haults execution until the specified mount is valid or the specified wait duration has been reached.
#!/bin/bash
MAX_WAIT="10"
[[ -z "$1" ]] && exit 1 || MOUNT="$1"
if [[ ! -z "$2" ]]; then
reg="^[0-9]+$" && [[ ! "$2" =~ $reg ]] && exit 1
MAX_WAIT="$2"
fi
seconds_waited=0 # not necessary, but prevents an env var from conflicting
while [[ ! $(findmnt "$MOUNT") ]]; do
seconds_waited=$(($seconds_waited + 1))
[[ "$seconds_waited" -ge "$MAX_WAIT" ]] && exit 1 || sleep 1
done
echo 1 && exit 0
@Ethorbit
Copy link
Author

Ethorbit commented Nov 20, 2022

Usage in a script:

if [[ $(mount-wait.sh "/mnt/someplace" 5) ]]; then 
      echo "Mount found!"
else
      echo "Never found a mount."
fi 

This example will wait a maximum of 5 seconds for something to be mounted on /mnt/someplace before giving up.
If it gives up, it says "Never found a mount.", otherwise "Mount found!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment