我有这个脚本:
GLOBAL_VAR="some global value"
function test
{
echo $1
echo ${GLOBAL_VAR}
}
export -f test
parallel --jobs 5 --linebuffer test ::: "${files[@]}"
我怎样才能$GLOBAL_VAR
从平行处看到?
答案1
Shell 变量对于子进程是不可见的,除非它们被导出,所以你需要说
export GLOBAL_VAR
在子进程启动之前的某个地方;在现代 shell 中,导出可以与分配相结合
export GLOBAL_VAR="some global value"
答案2
“Shell 变量对于子进程是不可见的,除非它们被导出“——即使如此,如果您远程运行命令,它们仍然不可见。除非您使用env_parallel
GNU Parallel 的前端来复制环境:
. `which env_parallel.bash`
aliases
alias myecho='echo aliases'
env_parallel myecho ::: work
env_parallel -S server myecho ::: work
env_parallel --env myecho myecho ::: work
env_parallel --env myecho -S server myecho ::: work
functions
myfunc() { echo functions $*; }
env_parallel myfunc ::: work
env_parallel -S server myfunc ::: work
env_parallel --env myfunc myfunc ::: work
env_parallel --env myfunc -S server myfunc ::: work
variables
myvar=variables
env_parallel echo '$myvar' ::: work
env_parallel -S server echo '$myvar' ::: work
env_parallel --env myvar echo '$myvar' ::: work
env_parallel --env myvar -S server echo '$myvar' ::: work
arrays
myarray=(arrays work, too)
env_parallel -k echo '${myarray[{}]}' ::: 0 1 2
env_parallel -k -S server echo '${myarray[{}]}' ::: 0 1 2
env_parallel -k --env myarray echo '${myarray[{}]}' ::: 0 1 2
env_parallel -k --env myarray -S server echo '${myarray[{}]}' ::: 0 1 2