如果 bash 中没有满足条件则打印语句
print statement if no condition is met in bash
我有一个 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 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 语句,那么只会执行第一个判断密钥是否超过 90 天的 if 语句。
我怎样才能这样写:
- 如果第一个密钥超过 90 天并发送电子邮件。
- 如果第二个密钥超过 90 天,将发送一封电子邮件。
- 如果没有密钥超过 90 天,则会打印一条消息,说明没有密钥超过 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
如果我没理解错的话,你的逻辑是这样的:
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
如果是这样,将 condition1
替换为 [ "$key1dtSec" -lt "$taSec" ]
,condition2
也是如此,并在每个块中插入适当的操作。
我有一个 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 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 语句,那么只会执行第一个判断密钥是否超过 90 天的 if 语句。
我怎样才能这样写:
- 如果第一个密钥超过 90 天并发送电子邮件。
- 如果第二个密钥超过 90 天,将发送一封电子邮件。
- 如果没有密钥超过 90 天,则会打印一条消息,说明没有密钥超过 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
如果我没理解错的话,你的逻辑是这样的:
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
如果是这样,将 condition1
替换为 [ "$key1dtSec" -lt "$taSec" ]
,condition2
也是如此,并在每个块中插入适当的操作。