Linux automation with a script

Submitted by peter on Wed, 12/21/2022 - 20:56

You can automate some things in Linux using scripts. Here is a quick example or two.

I am using Linux Mint with the Cinnamon user interface plus the Raspberry Pi OS on my Pi computers. For there examples, they are almost identical. Most distributions of Linux have similar file managers and text editors. Use whatever is on your computer. If you do not have Linux and want to experiment, an old notebook computer is a cheap and easy starting place. Just install Linux Mint.

Open a text editor. The one supplied in Linux Mint is named xed and appears in the menu as Text Editor. Programmers would already be using Bluefish or equivalent.

Type, or copy and paste, the following lines into the text file. Save the file in your home directory with the name hello.

#!/bin/bash

echo "Hello."

In File Manager, select the file named hello, right click the file, then select Permissions. Look for the box in the line Execute [] Allow executing file as a program. Click the box to switch on the permission then select Close.


Open terminal.

Type in the following command.

./hello 

Here is the result on my notebook.


 

There are lots of things you can put in a script. My main use is to add stuff and change settings after a default install of Linux. I also like to rename files to fit a single standard. Showing you all the bits in one big script would be too hard to explain. Lets look at some more small examples.

You might like to record disk space before and after a big change. The following script displays disk space.

#!/bin/bash

echo "df -h"

df -h

The result will be something like this:

./disk_space

df -h

Filesystem      Size  Used Avail Use% Mounted on

tmpfs           1.6G  2.1M  1.6G   1% /run

/dev/nvme0n1p2   58G   13G   42G  24% /

tmpfs           7.7G     0  7.7G   0% /dev/shm

tmpfs           5.0M  4.0K  5.0M   1% /run/lock

/dev/nvme0n1p1  500M  5.3M  494M   2% /boot/efi

/dev/nvme0n1p7  411G  370G   20G  95% /home

tmpfs           1.6G  112K  1.6G   1% /run/user/1000

What do the lines in the script mean?

The line #!/bin/bash tells the command line processor how to interpret the lines. There are other options. All the scripts I see use this one standard.

The line echo "df -h" places a comment in the output. Use echo to document the script in a way that is readable when the script runs.

If you want a comment just for the file without displaying in the output, use the following form.

#!/bin/bash

# Display disk space usage.

df -h

The line df -h is the actual command. You can test commands by typing them direct into the Terminal window. Type df -h into the command line now then press enter. Practice commands in Terminal before adding them to a script.

How do you record the results for later viewing?

When you run lots of steps in a script, you meed a log to keep track of what happened. The following script writes things to a file named update.log.

#!/bin/bash

echo "df -h"

echo "df -h" >> update.log

df -h > update.log

Here is another way to write the same log. The tee part writes to both the screen and the log file which saves you duplicating the command.

#!/bin/bash

echo "df -h" | tee -a update.log

df -h > update.log

Keep track of time

Here is the disk space display with a recording of the time used. In this case, there will be almost zero seconds. Some of your bigger scripts might run for minutes or hours.

#!/bin/bash

s=$SECONDS

echo "df -h" | tee -a update.log

df -h > update.log

echo "df used $(( SECONDS - s )) seconds." | tee -a update.log

Clarity

Here is the disk space display with new lines added to increase the clarity of both the display and the log. Some commands start or end with new lines while others do not. You can space out the display and log with extra lines for clarity.

#!/bin/bash

s=$SECONDS

echo "df -h

" | tee -a update.log

df -h > update.log

echo "df used $(( SECONDS - s )) seconds.

" | tee -a update.log

Update with purge and clean

Here is a longer script to update packages the same as Update Manager does automatically. But you can run this script any time including in the middle of a reconstruction or a fresh install. Plus this script purges files no longer needed and cleans stuff out, all in one hit, saving you potentially gigabyes of disk space. This is useful when using the tiny SSDs in old notebooks or the tiny microSD cards often use in Raspberry Pi computers.

Notice how the lines are spaced out for easy reading. There are extra lines, echo "" | tee -a update.log, just to insert the right spacing in the display and log. Some of the lines have extra spaces to align similar parts for easy reading. The command line ignores extra spaces when they are outside of specific values. Feel free tp make your scripts spacious and readable. Comments cost nothing during processing and save minutes when you read the script before use.

#!/bin/bash



# Purge and clean out unused packages.

echo "Purge and clean out unused packages. Start.

" | tee -a update.log



# autoremove is used to remove packages that were installed to satisfy dependencies

# for other packages and are now no longer needed.

echo "apt autoremove -y" | tee -a update.log

sudo  apt autoremove -y  | tee -a update.log

echo "" | tee -a update.log



echo "apt autoremove --purge -y" | tee -a update.log

sudo  apt autoremove --purge -y  | tee -a update.log

echo "" | tee -a update.log



# Clear out the local repository of retrieved package files.

echo "apt-get clean" | tee -a update.log

sudo  apt-get clean  | tee -a update.log

echo "" | tee -a update.log



echo "Purge and clean out unused packages. End." | tee -a update.log


I ran the script a few times as a test for consistency. The following screenshot shows no changes because they were performed in a previous test.


 

The next example is the log from the script.

Purge and clean out unused packages. Start.



apt autoremove -y

Reading package lists...

Building dependency tree...

Reading state information...

0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.



apt autoremove --purge -y

Reading package lists...

Building dependency tree...

Reading state information...

0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.



apt-get clean



Purge and clean out unused packages. End.

Experiment

The best time to learn how to use scripts is before you a rushing to do something that can benefit from a script. Experiment. Have fun. Everything on this page should be safe. Look up options for the commands. read about other commands.

Learn

Scripts can include other files to build up one big script from lots of little scripts. Scripts can test for things before making changes. You have if, then, else, and loops. Everything you need to find out if something exists before you attempt to change it or install it or delete it.

You can build scripts to do small steps then use those components in scripts to build machines for specific uses. I have one for a file server build on about ten component scripts.

Automate

The big step you have to take with new hardware is transferring your existing configuration to a new machines. Multiply that work when you are adding several Raspberry Pi computers as file servers, media servers, irrigation system controllers, and everything else. Scripts can handle heaps of repetitive work.