我正在编写一个脚本,它将从文件夹中获取用户列表,并将它们与/ 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
但是,输出仍会显示任何应该被过滤掉的所述文件夹。我觉得回声本身是不够的,实际上从输出中删除任何与$ FILTER匹配的内容可能会丢失。
思想?
答案 0 :(得分: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
数组中。