Useful Bash Scripting notes

From packets2photons
Jump to navigation Jump to search

Always start with a Shebang. This tells the system about how the code will be interpreted. For a bash script, I start with:

#!/bin/bash 

This will use the bash shell.

Running basic commands

You can run any command in bash by simply including it in a file. For example, create a file called test and insert the following contents:

#!/bin/bash 

date

Save it and give it execute permissions and then test the output. If you didn't want to run date, but put the output in a variable. You can do the following

thedate=$(date)

Processing text in bash

Often we don't quite get the exact output from a command that we would like. Firstly, the first run of top is often inaccurate and secondly, we may just want the number.

The following command will run top twice:

top -bn2

By adding | Grep Cpu, we can extract the two important lines:

top -bn2 | grep Cpu

You can append the following to the previous line to remove the first line:

| awk '{if(NR>1)print}'

Perhaps we want to also isolate the CPU temp as just a number. So from the following:

%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

We want to process the text such that we are just provided with the number emphasised in bold. Lets do this in steps.

CPUtemp=$(top -bn2 | grep Cpu)
echo $CPUtemp

You can remove all blank spaces with:

CPUtemp=${CPUtemp//blank:/}

You can remove text before a value, in this case a :, by matching on a certain value:

CPUtemp="${CPUtemp##*:}"

You can remove text after the first instance of a value, in this case the value is a ., with:

CPUtemp=${CPUtemp%%.*}

Loops

Bash has many different types of loops, lets introduce you to one here:

 #!/bin/bash

 count=1

 while [ $count -le 20 ] ; do
         ((count++))
         thedate=$(date)
         echo $thedate
 done

Note: With many other programming languages we would do simply count++ or count=count+1. In bash any variable can be an integer or a string. To do any arithmetic, you should use $((...)).

Take the following snippet as an example and, integrate the following while loop with your existing code. Do not progress until your code is simply printing a single temperature integer 20 times.

Conditionals

Integrate the following code sample:

 if [ $CPUtemp -gt 50 ] ; then
                echo "High load! $CPUtemp"
        else
                echo "Low load! $CPUtemp"
        fi

Loop over the CPU temperature 20 times. To create some CPU load try installing stress.

sudo apt install stress 

then:

stress --cpu 4

Ensure your program is functioning correctly by correctly assessing the CPU temp and stating "High load!" before moving on.

Logging =

Lets assume that you want to create a log file. We can create a string including the date. Integrate the following into your program.

logtext="High Load - ${CPUtemp} ${thedate}"
echo $logtext

Note that the variables are enclosed within {}.

You can append the following to log the exceptions to a local file.

echo $logtext >> cpuloadlog

The following statement would allow you to log to a remote server:

echo $logtext | ssh username@IPorDNSentry "cat >> /home/ubuntu/remotelogfile"

Note that for this to work you must have SSH keys installed SSH Keys et al

CRON

If this were a real script we would not loop 20 times. Remove the looping from your script and ensure that this runs every minute using CRON.

Challenge task

Look carefully at how books are organised the url and create a bash program to scrape as many clear text .txt books as you can.