有一个功能search
search(){
echo "$1"
# If directory -> recursion
# If file and regex ok -> echo
for file in "$1"/*; do
([[ -d "$file" ]] && search "$file" $2 ||
if [[ ( -f "$file" ) && ( "$file" =~ $2 ) ]];
then
echo "$file"
fi)
done
}
如果你从命令行参数调用它:search "$1" "$2"
./s.sh . *
,那么输出将是这样的:
> ./s.sh . *
.
./a
./aaa.mp3
./a_directory
./a_directory/aaa.aaa
./a_directory/another_file
./a_directory/b
./alala.txt
./alphabet.txt
./a.sh
但是如果直接通过设置参数调用search "." "*"
,那么输出会是:
> ./s.sh . *
.
./a_directory
./a_directory/aaa.aaa
./a_directory/another_file
./a_directory/b
可能是什么问题呢?