HA

bash函数 & user 代行

Tags:

bash函数

将函数追加写入~/.bashrc文件即可持久化,否则只在当前shell session内有效,关闭即无效

不要忘记加完需要source ~/.bashrc才会生效

function functionName {
        <commands>
}

例子:

function f1 {
        echo "Hello I'm function 1"
        echo "Bye!"
}

linux下root用户指定user执行操作

sudo -H -u otheruser bash -c 'echo "I am $USER, with uid $UID"'

-H 可选,使用bash -c xxx可执行多条命令,单个命令可直接跟user参数后面

reference

  1. Bash Function & How to Use It
  2. Run a shell script as another user that has no password