以下のスクリプトは、ディレクトリに4つ以上のファイルが含まれている場合に単一のディレクトリの所有者を確認および変更するために使用されます。すべてのディレクトリを確認し、すべてのディレクトリに4つ以上のファイルが含まれており、所有者を変更できるように複数のディレクトリを追加する方法は?
#!/bin/bash
FILE=""
DIR="/home/ec2-user/test1"
dir_files=("$DIR"/*)
if [[ "${#dir_files[@]}" -gt 4 ]] ; then
#More than 4 files change owner satha
chown satha $DIR
elif [[ -e "${dir_files[0]}" ]] ; then
#change owner if DIR is below 4 files
chown lohith $DIR
else
#change owner if DIR is empty
chown lohith $DIR
fi
ベストアンサー1
#!/bin/zsh -
dirs=( $^argv(N-/) ) # those of the arguments that can be determined to
# be directories after symlink resolution
for dir ($dirs) {
files=( $dir/*(NDY5) ) # files in $dir, stopping at the 5th
case $#files {
(5) chown -- satha $dir;; # for > 4 files
(0) chown -- lohith $dir;; # for no file
(*) chown -- lohith $dir;; # anything else (1 to 4)
}
}