如何摆脱“创造”=0“;当运行shell脚本时,是否创建文件?
我有一个shell脚本,它询问用户当前工作目录中有多少个文件 我已经在桌面位置创建了该脚本,当我运行该脚本时,它工作正常,但当我复制并粘贴到任何其他文件夹中时,如果我尝试运行,它正在创建一个名为“=0”的文件 我也发布了脚本,有人能帮我解决这个问题吗如何摆脱“创造”=0“;当运行shell脚本时,是否创建文件?,shell,Shell,我有一个shell脚本,它询问用户当前工作目录中有多少个文件 我已经在桌面位置创建了该脚本,当我运行该脚本时,它工作正常,但当我复制并粘贴到任何其他文件夹中时,如果我尝试运行,它正在创建一个名为“=0”的文件 我也发布了脚本,有人能帮我解决这个问题吗 #!/bin/bash numfiles=$(ls | wc -l) try=1 read -p "Guess the number of files present in the directory: : " num_dir while [
#!/bin/bash
numfiles=$(ls | wc -l)
try=1
read -p "Guess the number of files present in the directory: : " num_dir
while [ $num_dir>=0 ]
do
if [ $try != 1 ]
then
read -p "Guess the number of files present in the directory: : " num_dir
fi
if [ $num_dir == $numfiles ]
then
exit
elif [ $num_dir -gt $numfiles ]
then
echo "num_dir is greater than numfiles"
else
echo "num_dir is less than numfiles"
fi
try=0
done
当[$num\u dir>=0]时,错误出现在
中。该条件将$num\u dir
的“输出”重定向到文件=0
你需要
while [ $num_dir -ge 0 ]
或者Bash特定的
while [[ $num_dir >= 0 ]]
test
没有=
操作符;它被解析为输出重定向,相当于
[ $num_dir > =0 ]
或
(在识别命令的参数之前,操作员会识别重定向)
要进行数值比较,请使用-ge
:
# Quote to avoid a syntax error if num_dir has an empty value
[ "$num_dir" -ge 0 ]
使用:
这样您就不会得到不需要的文件,用户输入也可以是0,这将结束循环,您可以摆脱try
try,而[[$num_dir>=0]
=
仅在(…)
或$(…)
中的算术表达式中工作;您仍然需要-ge
内部[…]
。
# Quote to avoid a syntax error if num_dir has an empty value
[ "$num_dir" -ge 0 ]
while true