Pratik
Pratik

Reputation: 31

How do check the mountpoint in Linux with for loop

I want to write a Bash script, to check if a mountpoint exist. If it does then do 'something' if not 'sleep for 5 sec'.

I want to write a for loop, so that if it is mounted initially, I can check the same condition until its true

if mountpoint -q /foo/bar; then
   /etc/init.d/iptables
else 
   sleep 5
fi

How can I write the for loop to check the mountpoint until its exists?

Upvotes: 0

Views: 1344

Answers (2)

Perplexabot
Perplexabot

Reputation: 1989

Here is one way:

mnt_path=/mnt/
while ! mountpoint -q "$mnt_path"; do
    # mountpoint does not exist
    sleep 5
done
# while loop exited, meaning mount point now exists
cat /etc/init.d/iptables

I would recommend introducing a time-out.

Upvotes: 1

AfroThundr
AfroThundr

Reputation: 1225

If your objective is to hold off on starting iptables until the mountpoint exists, you could do the following:

while ! mountpoint -q /foo/bar; do
    sleep 5
done

/etc/init.d/iptables

The loop condition is the return code of mountpoint -q /foo/bar, which will be 1 for a nonexistent mount and 0 for an existing mount. The loop will continue until mountpoint returns 0 (meaning the mountpoint now exists), then the next command starting iptables will run.

Upvotes: 0

Related Questions