数组满足条件时退出循环

时间:2020-09-05 01:06:53

标签: bash shell

我正在编写脚本。

  • 脚本在多台服务器上运行。
  • 它将电子邮件发送到“收件人”字段中提到的地址。
  • 邮件的两个收件人(user1@domain.com或user2@domain.com)可以是其中之一,具体取决于执行脚本的服务器。
  • 有4个预定义服务器的列表。如果脚本在这些服务器之一上执行,它将电子邮件发送到user1@domain.com。否则,它将电子邮件发送到user2@domain.com。

我认为这是正确的,但是我错过了一个逻辑,即在每台服务器上都将检查if和else条件。

#!/bin/bash
set -x
SERVER_NAME=$(hostname -s)
FILE_TEMP=/tmp/new.log
echo -e "This is a test from SERVER_NAME" >> $FILE_TEMP
subject="sending file from $SERVER_NAME"

Servers=(flipunix1 flipunix2 flipunix3 flipunix7)
for i in ${Servers[*]}
do
    if [ "$SERVER_NAME" == "$i" ];
    then
        to="user1@domain.com"
        echo -e "server name picked is $SERVER_NAME and i value is $i "
        break
    else
        to="user2@domain.com"
        echo -e "server name picked is $SERVER_NAME and i value is $i "
        break
    fi
done

also_to="user3@domain.com"
mail -s "$subject" "$to" "$also_to" < $FILE_TEMP

2 个答案:

答案 0 :(得分:1)

如果to="yugnesh@samil.com"SERVER_NAME数组中的一个,则似乎要发送Servers,否则,您想发送to="sanket@samil.com"。如果是这样,则循环逻辑将设置to="sanket@samil.com"并在每次SERVER_NAME不匹配时中断循环-即使SERVER_NAME在下一次迭代中可能匹配。您可以通过删除break来解决此问题,例如

for i in ${Servers[*]}
do
    if [ "$SERVER_NAME" = "$i" ];
    then
        to="yugnesh@samil.com"
        echo -e "server name picked is $SERVER_NAME and i value is $i "
        break
    else
        to="sanket@samil.com"
    fi
done

现在每次设置to="sanket@samil.com",但是如果在SERVER_NAME中找到名称,则循环以to="yugnesh@samil.com"退出。仅当没有匹配项时,发送方才保留to="sanket@samil.com"

您只需在循环之前设置to="sanket@samil.com"并执行以下操作:

#!/bin/bash
set -x
SERVER_NAME=$(hostname -s)
FILE_TEMP=/tmp/new.log
echo -e "This is a test from SERVER_NAME" >> $FILE_TEMP
subject="sending file from $SERVER_NAME"
to="sanket@samil.com"

Servers=(flipunix1 flipunix2 flipunix3 flipunix7)
for i in ${Servers[*]}
do
    if [ "$SERVER_NAME" = "$i" ];
    then
        to="yugnesh@samil.com"
        echo -e "server name picked is $SERVER_NAME and i value is $i "
        break
    fi
done

also_to="flipteam@samil.com"
mail -s "$subject" "$to" "$also_to" < $FILE_TEMP

那将完成同样的事情。 (如果您想输出已使用的消息,则可以添加if ... fi并选中if [ "$to" = "sanket@samil.com" ]。)

您还可以完全消除循环,只需将[[ ... ]]与数组内容和SERVER_NAME一起使用,例如

#!/bin/bash
set -x
SERVER_NAME=$(hostname -s)
FILE_TEMP=/tmp/new.log
echo -e "This is a test from SERVER_NAME" >> $FILE_TEMP
subject="sending file from $SERVER_NAME"
to="sanket@samil.com"

Servers=(flipunix1 flipunix2 flipunix3 flipunix7)

[[ ${Servers[@]} =~ $SERVER_NAME ]] && {
    to="yugnesh@samil.com"
    echo -e "server name picked is $SERVER_NAME "
}

also_to="flipteam@samil.com"
mail -s "$subject" "$to" "$also_to" < $FILE_TEMP

(有关检查if [ "$to" = "sanket@samil.com" ]的注意事项,如果您想根据所选内容输出内容)

让我知道我是否理解不正确。

注意:只有一个=用于检查[...]中的字符串是否相等)

答案 1 :(得分:0)

您可以尝试以下方法:

#!/bin/bash
set -x
SERVER_NAME=$(hostname -s)
FILE_TEMP=/tmp/new.log
echo -e "This is a test from SERVER_NAME" >> $FILE_TEMP
subject="sending file from $SERVER_NAME"

Servers=(flipunix1 flipunix2 flipunix3 flipunix7)
for i in ${Servers[*]}
do
if [ "$SERVER_NAME" == "$i" ];
then
to="yugnesh@samil.com"
echo -e "server name picked is $SERVER_NAME and i value is $i "
break
fi
done

if [ -z "$to" ];
then
to="sanket@samil.com"
echo -e "server name picked is $SERVER_NAME "
fi

also_to="flipteam@samil.com"
mail -s "$subject" "$to" "$also_to" < $FILE_TEMP

或(较短的版本)

#!/bin/bash
set -x
SERVER_NAME=$(hostname -s)
FILE_TEMP=/tmp/new.log
echo -e "This is a test from SERVER_NAME" >> $FILE_TEMP
subject="sending file from $SERVER_NAME"

Servers=(flipunix1 flipunix2 flipunix3 flipunix7)

if ! printf "%s\n" "${Servers[@]}" | grep -qw -m 1 "$SERVER_NAME";
then
to="sanket@samil.com"
else
to="yugnesh@samil.com"
fi

also_to="flipteam@samil.com"
mail -s "$subject" "$to" "$also_to" < $FILE_TEMP