開始使用
echo "admin" | sudo service tomcat7 stop
始終提示輸入密碼
後來查看了下sudo命令的使用
man sudo
發現有如下的解釋:
-S The -S (stdin) option causes sudo to read the password fromthe 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工具,專門用來實現自動交互功能的,由於我的需求挺簡單的,暫時用不到,後面有用的話,再做研究。