作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
下面是我的脚本,其中我根据情况将值放入 local_server
变量中:
#!/bin/bash
servers=(machine981.abc.host.com luck123.pqr.host.com gond654.def.host.com)
case $(hostname -f) in
*abc.host.com)
local_server=("${servers[0]}")
;;
*pqr.host.com)
local_server=("${servers[1]}")
;;
*def.host.com)
local_server=("${servers[2]}")
;;
*) echo "unknown host: $(hostname -f), exiting." && exit 1 ;;
# ?
esac
# now this print works fine and it shows right value
echo $local_server
hello() {
el=$1
we=$2
# but this print inside "hello" function doesn't print anything? Why?
echo $local_server
}
export -f hello
parallel -j 3 hello {} $primary ::: ${pk[@]} &
parallel -j 3 hello {} $secondary ::: ${sk[@]} &
现在我有一个函数 hello()
,我想在其中使用 local_server
变量,但看起来我不能使用它?它打印为空。这背后的原因是什么?如何在 hello
函数中打印 local_server
变量?
最佳答案
您有两个选择。导出变量:
export local_server
或使用 env_parallel:
env_parallel -j 3 hello {} $primary ::: ${pk[@]} &
(env_parallel 甚至复制数组、函数和别名 - 与您在 UNIX 中习惯的相反)。
关于linux - 如何在 shell 脚本中的函数内打印变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49838648/
我是一名优秀的程序员,十分优秀!