bash 脚本:嵌套命令的结果未分配给变量
bash script: result of nested commands result not assigning to variable
具有 pattern:st2-api-alpha
的字符串
想 trim 他们喜欢 st2-api
基本上想删除每个字符串中“-”上第二次出现的文本。
以下是我的陈述:
for (( i=1; i<${#all_containers_list[@]}; i++ )){
echo "trimmed[$i] - "${all_containers_list[i]}|cut -f1-3 -d'-'
temp="$(${all_containers_list[i]}| cut -f1-3 -d'-')"
echo "temp is:$temp"
all_containers_list[i]=$temp
echo "all_containers_list[$i] is ${all_containers_list[i]}"
}
当我回应时:
trimmed[1] - st2-ui
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-ui-server-alpha: command not found
温度是:
all_containers_list[1] is
trimmed[2] - st2-api
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-api-alpha: command not found
温度是:
all_containers_list[2] is
trimmed[3] - st2-whiteboard
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-whiteboard-alpha: command not found
温度是:
all_containers_list[3] 是
trimmed[4] - st2-aspose
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-aspose-alpha: command not found
温度是:
all_containers_list[4] is
trimmed[5] - determined_bassi
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: determined_bassi: command not found
温度是:
all_containers_list[5] is
trimmed[6] - st2-ui
/home//st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-ui--alpha: command not found
温度是:
all_containers_list[6] 是
echo "trimmed[$i] - "${all_containers_list[i]}|cut -f1-3 -d'-'
--- 这工作正常。
但是下面的分配不起作用
temp=$(${all_containers_list[i]}| cut -f1-3 -d'-')
请求您的帮助来解决这个问题!
OS 是 CentOS , bash script
我认为调用子进程只是为了删除字符串的一部分,有点矫枉过正。假设您已将字符串存储在变量中,比如
str=st2-api-alpha
你可以简单地做一个
if [[ $str =~ [^-]*-[^-]* ]]
then
str=${BASH_REMATCH[0]}
fi
如果字符串不包含破折号,则保持不变。您可以简单地将此解决方案推广到您拥有字符串数组的情况。
具有 pattern:st2-api-alpha
的字符串想 trim 他们喜欢 st2-api
基本上想删除每个字符串中“-”上第二次出现的文本。
以下是我的陈述:
for (( i=1; i<${#all_containers_list[@]}; i++ )){
echo "trimmed[$i] - "${all_containers_list[i]}|cut -f1-3 -d'-'
temp="$(${all_containers_list[i]}| cut -f1-3 -d'-')"
echo "temp is:$temp"
all_containers_list[i]=$temp
echo "all_containers_list[$i] is ${all_containers_list[i]}"
}
当我回应时:
trimmed[1] - st2-ui
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-ui-server-alpha: command not found
温度是:
all_containers_list[1] is
trimmed[2] - st2-api
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-api-alpha: command not found
温度是:
all_containers_list[2] is
trimmed[3] - st2-whiteboard
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-whiteboard-alpha: command not found
温度是:
all_containers_list[3] 是
trimmed[4] - st2-aspose
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-aspose-alpha: command not found
温度是:
all_containers_list[4] is
trimmed[5] - determined_bassi
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: determined_bassi: command not found
温度是:
all_containers_list[5] is
trimmed[6] - st2-ui
/home//st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-ui--alpha: command not found
温度是:
all_containers_list[6] 是
echo "trimmed[$i] - "${all_containers_list[i]}|cut -f1-3 -d'-'
--- 这工作正常。但是下面的分配不起作用
temp=$(${all_containers_list[i]}| cut -f1-3 -d'-')
请求您的帮助来解决这个问题! OS 是 CentOS , bash script
我认为调用子进程只是为了删除字符串的一部分,有点矫枉过正。假设您已将字符串存储在变量中,比如
str=st2-api-alpha
你可以简单地做一个
if [[ $str =~ [^-]*-[^-]* ]]
then
str=${BASH_REMATCH[0]}
fi
如果字符串不包含破折号,则保持不变。您可以简单地将此解决方案推广到您拥有字符串数组的情况。