bash 脚本中的多个此处文档放入和退出 sudo 会导致错误“会话仍然打开,未卸载”

bash 脚本中的多个此处文档放入和退出 sudo 会导致错误“会话仍然打开,未卸载”

我正在尝试编写一个安装脚本,我可以在多台计算机上运行它来运行一堆安装程序(需要 sudo),但也可以执行诸如下拉 git 存储库之类的操作(我想以普通用户身份运行它,这样他们就可以所有权+ r/w/x 权限)。我的脚本使用 Ubuntu 14.04 和 bash(如有必要,愿意将它们切换到 zsh,但为了可移植性,希望将它们保留为 bash)

我做了一些阅读,发现这里的文档语法用于以同一用户身份运行多个命令。

但是我收到Sessions still open, not unmounting显示错误消息。

我怎样才能解决这个问题?这是一个问题吗?有没有更好的方法来使用 sudo?

这是我用来测试 sudo 的插入/退出和输出的脚本。

#!/bin/bash
if [[ $EUID -ne 0 ]]; then
   echo "[FAIL] This script must be run as root"
   exit 1
fi

sudo -u myusername bash << \EOF
echo ""
echo "I SHOULD BE A REGULAR USER 0"
echo "uid is '${UID}'"
echo "euid is '${EUID}'"
echo "user is '${USER}'"
echo "username is '${USERNAME}'"
echo ""
EOF

echo ""
echo "I SHOULD BE SUDO'D 1"
echo "uid is '${UID}'"
echo "euid is '${EUID}'"
echo "user is '${USER}'"
echo "username is '${USERNAME}'"
echo ""


sudo -u myusername bash << \REG_USER_END
echo ""
echo "I SHOULD BE A REGULAR USER 2"
echo "uid is '${UID}'"
echo "euid is '${EUID}'"
echo "user is '${USER}'"
echo "username is '${USERNAME}'"
echo ""
REG_USER_END

echo ""
echo "I SHOULD BE SUDO'D 3"
echo "uid is '${UID}'"
echo "euid is '${EUID}'"
echo "user is '${USER}'"
echo "username is '${USERNAME}'"
echo ""

exit 0;

输出:

I SHOULD BE A REGULAR USER 0
uid is '1000'
euid is '1000'
user is 'myusername'
username is 'myusername'

Sessions still open, not unmounting

I SHOULD BE SUDO'D 1
uid is '0'
euid is '0'
user is 'root'
username is 'root'


I SHOULD BE A REGULAR USER 2
uid is '1000'
euid is '1000'
user is 'myusername'
username is 'myusername'

Sessions still open, not unmounting

I SHOULD BE SUDO'D 3
uid is '0'
euid is '0'
user is 'root'
username is 'root'

相关内容