在solaris中使用awk

时间:2014-09-02 08:18:43

标签: awk solaris

我正在使用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

1 个答案:

答案 0 :(得分:2)

PASSLENGTHPASSLENGTH=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