歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Bash強制腳本使用某用戶執行

Bash強制腳本使用某用戶執行

日期:2017/3/1 9:53:09   编辑:Linux編程

受oneStack腳本啟發:
原來是可以輕松實現強制腳本使用某用戶權限執行的。

腳本內容如下:

#!/bin/bash
# filename: test.sh
# email: [email protected]
# date: 2013-09-02
user="root"
if [ `whoami` != "${user}" ]; then
exec su - "${user}" -c "sh ${PWD}/test.sh"
fi
echo "以下為腳本主體內容"
id
echo "執行id命令結束"

調試信息如下:

linuxied.com @localhost:~$ sh -x test.sh
+ user=root
+ whoami
+ [ xiaoyuwang != root ]
+ exec su - root -c sh /home/xiaoyuwang/test.sh
Password:
以下為腳本主體內容
uid=0(root) gid=0(root) groups=0(root)
執行id命令結束

Copyright © Linux教程網 All Rights Reserved