Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何摆脱“创造”=0“;当运行shell脚本时,是否创建文件?_Shell - Fatal编程技术网

如何摆脱“创造”=0“;当运行shell脚本时,是否创建文件?

如何摆脱“创造”=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 [

我有一个shell脚本,它询问用户当前工作目录中有多少个文件

我已经在桌面位置创建了该脚本,当我运行该脚本时,它工作正常,但当我复制并粘贴到任何其他文件夹中时,如果我尝试运行,它正在创建一个名为“=0”的文件

我也发布了脚本,有人能帮我解决这个问题吗

#!/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