方法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
(完)