【发布时间】:2023-03-12 10:12:02
【问题描述】:
我正在编写一个 shell 脚本,通过用户输入读取给定目录中的所有文件,然后计算有多少具有该扩展名的文件。我刚开始学习 Bash,我不确定为什么它没有找到文件或读取目录。我只举了 2 个例子,但我的计数总是 0。
这就是我运行脚本的方式
$./check_ext.sh /home/user/temp
我的脚本 check_ext.sh
#!/bin/bash
count1=0
count2=0
for file in "ls $1"
do
if [[ $file == *.sh ]]; then
echo "is a txt file"
(( count1++ ))
elif [[ $file == *.mp3 ]]; then
echo "is a mp3 file"
(( count2++ ))
fi
done;
echo $count $count2
【问题讨论】:
-
如果你只是想解决一个问题
find可以为你做这件事。如果您正在尝试学习 bash 编程,请按好先生。