Raspberry Pi Sh File: Install, Create and Run Shell/Bash Script (With Examples)

Create and run your own Raspberry Pi Sh (shell) script file and get ideas from our 4 examples.

0

Raspberry Pi SH (Bash) File: Install, Create and Run Shell Scripts

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:

  1. Automate home tasks or any hardware your Pi may be attached to.
  2. 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.
  3. Automate repetitive tasks, account creation for different users, and much more.
  4. 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.

ALSO SEE: How To Setup Raspberry Pi Remote Desktop Connection to Windows 10/11.

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:

  1. Batch convert file formats.
  2. Rename batch and move files.
  3. Use one command output as the input of another command.
  4. Whenever an event occurs it starts the applications.
  5. 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.

  1. Login to “Raspberry Pi”.
  2. Then navigate to the directory where you wish to save the script, e.g. /home/paul/scripts.
  3. 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

Running a 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.

ALSO SEE: 14 Open Source and Free Python Scripts For Ethical Hacking.

3 Examples of Bash Script for Raspberry Pi

Bash Script Examples 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.

Automatic Backup

You can create automatic backups for example if you are running your own web server or Plex server. This can be done by scheduling your script or using Cron jobs.

Delete Data

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.

Custom projects

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.

Conclusion

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!

LEAVE A REPLY

Please enter your comment!
Please enter your name here