分页: 1 / 1
shell脚本中怎样由user自动切换到root
发表于 : 2010-03-25 9:49
由 shenxun521
大家好 我想在脚本中由user 自动切换到root请问我该怎么做
比如我的密码是123456
我在脚本中su
然后想自动能够输入123456,以root的状态执行某些命令谢谢大家
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-03-26 12:28
由 aerofox
sudo
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-03-26 12:36
由 delectate
su root
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-03-27 20:27
由 shenxun521
先谢谢ls 汗了 但是我的意思是在脚本中直接su root 然后想自动填入密码123456
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-03-27 22:52
由 eexpress
echo 12345 | sudo -S
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-03-29 17:00
由 shenxun521
谢谢ls 但是我在sh脚本中
echo 12345|su root
会显示
su:必须从终端中执行
呵呵 就是想自动填入密码,不知道该怎么弄
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-03-31 14:38
由 jxcangel
想在脚本中做需要人工交互的工作,那大概就得用Expect了,不过Expect是一个基于TCL语言的shell,和bash之间有些不同,不过这个脚本倒是可以给你参考一下:
#!/usr/bin/expect
expect -re "\$"
send "su root\r"
expect -re "\[P|p]assword:"
send "your password\r"
expect -re "\#"
...........
简单来说,就是用expect来匹配关键字,然后用send来发送匹配到关键字以后要输入的字符,注意发送字符的时候后面要带上 \r 来模拟回车操作。
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-03-31 15:38
由 eexpress
执行看,是sudo -S
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-04-01 20:30
由 shenxun521
谢谢 ls各位 sudo -S 可以执行
但是我用
sudo -S apt-get install amarok 的时候产生如下信息
共升级了 0 个软件包,新安装了 14 个软件包,要卸载 0 个软件包,有 6 个软件未被升级。
需要下载 14.4MB 的软件包。
解压缩后会消耗掉 48.7MB 的额外空间。
您希望继续执行吗?[Y/n]中止执行。
shell 自动帮我终止执行,能有办法让脚本安装一个软件成功么
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-04-01 20:42
由 tenzu
apt-get后面跟-y不知道行不行
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-04-21 22:25
由 djhui
sudo -S apt-get install amarok -y
Re: shell脚本中怎样由user自动切换到root
发表于 : 2010-04-25 23:22
由 dongyuwei
执行visudo,把自己 添加到sudoer中