0

我正在编写一个脚本,该脚本将从文件夹中获取用户列表,并将它们与 /home/ 中的文件夹列表进行比较,并输出与服务器上的活动用户列表不匹配的任何文件夹。现在,我几乎把它放在了我想要的地方,但我很难理解如何将我想要过滤掉的东西放在一个数组中,将它与 $COMPARE 的输出进行比较,然后只输出文件夹与我要过滤的内容不匹配。

到目前为止,这是我的代码:

# Script for finding and displaying home folders that don't currently belong to an active user on the server
HOMEOUT=$(mktemp)
USEROUT=$(mktemp)
USERS=( $(find /var/cpanel/users/ -maxdepth 1 -type f -printf '%P\n' | sort >$USEROUT) )
HOME=( $(find /home* -maxdepth 1 -type d -printf '%P\n' | grep -v '^$' | sort >$HOMEOUT) )
FILTER=( 'mysql' 'migrate-sslrigs' 'rvadmin' 'cpeasyapache' 'lost+found' 'cpanelbranding' 'installd'
    'cprestore' 'cprubybuild' 'cprubygemsbuild' 'MySQL-install' 'mysql_upgrade' 'securervsite' 'virtfs' 'zabbix' )


# Compare the two files and only output folders from /home/ which don't match the active_users list
COMPARE=`comm -23 $HOMEOUT $USEROUT`

我在想“案例”可能是这里的方法,但我在解决这个问题时遇到了问题,我不确定这是否是处理这个问题的最佳方法。任何指导/帮助将不胜感激。

更新

这是当前代码:

HOMEOUT=$(mktemp)
USEROUT=$(mktemp)
USERS=( $(find /var/cpanel/users/ -maxdepth 1 -type f -printf '%P\n' | sort >$USEROUT) )
HOME=( $(find /home* -maxdepth 1 -type d -printf '%P\n' | grep -v '^$' | sort >$HOMEOUT) )
FILTER=( 'mysql' 'migrate-sslrigs' 'rvadmin' 'cpeasyapache' 'lost+found' 'cpanelbranding' 'installd'
        'cprestore' 'cprubybuild' 'cprubygemsbuild' 'MySQL-install' 'mysql_upgrade' 'securervsite' 'virtfs' 'zabbix' 'cpeasyapache'
        '.cpan' '.cpcpan' '.cpanm' )


# Compare the two files and only output folders from /home/ which don't match the active_users list
COMPARE=`comm -23 "$HOMEOUT" "$USEROUT" |
            comm -23 - <(
                for f in "${FILTER[@]}"; do
                    echo "$f"
                done)`

# Find the disk space of each folder
 pushd /home/ >/dev/null
for x in "$COMPARE"; do
        du -s $x | sort -n | cut -f 2-|xargs -i du -sh {}
done
  popd >/dev/null


# House cleaning
rm -f $HOMEOUT $USEROUT

当我使用 bash -x 运行脚本时,它显示以下内容:

+ FILTER=('mysql' 'migrate-sslrigs' 'rvadmin' 'cpeasyapache' 'lost+found' 'cpanelbranding' 'installd' 'cprestore' 'cprubybuild' 'cprubygemsbuild' 'MySQL-install' 'mysql_upgrade' 'securervsite' 'virtfs' 'zabbix' 'cpeasyapache' '.cpan' '.cpcpan' '.cpanm')
++ comm -23 /tmp/tmp.fxYQB27380 /tmp/tmp.vwLWZ27381
++ comm -23 - /dev/fd/63
+++ for f in '"${FILTER[@]}"'
+++ echo mysql
+++ for f in '"${FILTER[@]}"'
+++ echo migrate-sslrigs
+++ for f in '"${FILTER[@]}"'
+++ echo rvadmin
+++ for f in '"${FILTER[@]}"'
+++ echo cpeasyapache
+++ for f in '"${FILTER[@]}"'
+++ echo lost+found
+++ for f in '"${FILTER[@]}"'
+++ echo cpanelbranding
+++ for f in '"${FILTER[@]}"'
+++ echo installd
+++ for f in '"${FILTER[@]}"'
+++ echo cprestore
+++ for f in '"${FILTER[@]}"'
+++ echo cprubybuild
+++ for f in '"${FILTER[@]}"'
+++ echo cprubygemsbuild
+++ for f in '"${FILTER[@]}"'
+++ echo MySQL-install
+++ for f in '"${FILTER[@]}"'
+++ echo mysql_upgrade
+++ for f in '"${FILTER[@]}"'
+++ echo securervsite
+++ for f in '"${FILTER[@]}"'
+++ echo virtfs
+++ for f in '"${FILTER[@]}"'
+++ echo zabbix
+++ for f in '"${FILTER[@]}"'
+++ echo cpeasyapache
+++ for f in '"${FILTER[@]}"'
+++ echo .cpan
+++ for f in '"${FILTER[@]}"'
+++ echo .cpcpan
+++ for f in '"${FILTER[@]}"'
+++ echo .cpanm

但是,输出仍然显示任何应该被过滤掉的文件夹。我觉得 echo 本身是不够的,并且可能缺少一些东西来实际从输出中删除与 $FILTER 匹配的任何内容。

想法?

4

1 回答 1

2

你可以用这个替换你的最后一行:

COMPARE=`comm -23 "$HOMEOUT" "$USEROUT" |
            comm -23 - <(
                for f in "${FILTER[@]}"; do
                    echo "$f"
                done | sort)`

这部分:

<(for f in "${FILTER[@]}"; do
    echo "$f"
  done | sort)

被 bash 替换为连接到脚本输出的管道读取端的文件名,后者每行<( )打印一个数组的排序成员。FILTER

这个命令:

comm -23 - <(
    for f in "${FILTER[@]}"; do
        echo "$f"
    done | sort)`

从其输入中输出未出现在FILTER数组中的行。

于 2013-05-30T19:13:33.527 回答