繁体   English   中英

如何在不退出当前shell的情况下在当前shell中脚本中设置sudo?

[英]How to set sudo in current shell mid-script without leaving current shell?

我想在此bash函数中间脚本中启用sudo。 我相信问题在于sudo -s打开了一个新的shell,并且由于退出该shell,因此不会打印所有后续行。

function example {
    echo "one"
    sudo -s
    echo "two"
    echo "three"
}

我怎样才能将twothree也打印为sudo? 之前没有添加sudo

双重功能呢? 能帮上忙吗?

#!/bin/bash 
function test2 {
sudo echo "two"
sudo echo "three"
}
function test {
echo "one"
test2
}
test

在脚本test.sh中尝试以下操作:

#!/bin/bash
testfunc() {
echo "test";
sudo sh -c "echo 'haha'";
echo "hooo";
}
testfunc;

然后调用:

bash test.sh

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM