为什么此for循环不处理数组的所有元素? [英] Why does this for-loop not process all elements of the array?

查看:96
本文介绍了为什么此for循环不处理数组的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下脚本:

#!/bin/bash

asteriskFiles=("sip.conf" "extensions.conf")

for asteriskFile in $asteriskFiles
do
    # backup current configuration file
    cp somepath/${asteriskFile} test/
    echo "test"
done

这仅给我输出一次测试",因此循环仅运行一次,而不是运行两次(asteriskFiles数组中有两个条目).我究竟做错了什么?谢谢您的提示!

This gives me the output "test" only once, so the loop runs only once instead of two times (two entries in asteriskFiles array). What am I doing wrong? Thanks for any hint!

推荐答案

插图:

$ asteriskFiles=("sip.conf" "extensions.conf")
$ echo $asteriskFiles # is equivalent to echo ${asteriskFiles[0]}
sip.conf
$ echo "${asteriskFiles[@]}"
sip.conf extensions.conf

请注意引号很重要. echo ${asteriskFiles[@]}似乎可行,但是如果您的任何文件中包含空格,bash都会在空格上单词分割.

Note that the quotes are important. echo ${asteriskFiles[@]} might seem to work, but bash would wordsplit on whitespace if any of your files had whitespace in them.

这篇关于为什么此for循环不处理数组的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆