脚本不起作用

脚本不起作用

我的程序应该打印出可读写的文件、打印出文件数量(可读写的)、打印出文件总数,最后以长列表格式列出当前目录中的所有文件(按修改时间排序(最新文件在前))。但我遇到了一些问题,我无法弄清楚我的代码中缺少什么/出了什么问题。

#!/bin/bash
RWFILE= 0
ALL= 0
FILE= `ls`
for f in $FILE
do
if[[ -r $FILE && -w $FILE ]]
then
    RWFILE= RWFILE + 1
    echo possible readable & writable files: $FILE
fi
ALL= ALL +1
echo Number of possible readable and writable files in current directory: $RWFILE
echo Number of files in current directory $ALL
echo list of files in this directory by modification time: ls -l
done

每次运行程序时都会出现几个错误,

  1. line 3: 0: command not found

  2. line 4: 0: command not found

  3. line 11: syntax error near unexpected token 'then'

  4. line 11: 'then'

谁能告诉我哪里出了问题?

答案1

剧本中有几个问题。我认为修改一下应该可以。

#!/bin/bash
RWFILE="0"
ALL="0"
for f in *
do
if [[ -r $f && -w $f ]] ;
then
    RWFILE=$((RWFILE+1))
    echo "possible readable & writable files: $RWFILE"
fi
ALL=$((ALL+1))
echo "Number of possible readable and writable files in current directory: $RWFILE"
echo "Number of files in current directory $ALL"
echo "list of files in this directory by modification time: ls -l"
done

答案2

@LilloX 的解决方案应该可行,但我建议使用find

find . -maxdepth 1 -perm 644 | wc -l

这将返回当前目录中文件所有者具有读写权限的文件数。如果您| wc -l从命令中删除,它将返回文件本身。有关如何使用的更多信息,
请参阅。man findfind


编辑:

这是摘录自man find。如您所见,有很多种方法可以定义搜索中的权限。如果您想要匹配任何人都可读写的文件,并且还包含具有其他权限(可执行)的文件,则可以使用find -perm /u+w,g+w,a+w

 find . -perm 664

    Search for files which have read and write permission for their owner, and group, but which other users can read but not  write  to.   Files  which
    meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched.

    find . -perm -664

    Search  for files which have read and write permission for their owner and group, and which other users can read, without regard to the presence of
    any extra permission bits (for example the executable bit).  This will match a file which has mode 0777, for example.

    find . -perm /222

    Search for files which are writable by somebody (their owner, or their group, or anybody else).    

    find . -perm /220
    find . -perm /u+w,g+w
    find . -perm /u=w,g=w

    All three of these commands do the same thing, but the first one uses the octal representation of the file mode, and the other two use the symbolic
    form. These  commands  all search for files which are writable by either their owner or their group.  The files don't have to be writable by both
    the owner and group to be matched; either will do.

    find . -perm -220
    find . -perm -g+w,u+w

    Both these commands do the same thing; search for files which are writable by both their owner and their group.

    find . -perm -444 -perm /222 ! -perm /111
    find . -perm -a+r -perm /a+w ! -perm /a+x

    These two commands both search for files that are readable for everybody ( -perm -444 or -perm -a+r), have at least one write bit set ( -perm  /222
    or -perm /a+w) but are not executable for anybody ( ! -perm /111 and ! -perm /a+x respectively).

相关内容