问题
在运行Linux Bash脚本的时候,经常会被linux系统要求输入密码才能获取sudo的权限继续脚本的执行。交互执行时,脚本执行人员可以人工输入密码来时脚本获取sudo的权限来继续运行。
但是,我们的问题是:全自动运行bash 脚本的时候怎么办。有没有一种方法来自动的帮助我们输入密码获取sudo的权限呢?
解决方法
echo "password for sudo" | sudo -S xxxx.sh
or
echo "password for sudo" | sudo -S yourCommand
对于参数 -S 对于的解释,在linux online manual上可以查到。
-S, --stdin
Write the prompt to the standard error and read the
password from the standard input instead of using the
terminal device.
结论
简单,好使
博客围绕Linux Bash脚本运行时需输入密码获取sudo权限的问题展开。交互执行时可人工输入密码,但全自动运行时存在困难。通过查询linux online manual找到参数 -S 来解决自动输入密码获取权限的问题,结论是该方法简单好使。

3万+

被折叠的 条评论
为什么被折叠?



