Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacBH928

macrumors G3
Original poster
May 17, 2008
8,763
3,913
So I learned you can automate this, I have these commands that I want to execute in sequence on Raspbian:

Sudo apt update
sudo apt dist-upgrade
sudo apt clean
sudo reboot

but I dont know how to put them in bash file/command, I know how to launch Nano. After the file is saved how do I execute the bash script? cat "scriptname.sh" ?
 
I'm about to leave for work so someone will give you a more polished version, but the script could theoretically be as simple as a text file containing the following:

Code:
#!/bin/sh

apt update
apt dist-upgrade
apt clean
reboot

then run this command to make it executable:

Code:
chmod +x your-script-filename-here.sh

Invoking the script is just

Code:
sudo ./your-script-filename-here.sh
 
I'm about to leave for work so someone will give you a more polished version, but the script could theoretically be as simple as a text file containing the following:

Code:
#!/bin/sh

apt update
apt dist-upgrade
apt clean
reboot

then run this command to make it executable:

Code:
chmod +x your-script-filename-here.sh

Invoking the script is just

Code:
sudo ./your-script-filename-here.sh


Code:
#!/bin/bash

apt update
apt dist-upgrade
apt clean
reboot

Would actually use the bash interpreter for the commands instead of the sh one.
 
chmod +x your-script-filename-here.sh
Code:
#!/bin/bash

apt update
apt dist-upgrade
apt clean
reboot

Would actually use the bash interpreter for the commands instead of the sh one.

HACK THE PLANET!!

it worked! Although it didn't work when I was in the Desktop directory where I saved the file. I went to the root directory(or user?!) and did "sudo ~/Desktop/upgrade.sh" and it worked.

Whats the difference between using bash or sh?

thanks guys!
 
it worked! Although it didn't work when I was in the Desktop directory where I saved the file. I went to the root directory(or user?!) and did "sudo ~/Desktop/upgrade.sh" and it worked.

Whats the difference between using bash or sh?

thanks guys!

Put simply, sh is more portable.

They are both scripting languages. In ye olde days when Bell Labs sold Unix, that came with the Bourne shell (/bin/sh), which became the basis for the POSIX defined shell scripting language standard. Later on, the GNU people wrote the "Bourne Again Shell" or bash, which is similar, but with expanded features. It has enough syntax differences that you will not always be able to run a bash script using /bin/sh.

Now, a lot of Linux systems just symlinked /bin/sh to /bin/bash, so you were actually running bash either way. I have hated that practice for decades. It caused many Linux users to think that /bin/sh would always be bash and write scripts that used bash-specific syntax that would not work on a system where /bin/sh was an actual Bourne shell implementation. As a BSD guy, I have had headaches from trying to convert a script written in bash to sh because the original developer used Linux where the differences didn't matter.

In your case, a very simple script like that will run fine using either shell because it does not use any syntax that is specific to one or the other. For my stuff (and anyone who asks for my opinion, ha), I always use /bin/sh because it is guaranteed to exist on any POSIX compliant system, so I can move that script to different machines and it will still work.
 
Whats up...
So member 556fmjoe kindly wrote for me this script
Code:
#!/bin/sh

apt update
apt dist-upgrade
apt clean
reboot

During the update process after it downloads the new packages it asks if I would like to install the new packages for it to continue. I have to:
1-press "y"
2-press enter

can this be added to the script?!
 
Whats up...
So member 556fmjoe kindly wrote for me this script
Code:
#!/bin/sh

apt update
apt dist-upgrade
apt clean
reboot

During the update process after it downloads the new packages it asks if I would like to install the new packages for it to continue. I have to:
1-press "y"
2-press enter

can this be added to the script?!

Change it to this and it will answer the question automatically.

Code:
#!/bin/sh

apt update
apt -y dist-upgrade
apt clean
reboot
 
Change it to this and it will answer the question automatically.

Code:
#!/bin/sh

apt update
apt -y dist-upgrade
apt clean
reboot
awesome thanks!
I created a file in nano and saved it then ran the chmod command, I tried it and it seems to work but my file does not have the extension .sh its simply called "update" is that ok?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.