Search code examples
bashubunturootsudochmod

Script not recognizing sudo password and fails


So I have this little script that runs sudo apt-get upgrade and update automatically (even answers if I want to upgrade or not), but when I run it through ./sync.sh it prompts me for my password, even tho I declared it into the script.

Does anyone know what's going on?

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}"

The output is the following:

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 --------

Solution

  • The problem may lay in the fact that echo password | yes returns forever

    y
    y
    y
    

    Try to use xargs:

    echo password | xargs yes
    

    or simply yes without pipe

    yes password