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你的密码EOF说明:在shell脚本
·
- 运行如下代码为例
sudo apt-get update
- 第一种方法
使用管道(上一个命令的 stdout 接到下一个命令的 stdin):
#!/bin/bash echo yourpassword | sudo -S apt-get update
- 第二种方法
使用文本块输入重定向:
#!/bin/bash sudo -S apt-get update << EOF yourpassword EOF
说明:在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参数以上命令将起不到作用
更多推荐
已为社区贡献1条内容
所有评论(0)