我正在编写脚本。
我认为这是正确的,但是我错过了一个逻辑,即在每台服务器上都将检查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
答案 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