bash:函数作用域外的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33376904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash: variable outside function scope
提问by DSD
Is there a possible way to initialize a global variable in bash and assign it a value in a function, then use it out side the function scope?
有没有办法在 bash 中初始化全局变量并在函数中为其赋值,然后在函数作用域之外使用它?
Function example:
功能示例:
globla_var=""
_DBINFO()
{
curl -su $AUTH https://<balla bla >/databases | jq -c 'map(select(.plan.name != "Sandbox")) | .[] | {id, name}'| \
while read db
do
idb=$(echo "$db" | jq -r '.id')
name=$(echo "$db" | jq -r '.name')
if [[ $name = '<bla>' ]]; then
$global_var_her = $(<bla value>)
fi
done
}
then use it outside the function:
然后在函数外使用它:
echo $global_var
the result
结果
declare -r global_var
: line 16: =<bla bla>: command not found
I tried using declare:
我尝试使用声明:
_DBINFO()
{
while read db
do
idb=$(echo "$db" | jq -r '.id')
name=$(echo "$db" | jq -r '.name')
if [[ $name = '<bla>' ]]; then
global_var=value
fi
done < <(curl -su "$AUTH" "https://$host/databases" |
jq -c 'map(select(.plan.name != "Sandbox")) | .[] | {id, name}')
}
AUTH="user:password"
host="example.com"
_DBINFO
echo "The global variable is $global_var"
same results
相同的结果
回答by that other guy
Yes you can, but you have to be careful about subshells that limit scope in unexpected ways. Piping to a while read
loop like you are doing is a common pitfall.
是的,您可以,但是您必须小心以意想不到的方式限制范围的子shell。while read
像您这样做的管道循环是一个常见的陷阱。
Instead of piping to a while read loop, use redirection and process substitution:
而不是管道到 while 读取循环,使用重定向和进程替换:
global_var=$(<bla value>) # or maybe: global_var='<bla value>'
You also need to make sure your assignment is syntactically valid. $var = value
is not a valid bash assignment, while var=value
is. shellcheckcan point out many things like that.
您还需要确保您的分配在语法上是有效的。$var = value
不是有效的 bash 分配,var=value
而是。shellcheck可以指出很多类似的事情。
回答by whoan
Maybe you're confusing bash
with php
. Just remove the $
, the _her
part, and the space:
也许你混淆bash
使用php
。只需删除$
、_her
部分和空格:
$global_var_her = $(<bla value>)
instead of:
代替:
$(<bla value>)
With that, the error goes away.
这样,错误就消失了。
On the other hand, check that other guy's answerregard to the pipelines.
And yes, it is possible to define a global variable and it is done the way you're doing it.
是的,可以定义一个全局变量,并且按照您的方式完成。
Maybe is useful pointing something about this:
也许是有用的指出一些关于这个:
global_var=""
_DBINFO(){
content=$(
curl -su $AUTH https://<balla bla >/databases |
jq -c 'map(select(.plan.name != "Sandbox")) | .[] | {id, name}'
)
while read -r db
do
idb=$(echo "$db" | jq -r '.id')
name=$(echo "$db" | jq -r '.name')
if [[ $name = '<bla>' ]]; then
global_var=$(echo "<bla value>")
fi
done <<<"$content"
}
If <bla value>
is a command, the sentence is ok, because that't the way to capture the output of a command with Command Substitution
.
如果<bla value>
是命令,则该句子没问题,因为这不是使用Command Substitution
.
If, instead, is a literal value, just remove the $()
leaving just '<bla value>'
(global_var='<bla value>'
). This is the same to do $(echo '<bla value>')
but that would be an unnecessary waste of resources.
相反,如果是文字值,只需删除$()
仅'<bla value>'
( global_var='<bla value>'
) 即可。这样做是一样的,$(echo '<bla value>')
但那将是不必要的资源浪费。
回答by whoan
All variables have global scope, unless declared local inside some function.
所有变量都有全局作用域,除非在某个函数中声明为局部变量。
So, all the vars in your program are valid outside the function.
But there are several problems:
因此,程序中的所有变量在函数之外都是有效的。
但是有几个问题:
- You are sending the output of
curl ...
with a pipe|
. That creates a sub-shell. Variables changed inside a sub-shell are not translated to global. - The name of the var was
globla_var=""
. Is that a typo? Should it beglobal_var
?. - There is an space before and after the = in:
$global_var_her = "bla"
. That is incorrect syntax in bash, and in shell in general. - There is a
$
on the left side of an equality. That does not assign to the variableglobal_var_her
. - There is an additional
_her
which seems UN-needed.
- 您正在
curl ...
使用管道发送输出|
。这将创建一个子外壳。在子外壳内更改的变量不会转换为全局变量。 - var 的名称是
globla_var=""
. 这是一个错字吗?应该是global_var
吗?。 - 在 = in: 之前和之后有一个空格
$global_var_her = "bla"
。这在 bash 和 shell 中都是不正确的语法。 - 有一个
$
放在等号的左边。这不会分配给变量global_var_her
。 - 还有一个
_her
似乎是联合国需要的。
All corrected, this should work:
全部更正,这应该有效:
##代码##