Bash 使用-z时在后台缓存输出中测试命令,但使用=
我有一些奇怪的行为Bash 使用-z时在后台缓存输出中测试命令,但使用=,bash,Bash,我有一些奇怪的行为 rm /tmp/a touch /tmp/a int=1 while [ "aaaa" != "$(cat /tmp/a)" ]; do echo "loop"; sleep 1; int="$((int+1))" if [ "$int" == "10" ]; then echo "aaaa" > /tmp/a fi cat /tmp/a; done 此脚本将输出循环,然后在int为10时停止,但 rm /tmp/a touch /tmp/a int=1
rm /tmp/a
touch /tmp/a
int=1
while [ "aaaa" != "$(cat /tmp/a)" ]; do
echo "loop";
sleep 1;
int="$((int+1))"
if [ "$int" == "10" ]; then
echo "aaaa" > /tmp/a
fi
cat /tmp/a;
done
此脚本将输出循环
,然后在int
为10时停止,但
rm /tmp/a
touch /tmp/a
int=1
while [ -z "$(cat /tmp/a)" ]; do
echo "loop";
sleep 1;
int="$((int+1))"
if [ "$int" == "10" ]; then
echo "aaaa" > /tmp/a
fi
cat /tmp/a;
done
这将永远循环。bash版本:
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
GNUBash,版本4.2.46(1)-发行版(x86_64-redhat-linux-GNU)
版权所有(C)2011免费软件基金会。
许可证GPLv3+:GNU GPL版本3或更高版本
我想知道当我使用
-z
时,为什么命令的输出会被缓存。如果你能用一些链接/文档为我指出正确的方向,那就太棒了。谢谢为什么您试图从当前文件夹cat a访问文件a
代码>?不应该是cat/tmp/a
?问题是否只发生在/tmp
上?哪一个是/tmp filesystem可能是一些cron作业在编写时正在清理/tmp
文件夹中的文件,使其无法用于下一次迭代。因为逻辑(两者)在其他文件夹中工作,而不是使用[[-s$file]]检查它是否工作empty@NahuelFouilleul这不是一个/tmp
问题,如果我有一个不同的路径是相同的(当前目录等…),为什么您试图从当前文件夹cat a访问文件a
代码>?不应该是cat/tmp/a
?问题是否只发生在/tmp
上?哪一个是/tmp filesystem可能是一些cron作业在编写时正在清理/tmp
文件夹中的文件,使其无法用于下一次迭代。因为逻辑(两者)在其他文件夹中工作,而不是使用[[-s$file]]检查它是否工作empty@NahuelFouilleul这不是一个/tmp
问题,如果我有一个不同的路径是相同的(当前目录等…)