Сценарий не работает

Моя программа, предполагают для распечатывания файлов, которые читаемы и перезаписываемы, распечатывают сумму файлов (которые читаемы и перезаписываемы), распечатайте общую сумму файлов и наконец перечислите все файлы в текущем каталоге в формате длинного списка (отсортированный по времени изменения (новейший первый)). Но я столкнулся с некоторыми проблемами, и я не могу выяснить то, что отсутствует/что не так в моем коде.

#!/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
задан 6 September 2016 в 18:08

2 ответа

В сценарии существует несколько проблем. Я думаю так исправленный, должен работать..

#!/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
1
ответ дан 7 December 2019 в 13:51

Решение @LilloX должно работать, но я рекомендовал бы использовать find для задания:

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

Это возвращает количество файлов с чтением и полномочиями записи для владельца файла в текущем каталоге. Если Вы удаляете | wc -l из команды, она возвращает сами файлы.
Видят man find для получения дополнительной информации о том, как использовать find.

<час>

Редактирование:

Это - выборка от man find. Как Вы видите, существует много возможностей определить полномочия в поиске. Если Вы хотите соответствовать файлам, которые читаются - и writeable любым и также включают файлы, которые имеют дополнительные (executeable) полномочия, можно использовать 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).
1
ответ дан 7 December 2019 в 13:51

Другие вопросы по тегам:

Похожие вопросы: