/ / bash - използване на рекурсия - броене на изпълнимите файлове в dir - bash, shell, recursion

bash - използване на рекурсия - броене на изпълнимите файлове в dir - bash, shell, recursion

Аз не търся някой високо ниво АСС, само с един ред, аз се опитвам да направя скрипт, който ще работи и да го накарам да работи с рекурсията и да го разбере :)

Опитвам се да науча башинг скриптове и да се сблъскам с някои проблеми:

Опитвам се да преброя броя на изпълнимите файлове в директория и в поддиректорите

това е това, за което си мислех

име на файла 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

Отговори:

1 за отговор № 1

Ако наистина искате да използвате рекурсия (което е aлоша идея в Баш): Първо, не се обадете на сценария рекурсивно.Вместо това, наречете рекурсивно функция.Това ще бъде по-ефективно (без фойерверх режийни) Опитвате се да поправите синтаксиса си:

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