grep -E -v '^(halt|sync|shutdown)' /etc/passwd | awk -F: '($7 != "'"$(which
nologin)"'" && $7 != "/bin/false") { print $1 " " $6 }' | while read -r user
dir; do
if [ ! -d "$dir" ]; then
echo "Fail: see below"
echo "The home directory ($dir) of user $user does not exist."
else
echo "pass"
fi
done
我该如何修复我的输出,以便第一个 if echo"Fail: see below" 仅打印一次,而第二个 if echo"Fail: see below" 打印任何不存在的用户。我的 else 语句也只是打印自己,我该如何停止它
答案1
我为此苦苦挣扎,因为我以前从未以使用内壳的方式编程过(这会破坏我需要的输出)。我想出了下面的方法,虽然效率稍低,但我认为在绝大多数情况下不会产生明显差异。此外,这假设主目录中没有空格:
#! /bin/bash
failcount=0
for each in `grep -E -v '^(async|shutdown)' /etc/passwd | awk -F: '($7 != "'"$(which nologin)"'" && $7 != "/bin/false") { print $1 ":" $6 }'`
do
user=`echo $each | cut -f1 -d":"`
dir=`echo $each | cut -f2 -d":"`
if [ ! -d "$dir" ]; then
if [ $failcount -eq 0 ]
then
echo "Fail: see below"
fi
echo "The home directory ($dir) of user $user does not exist."
failcount=$(( $failcount + 1 ))
fi
done
if [ $failcount -eq 0 ]
then
echo "Pass"
fi