bash脚本,自动输入sudo的密码

 

解决方法: echo + | (管道)

开始使用 

echo "admin" | sudo service tomcat7 stop

始终提示输入密码

后来查看了下sudo命令的使用

man sudo

发现有如下的解释:

-S          The -S (stdin) option causes sudo to read the password from
                   the standard input instead of the terminal device.  The
                   password must be followed by a newline character.

因此,之前的命令修改如下:

echo "admin" | sudo -S service tomcat7 stop


OK可以work

PS:网上说Shell 的expect工具,专门用来实现自动交互功能的,由于我的需求挺简单的,暂时用不到,后面有用的话,再做研究。

猜你喜欢

转载自blog.csdn.net/GorgeousChou/article/details/83045501
今日推荐