Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash for循环中的嵌套函数不工作_Bash_Shell - Fatal编程技术网

Bash for循环中的嵌套函数不工作

Bash for循环中的嵌套函数不工作,bash,shell,Bash,Shell,for循环中的嵌套函数似乎没有按预期工作。循环在迭代一次后中断 # copy files copyFiles () { for (( i=0; i<${#filetype[@]}; ++i )); do //some code goes here done } # copy common components copyComponents () { copyFiles $1 } for (( i=0; i<3; ++i )); do echo $i

for循环中的嵌套函数似乎没有按预期工作。循环在迭代一次后中断

# copy files
copyFiles () {
   for (( i=0; i<${#filetype[@]}; ++i )); do
     //some code goes here
   done
}

# copy common components
copyComponents () {
  copyFiles $1
}

for (( i=0; i<3; ++i )); do
  echo $i //iterates only once.. expectation is thrice
  case $1 in
    components)
       copyComponents $module;;
    *)
       echo "unknown type"
   esac
done
#复制文件
复制文件(){

对于((i=0;i),问题在于shell中的所有变量都是全局变量,除非另有声明。下面是一个简单的示例来演示这一点:

func () {
    for ((i=0; i<10; ++i)); do
        echo "func: $i"
    done
}

for ((i=0; i<5; ++i)); do
    echo "Loop: $i"
    func
done

echo "$i"
copyFiles () {
    local i
    for (( i=0; i<${#filetype[@]}; ++i )); do
        # some code goes here
    done
}