sudo 密码直接添加到命令行以方便实现脚本自动化

方法1:

使用管道传递密码。

比如 root 密码为 abc,要执行命令 fdisk -l, 则用法如下,

$ echo abc | sudo -S fdisk -l

其中 -S 参数表示: read the password from the standard input instead of using the terminal device.  The password must be followed by a newline character.

 

方法2:

使用文本重定向。

比如 root 密码为 abc,要执行命令 fdisk -l, 则用法如下 (shell 脚本中),

sudo fdisk -l << END   # 后续的输入作为子命令或子 shell 的输入,直到遇到 END。(这里的 END 可以自定义,比如 OK, EOF,等都可以)
abc
END

 

(完)

 

posted @ 2020-11-09 10:15  Anonymous596  阅读(2807)  评论(0编辑  收藏  举报