如何使用for循环检查Linux中的装载点

如何使用for循环检查Linux中的装载点,linux,bash,Linux,Bash,我想写一个Bash脚本,检查是否存在挂载点。如果有,那么做“某事”,如果没有“睡眠5秒” 我想写一个for循环,这样如果它最初被装入,我就可以检查相同的条件,直到它为真 if mountpoint -q /foo/bar; then /etc/init.d/iptables else sleep 5 fi 如何编写for循环来检查挂载点,直到其存在?这里有一种方法: mnt_path=/mnt/ while ! mountpoint -q "$mnt_path"; do

我想写一个Bash脚本,检查是否存在挂载点。如果有,那么做“某事”,如果没有“睡眠5秒”

我想写一个for循环,这样如果它最初被装入,我就可以检查相同的条件,直到它为真

if mountpoint -q /foo/bar; then
   /etc/init.d/iptables
else 
   sleep 5
fi
如何编写for循环来检查挂载点,直到其存在?

这里有一种方法:

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

我建议引入一个超时。

如果您的目标是推迟启动iptables,直到挂载点存在,您可以执行以下操作:

while!挂载点-q/foo/bar;做
睡眠5
完成
/etc/init.d/iptables
循环条件是
mountpoint-q/foo/bar
的返回代码,对于不存在的装载,返回代码为
1
,对于现有装载,返回代码为
0
。循环将继续,直到
mountpoint
返回
0
(意味着挂载点现在存在),然后下一个启动iptables的命令将运行