900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > ubuntu/linux运行shell脚本sudo自动输入密码(亲测可以)

ubuntu/linux运行shell脚本sudo自动输入密码(亲测可以)

时间:2018-09-01 23:21:55

相关推荐

ubuntu/linux运行shell脚本sudo自动输入密码(亲测可以)

运行如下代码为例

sudo apt-get update

第一种方法

使用管道(上一个命令的 stdout 接到下一个命令的 stdin):

#!/bin/bashecho yourpassword | sudo -S apt-get update

第二种方法

使用文本块输入重定向:

#!/bin/bashsudo -S apt-get update << EOF yourpasswordEOF

说明:在shell脚本中,通常将EOF与 << 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止,再返回到主Shell,即将‘你的密码’当做命令的输入

-S参数的作用

使用man命令查询sudo,对参数-S的说明如下:

Write the prompt to the standard error and read the password from the standard inputinstead of using the terminal device. The password must be followed by a newline character.

可见 加上-S参数 sudo才会从标准输入中读取密码,不加-S参数以上命令将起不到作用

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。