Bash 脚本:如何显示密码每 2 周到期一次的输出

Bash Scripting: How to display output for the passwords expiry every 2 week

我的问题是,如何编辑脚本使得 如果 PASS_MAX_DAYS 等于或小于 14 天则等于 "Vulnerability: No"

Output

我的脚本

#!/bin/bash

passwordexpiry=`grep "^PASS_MAX_DAYS" /etc/login.defs`

if [[ $(passwordexpiry) == "PASS_MAX_DAYS    99999" ]]
then
      isVulnerable="Yes"
else 
      isVulnerable="No"
fi
  echo "Audit criteria: The passowrds expires every 2 weeks"
  echo "Vulnerability: ${isVulnerable}"
  echo "Details: See below"
  echo
  echo "Command:"
  echo "grep "^PASS_MAX_DAYS" /etc/login.defs"
  echo
  echo "Output:"
  echo ${passwordexpiry}
  echo

您可以使用grep -oP "^PASS_MAX_DAYS\s+\K([0-9]+)" /etc/login.defs提取值:

#!/bin/bash

max=14
passwordexpiry=`grep -oP "^PASS_MAX_DAYS\s+\K([0-9]+)" /etc/login.defs`

if [ "$passwordexpiry" -le "$max" ]
then
      isVulnerable="No"
else 
      isVulnerable="Yes"
fi

echo "$isVulnerable"

\K 从值 ([0-9]+)

的位置开始匹配