A .sh (shell) or bash file can be utilized in increasing productivity and managing menial, repetitive tasks. When a script has properly set permissions and syntax it then executes commands in a fraction of the time that you would take. In this post, we will create and run a shell (.sh) script on Raspberry Pi 3/4. We will also include 3 Raspberry Pi bash script examples that you can use as a starting point.
With Shell Scripts you can automate anything you can do on the Linux command line. It is a file that carries shell commands executed one by one as they were input on the command line directly.
The best benefit is they help save time by executing many commands at the same time or if you intend to automatically boost commands like pi boots up.
2 most common advantages of using a shell script on your Raspberry P:
- Automate home tasks or any hardware your Pi may be attached to.
- With this write down a script to automate a set of instructions that need to be executed one after the other in spite of typing in commands one after the other.
- Automate repetitive tasks, account creation for different users, and much more.
- Data Backup, would you want to manually run your backups at 2 in the morning? No.
How does an SH file or Bash Script Work?
In the realm of Linux and systems in general there is the concept of programs and processes. The program usually carries binary data which consists of a series of instructions for the CPU and other resources which include pictures, sound files and much more which are organized in the form of a package and typically stored there on the hard disk.
Now, when you are running a program it is not merely the program it is indeed a copy of it which is known as process. The instructions and resources are copied there from the hard disk into RAM or working memory. Moreover, some space is also allocated in RAM for the process to store variables and some flags to allow the OS to be able to manage as well as track the complete process during its execution.
The Bash Process
Now chances are that there can be many different processes representing the very same program that is running in the memory at the same time. like you can have two terminals open and also be running the cp command in both of them. In such as case there shall be two cp processes present on the system currently. As they finish running the system shall destroy them and no processes shall be there to represent program cp.
Now when you are at the terminal you need to have the bash process running in order to have the bash shell. What if you start running a script? Well, it won’t run in that process but shall start a new process to run within.
How To Create and Run Your Own .sh Shell Script on Raspberry Pi 3/4 (With Examples)
If you are the kind of person who enters long sequences of commands into the command line then this is designed for you. If you know how to write a shell script then many operations can be accomplished with only single command. It can be used to:
- Batch convert file formats.
- Rename batch and move files.
- Use one command output as the input of another command.
- Whenever an event occurs it starts the applications.
- Have control on what happens whenever the system boots up.
Among many features, one best is its ability to create pipes. For those who are not aware, this allows the output of one command to be forwarded to the input of the next one. Moreover, they can also be used with many commands. Its basic syntax is as follows an example:
Run First Backup | Run Offsite Backup
Create a .sh bash Script File in 5 Steps
These are executable text files with the extension “.sh”. We are going to explain to you with examples, we shall be writing a simple “hello world” script to explain to you how to create and run them.
- Login to “Raspberry Pi”.
- Then navigate to the directory where you wish to save the script, e.g. /home/paul/scripts.
- After this open “nano or vim text editor” and create a new file which shall be named “hello-world-from-ninja-ide.sh” by entering the following command:
[email protected]:~# vi hello-world-from-ninja-ide.sh
4. After this enter the following code there in the text editor.
#!/bin/bash Echo “hello world from ninja-ide!”
The first line is called shebang which tells the BASH shell to execute the command there in the script. Each script created shall need this on the first line.
5. Exit and then save the file in Nano by hitting “Ctrl-X” from the keyboard to save and exit.
Now you need to use chmod:
[email protected]:~# chmod u+x hello-world-from-ninja-ide.sh
Running your Shell Script
Navigate to the directory where the file has been saved and enter the following command:
[email protected]:~# cd /home/paul/scripts
Once you are in the directory, then just put ./ in front of the script and run it.
[email protected]:~# ./hello-world-from-ninja-ide.sh
That is all, you have now successfully run your bash script on Raspberry Pi.
3 Examples of Bash Script for Raspberry Pi
Command delay script
If you wish to pause any command execution for a certain time then you can use this command, simply set the delay amount by seconds, minutes, hours, and days. For this create a file by the name “pause-command-runninh.sh” and add the script. Ti shall wait for around five seconds after running.
To remove any file use the “rm” command. For this create a file by the name “automatic-deletions.sh” with code to take the name of the file from the user and remove it. This can be used for things such as kill switches or if 3 or 4 password attempts fail, it self destroys.
If you are a student and require a project, you can use shell scripts and a Pi to make an interesting Weather predicting project. You can take it a step further and apply AI/ML models and datasets and use bash scripts to execute commands on different times of the day.
Raspberry Pi’s are great low-power and budget computers, these are more capable than you think they are! This article detailed all you need to know about Raspberry Pi sh file and bash file along with how you can create and run them. We also shared 3 bash script examples that you can run on your Raspberry Pi to have some automation. Happy scripting!