shell脚本中填写密码

使用shellj脚本中执行安装或一些操作时需要输入密码的问题?
环境:Ubantu 18.4.1
例如:sudo apt-get xfce4

第一种方式:上一个命令的 stdout 接到下一个命令的 stdin

#!/bin/bash
echo password | sudo -S apt-get update

第二种方式:使用文本块输入重定向

#!/bin/bash
sudo -S apt-get update << EOF 你的密码 EOF

说明:

在shell脚本中,通常将EOF与 << 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止,再返回到主Shell,即将‘你的密码’当做命令的输入

-S参数的作用
 -S, --stdin
     Write the prompt to the standard error and read the password from the standard 
     input instead of using the terminal device.
     The password must be followed by a newline character.
   说明:将提示写入标准错误,并从标准输入中读取密码,而不使用终端设备。密码后必须带有换行符。

猜你喜欢

转载自blog.csdn.net/qq_40267002/article/details/117334096