【发布时间】:2017-01-08 16:18:22
【问题描述】:
我正在尝试查找运行 ls -al 的每一行输出是否是一个文件或
目录以及是否隐藏并计算每个目录的类型。
编辑:我必须不使用find。
#!/bin/bash
#declare four different regex statements that match files, hidden files, directories and hidden directories (excluding . and ..)
#based on the output of each line of running ls -al
re_file='^\-[rwx\-]{9}\s[0-9]+\s([a-z_][a-z0-9_]{0,30})\s([a-z_][a-z0-9_]{0,30})\s[0-9]+\s\w{3}\s[0-9]+\s[0-9]{2}:[0-9]{2}\s[^\.](\w|\.)*$'
re_hidden_file='^\-[rwx\-]{9}\s[0-9]+\s([a-z_][a-z0-9_]{0,30})\s([a-z_][a-z0-9_]{0,30})\s[0-9]+\s\w{3}\s[0-9]+\s[0-9]{2}:[0-9]{2}\s\.\w(\w|\.)*$'
re_directory='^d[rwx\-]{9}\s[0-9]+\s([a-z_][a-z0-9_]{0,30})\s([a-z_][a-z0-9_]{0,30})\s[0-9]+\s\w{3}\s[0-9]+\s[0-9]{2}:[0-9]{2}\s[^\.](\w|\.)*$'
re_hidden_directory='^d[rwx\-]{9}\s[0-9]+\s([a-z_][a-z0-9_]{0,30})\s([a-z_][a-z0-9_]{0,30})\s[0-9]+\s\w{3}\s[0-9]+\s[0-9]{2}:[0-9]{2}\s\.\w(\w|\.)*$'
#declare four different counters for each type
file_count=0
hidden_file_count=0
directory_count=0
hidden_directory_count=0
#read through the output of ls -al line by line, assigning x the value of each line
ls -al $1 | while read x; do
#test if each line matches each of the regex statements, if it does then increment the relevant counter
if [[ $x =~ $re_file ]] ; then
file_count+=1
elif [[ $x =~ $re_hidden_file ]] ; then
hidden_file_count+=1
elif [[ $x =~ $re_directory ]] ; then
directory_count+=1
elif [[ $x =~ $re_hidden_directory ]] ; then
hidden_directory_count+=1
else
echo "!!!"
fi
done
total=$((file_count + hidden_file_count + directory_count + hidden_directory_count))
echo "Files found: $file_count (plus $hidden_file_count hidden)"
echo "Directories found: $directory_count (plus $hidden_directory_count hidden)"
echo "Total files and directories: $total"
目前,脚本输出!!!,因为与ls -al 的每一行的任何正则表达式语句都不匹配,并且所有计数器变量都保留在0。这是一个输入示例(尽管 Bash 在完成正则表达式检查之前删除了用于填充的额外空格)。
drwx--x--x 37 username groupname 4096 Jan 8 14:37 .
drwxr-xr-x 235 root root 4096 Nov 15 12:16 ..
drwx------ 3 username groupname 4096 Oct 27 14:35 .adobe
-rw------- 1 username groupname 14458 Dec 5 20:24 .bash_history
-rw------- 1 username groupname 2680 Sep 30 16:12 .bash_profile
-rw------- 1 username groupname 1210 Oct 7 09:40 .bashrc
drwx------ 12 username groupname 4096 Dec 6 15:24 .cache
drwxr-xr-x 17 username groupname 4096 Jan 8 14:37 .config
drwx------ 4 username groupname 4096 Dec 5 17:51 dir1
drwx------ 2 username groupname 4096 Nov 23 12:26 dir2
...
我已经在online Regex checker 上测试了正则表达式,他们按照我的意愿进行评估。我认为这是一个特定于 Bash 的问题。任何帮助表示赞赏。
【问题讨论】:
-
对于这个简单的任务,你为什么不只使用 grep