我正在尝试通过bash脚本自动化某些日常工作。为此,我需要创建以下逻辑脚本
这是我的代码 logic.sh
#! /bin/bash
su another_user
cd /home/another_user #tried with sudo too
在执行logic.sh之后
. logic.sh
我看到 root 用户切换到 other_user ,但是我留在/ root目录中
我做错了或bash脚本不适用于用户切换逻辑具有的自动化逻辑
答案 0 :(得分:0)
执行su
时,将启动一个新的shell。从该外壳退出时,脚本login.sh
将以cd /home/another_user
继续。您可以使用sudo -u another_user command_to_execute
以其他用户身份执行命令。
这是如何运行bash
以能够提供多个命令的示例:
sudo -u another_user /bin/bash<<EOF
cd
pwd
ls
EOF