我想在一个项目中使用fakeroot,但是这个项目有很多函数和变量,我需要传递给fakeroot。
#!/bin/bash
myVar="foo"
function testFunction() {
echo "$myVar"
}
fakeroot -- bash -c testFunction但它不运行testFunction或回显myVar
发布于 2021-07-25 12:44:12
好吧,我想出来了:
#!/bin/bash
myVar="foo"
function testFunction() {
echo "$myVar"
}
tmp_function=$(declare -f testFunction)
fakeroot -- bash -c "$tmp_function; testFunction"发布于 2021-07-25 14:04:57
您还可以使用bash的S导出函数功能。然而,考虑到fakeroot是一个sh脚本,您需要在一个sh实现不会像dash那样从环境中删除这些BASH_FUNC_fname%%变量的系统上。为了确保不会发生这种情况,您可以让bash本身将fakeroot解释为bash -o posix,这是一个sh解释器。
#!/bin/bash -
myVar="foo"
testFunction() {
printf '%s\n' "$myVar"
}
export myVar
export -f testFunction
fakeroot=$(command -v fakeroot)
bash -o posix -- "${fakeroot:?fakeroot not found}" -- bash -c testFunction请注意,还需要导出myVar才能使fakeroot启动的bash可用。您也可以在声明myVar和testFunction之前发出一个set -o allexport,而不是同时为它们调用D17。
https://unix.stackexchange.com/questions/657902
复制相似问题