gpt4 book ai didi

bash - 如果文件存在,如何在脚本中继续?

转载 作者:行者123 更新时间:2023-11-29 09:44:14 24 4
gpt4 key购买 nike

如果文件存在,如何在脚本中继续?

#!/bin/bash
echo "Start"
# waiting to be exist file
echo "file already exists, continuing"

最佳答案

如果 sleep X 则执行 while,这样它将每 X 秒检查一次文件是否存在。

当文件存在时,while 将结束,您将继续 echo "file already exists, continuiing"

#!/bin/bash
echo "Start"
### waiting to be exist file
while [ ! -f "/your/file" ]; # true if /your/file does not exist
do
sleep 1
done
echo "file already exists, continuing"

And goes instead of checking the file existence check if the script has already completed the background?

根据您发布的代码,我做了一些更改以使其完全正常工作:

#!/bin/bash
(
sleep 5
) &

PID=$!
echo "the pid is $PID"

while [ ! -z "$(ps -ef | awk -v p=$PID '$2==p')" ]
do
echo "still running"
sleep 1
done

echo "done"

关于bash - 如果文件存在,如何在脚本中继续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18893283/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com