0

This script I'm writing looks through a directory and counts the number of regular files and directories respectively. My code is this:

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

The output I get is:

./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

The script appears to be working fine minus the command not found error code that appears after filecount or dircount is incremented.

1
  • 4
    Better indent your code when you need help or show your code to anyone... Commented Jan 18, 2015 at 21:19

1 Answer 1

1

On the line 14, replace

$((dircount++))

by

((dircount++))

Check http://wiki.bash-hackers.org/syntax/arith_expr

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.