shell???

sh/bash/dash/ksh/zsh等Shell脚本
回复
381265330
帖子: 38
注册时间: 2008-05-25 15:52

shell???

#1

帖子 381265330 » 2010-12-16 23:53

shell脚本里的命令如何以root用户执行,就像安装软件时提示输入root密码???
huangyun
帖子: 49
注册时间: 2006-11-27 14:21
联系:

Re: shell???

#2

帖子 huangyun » 2010-12-17 2:27

直接在命令前面加 sudo ?
头像
icyomik
帖子: 76
注册时间: 2010-10-30 17:52

Re: shell???

#3

帖子 icyomik » 2010-12-17 13:29

在shell脚本中添加判断,如果uid不为0则写sudo -v,脚本结束前写sudo -k
个人博客:http://blog.jtwo.me/
女友淘宝:http://icykiss.taobao.com/
你有什么不开心的事情,说出来让大家开心一下。
头像
icyomik
帖子: 76
注册时间: 2010-10-30 17:52

Re: shell???

#4

帖子 icyomik » 2010-12-17 13:41

下面也可:

代码: 全选

if [[ `id -u` -ne 0 ]]; then
    echo "Need to be ROOT to run install."
    if [[ `which gksu >/dev/null` -ne 0 ]]; then
        echo 'use `sudo` or `su -c` to switch to root.'
        exit 1
    fi
    gksu $0
    exit 0
fi
个人博客:http://blog.jtwo.me/
女友淘宝:http://icykiss.taobao.com/
你有什么不开心的事情,说出来让大家开心一下。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: shell???

#5

帖子 eexpress » 2010-12-17 13:48

不是有sudo -S?
● 鸣学
头像
linxiaoyu
帖子: 39
注册时间: 2009-05-03 21:09

Re: shell???

#6

帖子 linxiaoyu » 2010-12-17 13:49

chmod +s +file 试试
GONE WITH THE WIND ~~~
回复