1

I am writing a shell script for running installation on Raspberry Pi my command is as below:

$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo apt-get dist-upgrade

my shell script is as below:

#!/bin/sh
apt-get update
apt-get -y upgrade
apt-get dist-upgrade

I save the file as .sh file and run it in Raspbian command prompt. But I get the invalid syntax error in line 2. there is an arrow pointing on the "upgrade"

I had refer most of the sample that i found online and i still cant figure out what's wrong with my bash file.

  • 1
    Hello. Since "upgrade" is on line 3, not line 2 this might be a problem with line-endings. What does file NameOfYourScript output? Copy the output it to your question. – Roger Jones Apr 4 at 7:37
  • 1
    Why not use the automation that's already available rather than creating your own? unix.stackexchange.com/a/107198/325016 – Dougie Apr 4 at 7:46
  • @RogerJones Sorry, in my script there i had one blank row before my installation command. i guess that's the reason. – alpha91 Apr 4 at 8:14
  • @alpha91 I don't think black rows are a problem. – GramThanos Apr 4 at 8:16
  • 1
    So the script in the question is not the script giving the error? How are we supposed to help you. Did you try the file command? – Roger Jones Apr 4 at 9:32
1

It may be possible that there are invisible characters in the script. I have this behavior also seen, mostly when using editor from MS Windows. For me it helped to create the script once again from scratch.

For this small test script start with:

rpi ~$ cat > testscript.sh <<EOF

Then type the commands of your script and as last line EOF. When you finished typing it looks like this:

> #!/bin/sh
> apt-get update
> apt-get -y upgrade
> apt-get dist-upgrade
> EOF

A final Enter will copy it to testscript.sh. Check with cat testscript.sh. This ensures that only linux console characters are used. Then execute the script with:

rpi ~$ sudo bash testscript.sh
Hit:1 http://raspbian.raspberrypi.org/raspbian stretch InRelease
Hit:2 http://archive.raspberrypi.org/debian stretch InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
rpi ~$

As you can see, it works on my RasPi.

  • if you end up with large files that were created/edited in windows, there is the dos2unix command that will help convert line endings and such – Chad G Apr 4 at 16:00
  • Hi Ingo, thank you very much for your reply. I found out my issue was actually i include "sudo" in my bash file. and also when i executing my bash file in my command windot, i am typing " sudo python update.sh" which is wrong. Thanks a lot for your detail sample. – alpha91 Apr 5 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.