![](/img/trans.png)
[英]How can I assign the output of a function with parameters to a variable using bash?
[英]In Bash, how can I modify a global variable in a function of a module?
我的腳本中有一個全局變量:
export _ERR
. library.sh #import my functions
在library.sh中,我想在函數A中進行設置:
function_a ()
{
[[ -f /mnt/file.txt ]] && {
_ERR="Error file does not exist"
}
}
然后在主腳本中調用函數A
output=$(functionA)
如果$ ERR不為空,則顯示$_ERR
:
[[ -n _ERR ]] && echo ${_ERR}
但是_ERR在我的主腳本中始終為空。
我究竟做錯了什么?
通過使用命令替換捕獲輸出,可以在子shell中運行該函數。 子外殼程序無法更改父外殼程序中的變量值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.