2013-08-05 58 views
2

我正试图找到一种方法,以便在用于AWS CLI命令的bash阵列的元素之间没有空格。该命令的过滤器表示过滤器的格式必须是'--filters name = string1,values = string1,string2'。删除bash数组元素之间的空格

,我目前有至今代码:这给我的

foo-bar, herp-derp, bash-array, 

输出

tag_filter=($(aws ec2 describe-tags --filter "name=value,values=${tags[@]}" | jq '[.Tags[] | {ResourceId}]')) 
regex=[[:alpha:]][-][[:xdigit:]] 
for x in ${tag_filter[@]} 
do 
    if [[ $x =~ $regex ]] 
    then 
    #parameter expansion to remove " from elements 
    resource_id+=("${x//\"},") 
    #$resource_id== "${resource_id_array[@]// /,}" 
    else 
    throw error message 
    fi 
done 
echo "${resource_id[@]}" 

,但它需要

foo-bar,herp-derp,bash-array, 

下一个滤镜命令上班。我所搜索的所有内容都是删除字符串中的空格,将字符串转换为数组,或者是一般数组上的文档,而且我在任何地方都没有看到类似的问题。

编辑:

我添加anubhava的print语句到我的代码,这样

then 
    #parameter expansion to remove " from elements 
    resource_id_array+=("${x//\"},") 
    resource_id= $(printf "%s" "${resource_id_array[@]}") 
    resource_id= ${resource_id:1} 
    #${resource_id[@]}== "${resource_id[@]// /,}" 
else 

现在给我我需要的,但给我的输出中“:找不到命令错误”时我回显“$ resource_id”后运行脚本

回答

0

所以我已经结束了做的是此基础上anubhava的回答和评论

tag_filter=($(aws ec2 describe-tags --filter "name=value,values=${tags[@]}" | jq '[.Tags[] | {ResourceId}]')) 
regex=[[:alpha:]][-][[:xdigit:]] 
for x in ${tag_filter[@]} 
do 
    if [[ $x =~ $regex ]] 
    then 
    #parameter expansion to remove " from elements 
    resource_id+=("${x//\"}") 
    else 
    throw error message 
    fi 
done 

resource_id=$(printf "%s" "${resource_id_array[@]}") 
echo "${resource_id[@]}" 
0

这就是echo与数组一起工作的方式。使用printf这样的:

printf "%s" "${resource_id[@]}" && echo "" 

,你应该看到:

foo-bar,herp-derp,bash-array, 
+0

,用来获取工作数组的输出,但我需要数组本身的格式。 – user2642953

+0

数组只有单个元素。你是否注意到单个元素中的尾部空格?尝试打印它:'echo“@ $ {resource_id [0]} @”'来验证。 – anubhava