我已经开始学习bash脚本。我在概念上有疑问,如何确定该脚本是以root用户还是普通用户身份运行?
我知道如果用户为0的UID,但是我不知道如何实现它。谁能解释我如何使它工作。"Not a root user"
当脚本以普通用户身份运行时,是否可以打印消息?
我的脚本很简单hello_world.sh
:
#!/bin/bash
echo 'Hello World';
POSIX shell具有内置变量$UID
和$EUID
。您也可以查看的退货id -u
。在每种情况下,id 0表示根。
#!/bin/sh
if [ $EUID -ne 0 ]; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句