脚本无法识别 sudo 密码并失败

Script not recognizing sudo password and fails

所以我有这个小脚本 运行s sudo apt-get 升级并自动更新(甚至回答我是否要升级),但是当我 运行 它通过 ./sync.sh 它提示我输入密码,即使我在脚本中声明了它。

有人知道这是怎么回事吗?

sync.sh

#Colors
green=$(tput setaf 2)
normal=$(tput sgr0)

clear
echo password | yes | sudo -S apt-get upgrade

echo "${green}-------- APT-GET UPGRADE DONE --------${normal}"

echo password | yes | sudo -S apt-get update

echo "${green}-------- APT-GET UPDATE DONE ---------${normal}"

输出如下:

Sorry, try again.
Sorry, try again.
Sorry, try again.
sudo: 3 incorrect password attempts
-------- APT-GET UPGRADE DONE --------
Sorry, try again.
Sorry, try again.
Sorry, try again.
sudo: 3 incorrect password attempts
-------- APT-GET UPDATE DONE --------

问题可能在于echo password | yes returns永远

y
y
y

尝试使用xargs:

echo password | xargs yes

或者只是 yes 没有管道

yes password