Centos 运行具有多个允许值的 if - then 命令

Centos 运行具有多个允许值的 if - then 命令

有没有办法在脚本中运行可以匹配多个值的 if 命令,例如,我会将脚本推送到一组服务器,并且每个服务器仅当服务器主机名是命令或列表中输入的值之一时才会运行该命令。

我尝试过这样的事情,但显然没有起作用。

#!/bin/bash
if [ $HOSTNAME = server1.domain.com, server2.domain.com, server3.domain.com, server4.domain.com ]
then
        /home/user/update_1
else
        /home/user/update_2
fi

如果我可以让它检查一个包含服务器列表的文件,那就更好了

答案1

您需要使用 || 或语法来实现此目的

if [ "$HOSTNAME" == 'server1.domain.com' ] || [ "$HOSTNAME == 'server2.domain.com'"]
then
    do something
else
    dont' do something
fi

如果您想检查文件列表,那么您可以执行以下操作:

echo -e "computer1\ncomputer2\nmycomputer" > computerfile
hostname=mycomputer
if grep "$hostname" computerfile > /dev/null
then
    echo true
else
    echo false
fi

其中 hostname 是包含计算机名称的变量的名称,computerfile是计算机列表(每行 1 个名称)。

相关内容