Search code examples
bashraspberry-piraspbian

raspberry pi, apt-get update in script does not work


When I run:

sudo apt-get update
sudo apt-get upgrade

from the command line, it works.

If I put the same line a script file maintain.script:

echo UPDATING SYSTEM SOFTWARE – UPDATE
sudo apt-get update
echo UPDATING SYSTEM SOFTWARE – UPGRADE
sudo apt-get upgrade

and run:

sudo ./maintain.sh

I get errors:

E: Invalid operation update
E: Invalid operation upgrade

I have marked the script as an executable.

Updated After Comment from FSQ

Here is the script file:

#!/bin/bash
echo "UPDATING SYSTEM SOFTWARE – UPDATE"
apt-get update
echo "UPDATING SYSTEM SOFTWARE – UPGRADE"
apt-get upgrade
echo "UPDATING SYSTEM SOFTWARE – DISTRIBUTION"
apt-get dist-upgrade
echo "REMOVING APPLICATION ORPHANS"
apt-get autoremove –purge
echo "UPDATING FIRMWARE"
rpi-update

Here is the command:

pi@raspberrypi2 ~/projects $ sudo ./maintain.sh

Here is the result:

: not foundsh: 1: ./maintain.sh: #!/bin/bash
UPDATING SYSTEM SOFTWARE – UPDATE
E: Invalid operation update
UPDATING SYSTEM SOFTWARE – UPGRADE
E: Invalid operation upgrade
UPDATING SYSTEM SOFTWARE – DISTRIBUTION
E: Invalid operation dist-upgrade
REMOVING APPLICATION ORPHANS
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package –purge
UPDATING FIRMWARE
: not foundsh: 11: ./maintain.sh: rpi-update

Solution

  • It was a file format problem. I was editing the files over a folder share using Windows notepad, which uses a different \r\n to Linux.

    Here is the command that corrected my script file:

    sed -i 's/\r//' maintain.sh
    

    Here is a script file I use to do all the script files in a folder, and make sure they are executable:

    #!/bin/bash
    echo "Correcting script file formats"
    for file in *.sh
    do
        echo $file
        sed -i 's/\r//' $file
        chmod +x $file
    done