简体   繁体   English

bash 脚本:嵌套命令的结果未分配给变量

[英]bash script: result of nested commands result not assigning to variable

Having string of pattern:st2-api-alpha具有模式字符串:st2-api-alpha

wanted to trim them like st2-api想像 st2-api 一样修剪它们

Basically wanted to remove the text followed by 2nd occurence on '-' for each string.基本上想删除文本,然后是每个字符串在“-”上的第二次出现。

Below are my statements:以下是我的陈述:

    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]}"
    }

when i echo:当我回声时:

trimmed[1] - st2-ui修剪 [1] - st2-ui

/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-ui-server-alpha: command not found /home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-ui-server-alpha: command not found

temp is:温度是:

all_containers_list[1] is all_containers_list[1] 是

trimmed[2] - st2-api修剪 [2] - st2-api

/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-api-alpha: command not found /home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-api-alpha: command not found

temp is:温度是:

all_containers_list[2] is all_containers_list[2] 是

trimmed[3] - st2-whiteboard修剪 [3] - st2-白板

/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-whiteboard-alpha: command not found /home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-whiteboard-alpha: command not found

temp is:温度是:

all_containers_list[3] is all_containers_list[3] 是

trimmed[4] - st2-aspose修剪 [4] - st2-aspose

/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-aspose-alpha: command not found /home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-aspose-alpha: command not found

temp is:温度是:

all_containers_list[4] is all_containers_list[4] 是

trimmed[5] - determined_bassi修剪 [5] -determined_bassi

/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: determined_bassi: command not found /home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh:第139行:determined_bassi:找不到命令

temp is:温度是:

all_containers_list[5] is all_containers_list[5] 是

trimmed[6] - st2-ui修剪 [6] - st2-ui

/home/ /st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-ui- -alpha: command not found /home/ /st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line 139: st2-ui- -alpha: command not found

temp is:温度是:

all_containers_list[6] is all_containers_list[6] 是

  1. echo "trimmed[$i] - "${all_containers_list[i]}|cut -f1-3 -d'-' --- This is working fine. echo "trimmed[$i] - "${all_containers_list[i]}|cut -f1-3 -d'-' --- 这工作正常。

  2. But the below assignment is not working temp=$(${all_containers_list[i]}| cut -f1-3 -d'-')但是下面的分配不起作用temp=$(${all_containers_list[i]}| cut -f1-3 -d'-')

Request your help to fix the same!请求您的帮助来解决相同的问题! OS is CentOS , bash script操作系统是 CentOS , bash 脚本

I think calling subprocesses just for removing a part of a string, is a bit of an overkill.我认为调用子进程只是为了删除字符串的一部分,有点矫枉过正。 Assuming that you have stored your string in a variable, say假设您已将字符串存储在变量中,例如

str=st2-api-alpha

you could simply do a你可以简单地做一个

if [[ $str =~ [^-]*-[^-]* ]]
then
  str=${BASH_REMATCH[0]}
fi

If the string does not contain a dash, it is left unchanged.如果字符串不包含破折号,则保持不变。 You can trvially generalize this solution to your case of having an array of strings.您可以简单地将此解决方案推广到您拥有字符串数组的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM