gpt4 book ai didi

linux - 仅当两个值都分配给 bash 中的两个变量时,如何连接两个变量?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:07 24 4
gpt4 key购买 nike

只有当两个值都分配给两个变量时,如何连接两个变量?

1. Assign $SERVER to $NODELIST if only $SERVER variable is set
2. Assign $NODE to $NODELIST if only $NODE variable is set
3. Concatenate $SERVER and $NODE [comma separated ]to $NODELIST if both $SERVER and $NODE is set

#!/bin/bash
SERVER='sn01,sn02'
NODE='cn01,cn02'

if [[ -z $SERVER ]] || [[ -z $NODE ]] ; then
NODE_LIST=$SERVER,$NODE
echo "$NODE_LIST"
elif [[ -z $SERVER ]] || [[ -z $NODE ]] ; then
NODE_LIST=$SERVER
echo "$NODE_LIST"
elif [[ -z $NODE ]] || [[ -z $SERVER ]] ; then
NODE_LIST=$NODE
echo "NODE_LIST"
else
echo "none of the variables specified"
fi

但无法根据要求分配“NODE_LIST”变量。

最佳答案

有点像,

$ [[ -z "$SERVER" || -z "$NODE" ]] && NODELIST="$SERVER$NODE"

关于linux - 仅当两个值都分配给 bash 中的两个变量时,如何连接两个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46403306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com