Reputation: 1101
i am not looking for some high level ans, with just one line, i am trying to make a script that will work and to make it work with recursion and to understand it :)
I am trying to learn bash scripting, and geting into some problems:
I am trying to count the number of executable files in a dir and in its sub dirs
this it what i was thinking about
file name countFiles:
#!/bin/bash
counter = 0
for FILE in ($1/*) /// going over all the files in the input path
do
if (-d $FILE); then /// checking if dir
./countFiles $FILE /// calling the script again with ned path
counter= $($counter + $0) /// addint to the counter the number of exe files from FILE
fi
if (-f $FILE); then /// checking if file
if (-x $FILE); then /// checking id exe file
counter = $counter + 1 // adding counter by 1
fi
fi
done
exit($counter) // return the number for exe files
Upvotes: 1
Views: 143
Reputation: 46903
If you really want to use recursion (which is a bad idea in Bash): first, don't call your script recursively. Instead, call a function recursively. This will be more efficient (no forking overhead). Trying to fix your syntax:
#!/bin/bash
shopt -s nullglob
count_x_files() {
# Counts number of executable (by user, regular and non-hidden) files
# in directory given in first argument
# Return value in variable count_x_files_ret
# This is a recursive function and will fail miserably if there are
# too deeply nested directories
count_x_files_ret=0
local file counter=0
for file in "$1"/*; do
if [[ -d $file ]]; then
count_x_files "$file"
((counter+=count_x_files_ret))
elif [[ -f $file ]] && [[ -x $file ]]; then
((++counter))
fi
done
count_x_files_ret=$counter
}
count_x_files "$1"
echo "$count_x_files_ret"
Upvotes: 1