我正在使用Solaris 5.10,我想解析/etc/default/passwd
并检查最小密码长度。以下是我使用的脚本:
if awk -v x=1 '$1 == "PASSLENGTH" && $2 == "6" {x=0} END{exit x}' /etc/default/passwd
then
echo "Control Area: User Identity Management; Ensure that minimum password length is set ; compliant" >> /etc/chef/report.txt
else
echo "Control Area: User Identity Management; Ensure that minimum password length is set ; Non compliant" >> /etc/chef/report.txt
fi
如果最低PASSLENGTH
等于6
,我会在report.txt
文件中回复。但问题是我的代码不会进入if块,即使PASSLENGTH
变量的值等于6
中的/etc/default/passwd
。
答案 0 :(得分:2)
PASSLENGTH
以PASSLENGTH=N
格式撰写
Awk使用空格/制表符/换行符作为默认分隔符
这意味着您的$1
实际上是PASSLENGTH=6
,而$2
设置为无法解释if语句失败的原因。
尝试
if awk -F= -v x=1 '$1 == "PASSLENGTH" && $2 == "6" {x=0} END{exit x}' /etc/default/passwd
then
echo "Control Area: User Identity Management; Ensure that minimum password length is set ; compliant" >> /etc/chef/report.txt
else
echo "Control Area: User Identity Management; Ensure that minimum password length is set ; Non compliant" >> /etc/chef/report.txt
fi
您也可以更改awk,这样就不必在开始时声明变量
awk -F= '$1 == "PASSLENGTH" && $2 != "6" {x=1} END{exit x}
全部用awk,最短的我认为它可以
awk '/^PASSLENGTH=6/{x=1}
END {print "Control Area: User Identity Management; Ensure that minimum password length is set; " (x?"compliant":"non compliant") >> "/etc/chef/report.txt"}' /etc/default/passwd