shell脚本使用sed模式在数组值周围加上双引号,忽略组件中的空格分隔符
我想用双引号将所有数组值括起来,忽略组件中的空格分隔符 例如,我正在运行下面的bash脚本shell脚本使用sed模式在数组值周围加上双引号,忽略组件中的空格分隔符,shell,Shell,我想用双引号将所有数组值括起来,忽略组件中的空格分隔符 例如,我正在运行下面的bash脚本 bash ''' #!/bin/bash git checkout deltafinal metaItems="$(git diff HEAD~1 --name-only)" #components involved in very last commit in git OUTPUT: metaItems=src/profiles/API Only.
bash '''
#!/bin/bash
git checkout deltafinal
metaItems="$(git diff HEAD~1 --name-only)" #components involved in very last commit in git
OUTPUT: metaItems=src/profiles/API Only.profile src/profiles/Chatter External User.profile src/profiles/only one.profile
array=($(echo $metaItems | sed 's! src/!,src/!g'))
OUTPUT:
array= metaItems=src/profiles/API Only.profile,src/profiles/Chatter External User.profile,src/profiles/only one.profile
Now I want to put the double quotes around every array value starting from "src/" keyword
,ending till ".profile" and store them in an array.
Expected Result:"src/profiles/API Only.profile","src/profiles/Chatter External User.profile","src/profiles/only one.profile"
P.S. It should ignore all the whitespaces between the characters
例如,如上所示的/API和Only和/或Chatter外部用户等,并将其视为3个单独的组件
src/profiles/API Only.profile- 1 word
src/profiles/Chatter External User.profile - 2 word
src/profiles/only one.profile - 3 word
请帮忙。我想你想要的只是
array=( $( git diff HEAD~1 --name-only ) )
应该将每个项目插入到一个不同的元素中。访问数组元素时,请特别小心引用。
对于文件名
abc
和def
$: printf "%s\n" ${array[@]} # unquoted, will parse incorrectly
a
b
c
d
e
f
绝对是不与
$: printf "%s\n" "${array[@]}" # properly quoted, parses properly
a b c
d e f
它不起作用了。。将空格分隔符视为“${array[@]}”中项的数组中的一个单独元素,例如array=src/profiles/API Only.profile,src/profiles/Chatter External User.profile,src/profiles/Only one.profile”,将“src/profiles/API”视为下一次迭代“External”中的一个元素“Only.profile,src/profiles/Chatter”和“用户.Profile”作为第三和第四个元素分别如何忽略元素之间的空间分隔符,并将它们看作是一个单词。我认为围绕它们的双引号会很好。请给出建议。这就是为什么我不在文件名中放置空格的原因。