使用 echo "passwd"|sudo -S command
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.
即-S使得sudo从标准输入读取密码而非终端
例如 在shell中执行关机命令
#!/bin/bash
echo "passwd"|sudo -S shutdown -P now