如果bash中不满足任何条件,则打印语句
我有一个bash脚本来确定某人的aws密钥是否超过90天。如果是,脚本会向他们发送一封电子邮件,其中包含有关如何旋转关键点的信息 如果没有超过90天的密钥,我希望脚本打印一条语句,这样就不会发送电子邮件 对于每个用户的密钥,我有两个独立的if语句:如果bash中不满足任何条件,则打印语句,bash,amazon-web-services,aws-iam,Bash,Amazon Web Services,Aws Iam,我有一个bash脚本来确定某人的aws密钥是否超过90天。如果是,脚本会向他们发送一封电子邮件,其中包含有关如何旋转关键点的信息 如果没有超过90天的密钥,我希望脚本打印一条语句,这样就不会发送电子邮件 对于每个用户的密钥,我有两个独立的if语句: if [ "$key1dtSec" -lt "$taSec" ]; then printf "%s created on %s\\nThis key is %s days old and needs to be replaced.\\nAn
if [ "$key1dtSec" -lt "$taSec" ]; then
printf "%s created on %s\\nThis key is %s days old and needs to be replaced.\\nAn email will be sent." "$user_access_key1" "$key1_date_created." "$key1AgeDays"
echo -e "$template1" | /usr/bin/mail --append="Content-type: text/html" -s "AWS Key Rotation Needed for $user_name in $aws_key" "$email_address"
echo; echo
fi
if [ "$key2dtSec" -lt "$taSec" ]; then
printf "%s created on %s.\\nThis key is %s days old and needs to be replaced.\\nAn email will be sent." "$user_access_key2" "$key2_date_created" "$key2AgeDays"
echo -e "$template2" | /usr/bin/mail --append="Content-type: text/html" -s "AWS Key Rotation Needed for $user_name in $aws_key" "$email_address"
echo; echo
fi
key1dtSec
和key2dtSec
是aws钥匙的使用时间(以秒为单位)taSec
设置为90天前
如果我使用一个elif
语句,并且对两个键只使用一个If/then语句,那么只会执行第一个If语句,该语句指示键是否超过90天
我如何写这封信,以便:
希望我正确地理解了你的问题。请检查下面的答案
key1=0
key2=0
if [ "$key1dtSec" -lt "$taSec" ]; then
printf "%s created on %s\\nThis key is %s days old and needs to be replaced.\\nAn email will be sent." "$user_access_key1" "$key1_date_created." "$key1AgeDays"
echo -e "$template1" | /usr/bin/mail --append="Content-type: text/html" -s "AWS Key Rotation Needed for $user_name in $aws_key" "$email_address"
echo; echo
key1=1
fi
if [ "$key2dtSec" -lt "$taSec" ]; then
printf "%s created on %s.\\nThis key is %s days old and needs to be replaced.\\nAn email will be sent." "$user_access_key2" "$key2_date_created" "$key2AgeDays"
echo -e "$template2" | /usr/bin/mail --append="Content-type: text/html" -s "AWS Key Rotation Needed for $user_name in $aws_key" "$email_address"
echo; echo
key2=1
fi
if [ "$key1" -eq 0 && "$key2" -eq 0]; then
echo "no key is older than 90 days."
fi
希望我正确地理解了你的问题。请检查下面的答案
key1=0
key2=0
if [ "$key1dtSec" -lt "$taSec" ]; then
printf "%s created on %s\\nThis key is %s days old and needs to be replaced.\\nAn email will be sent." "$user_access_key1" "$key1_date_created." "$key1AgeDays"
echo -e "$template1" | /usr/bin/mail --append="Content-type: text/html" -s "AWS Key Rotation Needed for $user_name in $aws_key" "$email_address"
echo; echo
key1=1
fi
if [ "$key2dtSec" -lt "$taSec" ]; then
printf "%s created on %s.\\nThis key is %s days old and needs to be replaced.\\nAn email will be sent." "$user_access_key2" "$key2_date_created" "$key2AgeDays"
echo -e "$template2" | /usr/bin/mail --append="Content-type: text/html" -s "AWS Key Rotation Needed for $user_name in $aws_key" "$email_address"
echo; echo
key2=1
fi
if [ "$key1" -eq 0 && "$key2" -eq 0]; then
echo "no key is older than 90 days."
fi
如果我理解正确,您的逻辑如下:
if condition1; then # first key
action1 # send email
fi
if condition2; then # second key
action2 # send different email
fi
if ! condition1 && ! condition2; then # neither key
action3 # print something
fi
如果是这样,请将
条件1
替换为[“$key1dtSec”-lt“$taSec”]
,与条件2
相同,并在每个块中插入适当的操作。如果我理解正确,您的逻辑如下:
if condition1; then # first key
action1 # send email
fi
if condition2; then # second key
action2 # send different email
fi
if ! condition1 && ! condition2; then # neither key
action3 # print something
fi
如果是这样,将
条件1
替换为[“$key1dtSec”-lt“$taSec”]
,与条件2
相同,并在每个块中插入适当的操作。-eq 0]
结尾处是一个语法错误,这会创建两个额外的全局变量,其中一个(或零,如我的回答)就足够了。@Tom Fenech,谢谢你的建议。请原谅语法错误-eq 0]
结尾是一个语法错误,这会创建两个额外的全局变量,其中一个(或零,如我的回答)就足够了。@Tom Fenech感谢您的建议。原谅语法错误