Linux 中的条件文件逻辑
Conditional File Logic in Linux
我在下面写了一些代码来查找当前用户不可读的文件。它给我的消息是无法在父文件夹的子目录中读取文件,即使我在循环外的脚本底部明确测试了它报告为不可读的文件之一。但是,我检查过所有文件都设置了允许读取的组权限,并且我使用 vi 打开了其中的几个文件。这是怎么回事?
脚本:
#!/bin/ksh
set -A files $(ls -1 )
echo "${#files[@]}"
for((i=0; $i < ${#files[@]}; i++)); do
if [ ! ${files[$i]} ]; then
echo "${files[$i]} not "
fi
done
echo "============"
if [ ! -r ./tmp/feederseries.txt ]; then
echo "./tmp/feederseries.txt not readable"
fi
输出:
$ testfiles.sh "*" -r
212
./buildhist: not -r
20170109_124058.txt not -r
20170109_124128.txt not -r
./cmpatches: not -r
tmp.txt not -r
./reports: not -r
archived not -r
./tmp: not -r
feederseries.txt not -r
============
执行此操作不需要数组。使用 ls
遍历文件列表无论如何都不是最好的主意。有关说明,请参阅 http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29。
使用 ls -1 *
可以获得实际目录中的文件列表、子目录后跟“:”以及子目录中的文件。子目录中的文件被报告为不可读,因为它们不存在于实际目录(您进行测试的目录)中。
让 shell 负责扩展并遍历列表:
#!/bin/ksh
for file in ; do
if [ ! $file ] ; then
echo "$file not "
fi
done
如果您只需要不可读的文件,可以这样做:
find .. ! -perm /u=r -print
我在下面写了一些代码来查找当前用户不可读的文件。它给我的消息是无法在父文件夹的子目录中读取文件,即使我在循环外的脚本底部明确测试了它报告为不可读的文件之一。但是,我检查过所有文件都设置了允许读取的组权限,并且我使用 vi 打开了其中的几个文件。这是怎么回事?
脚本:
#!/bin/ksh
set -A files $(ls -1 )
echo "${#files[@]}"
for((i=0; $i < ${#files[@]}; i++)); do
if [ ! ${files[$i]} ]; then
echo "${files[$i]} not "
fi
done
echo "============"
if [ ! -r ./tmp/feederseries.txt ]; then
echo "./tmp/feederseries.txt not readable"
fi
输出:
$ testfiles.sh "*" -r
212
./buildhist: not -r
20170109_124058.txt not -r
20170109_124128.txt not -r
./cmpatches: not -r
tmp.txt not -r
./reports: not -r
archived not -r
./tmp: not -r
feederseries.txt not -r
============
执行此操作不需要数组。使用 ls
遍历文件列表无论如何都不是最好的主意。有关说明,请参阅 http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29。
使用 ls -1 *
可以获得实际目录中的文件列表、子目录后跟“:”以及子目录中的文件。子目录中的文件被报告为不可读,因为它们不存在于实际目录(您进行测试的目录)中。
让 shell 负责扩展并遍历列表:
#!/bin/ksh
for file in ; do
if [ ! $file ] ; then
echo "$file not "
fi
done
如果您只需要不可读的文件,可以这样做:
find .. ! -perm /u=r -print