bash 脚本执行时自动切换到root用户

#!/usr/bin/env bash
# Author=liy36
export PS4='\[\e[35m\]+ $(basename $0):${FUNCNAME}:$LINENO: \[\e[0m\]'

if [ "$(id -u)" -ne 0 ];then
	echo "Switch to the root user execute $0 $@"
	exec sudo bash "$0" "$@"
	exit 0
fi

echo "I'm running on $(id -u)."
echo "args ${@}"
posted @ 2024-12-20 15:15  liy36  阅读(92)  评论(0)    收藏  举报