Last active
November 20, 2022 14:53
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage in a script:
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!"