Shell脚本"$ {array [@]}"扩展:第一个和最后一个条目具有额外的字符 [英] shell script “${array[@]}” expansion: first and last entry have extra characters
问题描述
我有一个使用数组的shell脚本.该脚本在数组的各个条目之间循环,但是由于某种原因,第一个和最后一个条目有问题.
I have a shell script that uses an array. The script cycle through the entries of the array but for some reason the first and last entry has a problem.
数组:
Queue_Names=( CLQueue DLQ ExpiryQueue )
for循环:
for i in "${Queue_Names[@]}"
do
#do stuff
done
我可以在控制台中看到并显示它在第一个条目中显示的内容:``CLQueue.最后一个条目显示:ExpiryQueue.
I can see in the console and shows that for the first entry it shows: �CLQueue. The last entry shows: ExpiryQueue�
我猜这是知道数组开始和结束的标记.不幸的是,它干扰了脚本的功能.我使用这些队列名称来搜索内容,但由于添加了字符,所以找不到它.我该如何摆脱它们或进行代码更改以避免出现此问题?
I'm guessing these are markers to know the start and end of the array. Unfortunately it is interfering with the functionality of the script. I use these Queue names to search for something and it fails to find it because of the added character. How do I get rid of them or is there a code change I do to avoid the problem?
推荐答案
"$ {Queue_Names [@]}""
不是"$ {Queue_Names [@]}"
,因为"
不是"
.
bash根本不会将智能引号"识别为引号;因此,其效果就如同未对扩展名进行了引用(对数组内容进行字符串拆分和glob-expansion)一样,将文字引号"嫁接到了开始和结束字符周围.
"Smart quotes" aren't recognized as quotes at all in bash; thus, the effect is the same as if the expansion had been unquoted -- string-splitting and glob-expansion on array contents -- with the literal "quotes" grafted around the start and end characters.
您需要使用真实的引号-"
-不能打开/关闭由某些文字处理软件或公司电子邮件工具创建的智能引号".
You need to use real quotes -- ""
-- not opening/closing "smart quotes" created by some word processing software or corporate email tools.
这篇关于Shell脚本"$ {array [@]}"扩展:第一个和最后一个条目具有额外的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!