我在bash脚本中遇到命令not found错误

时间:2015-01-18 21:17:24

标签: bash

我正在编写的这个脚本通过目录查看并分别计算常规文件和目录的数量。我的代码是这样的:

#!/bin/bash
#countf.sh
#this file counts the number of files and directories in a path recursively
#Variables
declare -i filecount="0"
declare -i dircount="0"
for file in /*
do
if [ -f $file ]
then
$((filecount++))
elif [ -d $file ]
then
$((dircount++))
fi
done
echo The number of files is "$filecount"
echo The number of directories is "$dircount"
echo $?

我得到的输出是:

./countf.sh: line 14: 0: command not found
./countf.sh: line 14: 1: command not found
./countf.sh: line 14: 2: command not found
./countf.sh: line 14: 3: command not found
./countf.sh: line 14: 4: command not found
./countf.sh: line 14: 5: command not found
./countf.sh: line 11: 0: command not found
./countf.sh: line 11: 1: command not found
./countf.sh: line 14: 6: command not found
./countf.sh: line 14: 7: command not found
./countf.sh: line 14: 8: command not found
./countf.sh: line 14: 9: command not found
./countf.sh: line 14: 10: command not found
./countf.sh: line 14: 11: command not found
./countf.sh: line 14: 12: command not found
./countf.sh: line 14: 13: command not found
./countf.sh: line 14: 14: command not found
./countf.sh: line 14: 15: command not found
./countf.sh: line 14: 16: command not found
./countf.sh: line 14: 17: command not found
./countf.sh: line 14: 18: command not found
./countf.sh: line 14: 19: command not found
./countf.sh: line 14: 20: command not found
./countf.sh: line 14: 21: command not found
./countf.sh: line 11: 2: command not found
./countf.sh: line 11: 3: command not found
The number of files is 4
The number of directories is 22
0

脚本似乎工作正常减去在filecount或dircount递增后出现的命令not found错误代码。

1 个答案:

答案 0 :(得分:1)

在第14行,替换

$((dircount++))

通过

((dircount++))

检查http://wiki.bash-hackers.org/syntax/arith_expr

相关问题