Paul Sutton

Bashscripting

Weather logging

So been experimenting with some of the options for ansiweather, partly as schools need to teach data logging as part of the national curriculum computing, however not all schools have the kit required.

As I have this running on a Raspberry Pi, I thought it may be possible to set one up to log just the location and temperature.

ansiweather -l paignton -p false -h false  -w false -i false >temponly.txt

so the above will display the location and temperature, but exclude pressure, humidity, wind and UV index.

now=$(date +"%m_%d_%Y")
echo "Filename : /nas/backup_$weather.csv"
echo $(date "+ %D, %T"), $(ansiweather -l Paignton -a false) >> weather_$now.csv

Control what is sent to stdout.

now=$(date +"%m_%d_%Y")
echo "Filename : /nas/backup_$weather.csv"
echo $(date "+ %D, %T"), $(ansiweather -l torquay -p false -h false  -w false -i false) >> torquay_$now.csv

Final script

now=$(date +"%m_%d_%Y")
echo "Filename : /nas/backup_$weather.csv"
echo $(date "+ %D, %T"), $(ansiweather -l torquay -p false -h false  -w false -i false) >> torquay_$now.csv

Tags

#Bash,#BashScripting,#Logging,#Data


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Logging the weather

I have modified the script that runs every 10 minutes to record the output from the ansiweather utility, This still saves the output to a file but adds the current date to the file name.

now=$(date +"%m_%d_%Y")
echo "Filename : /nas/backup_$weather.csv"
echo $(date "+ %D, %T"), $(ansiweather -l Paignton -a false) >> weather_$now.csv

So the output looks like

 Humidity: 71% - Pressure: 1023 hPa
06/09/25, 14:20:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 1.79 m/s WSW - Humidity: 69% - Pressure: 1024 hPa
06/09/25, 14:30:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 2.24 m/s SW - Humidity: 70% - Pressure: 1023 hPa
06/09/25, 14:40:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 3.13 m/s W - Humidity: 69% - Pressure: 1023 hPa
06/09/25, 14:50:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 1.79 m/s SW - Humidity: 69% - Pressure: 1023 hPa
06/09/25, 15:00:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 1.79 m/s WSW - Humidity: 71% - Pressure: 1023 hPa
06/09/25, 15:10:01, Weather in Paignton: 18 °C - UVI: 7.03 - Wind: 1.34 m/s SSW - Humidity: 71% - Pressure: 1024 hPa

As this runs on a headless pi4, I can login and copy the data files locally before uploading to my gopherspace (Look it up)

gopher://vern.cc/1/~zleap

Tags

#Bash,#BashScripting,#Data,#Logging,#Gopher,#RaspberryPi, #Weather


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash Shortcuts

This question was asked on the Linux Mint Matrix Support recently, I helped by looking up what was needed, and posting this as a reply, As this is also really useful and worth sharing more widely too.

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash,#BashScripting,#Bashscripting,#Keyboard,#Shortcuts


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

File integrity

If providing downloadable files, it is important to ensure that the downloaded file is the same as that provided on the host server, this ensures the file has not been tampered with in some way. The end user needs a way to check what they have downloaded is safe.

One way to do this is to create a file checksum with md5sum

let's say we have two files

-rw-rw-r-- 1 psutton psutton  67200 May  3 19:42 jcr.pdf
-rw-rw-r-- 1 psutton psutton 433231 Apr 28 17:50 southwest1.png

We want to create a checksum for both files. To do this we can run

md5sum southwest1.png 
502d4f9dacab9240f2622c032e6ea11f  southwest1.png
md5sum jcr.pdf 
b98edf4dd0afd0ede8ac6dabce66f522  jcr.pdf

This sends the checksum to stdout

So need a file to store this

md5sum jcr.pdf southwest1.png > md5sum2

We can then view the contents if this with

as previously discussed the > directs output to a specified file

cat md5sums
502d4f9dacab9240f2622c032e6ea11f  southwest1.png
b98edf4dd0afd0ede8ac6dabce66f522  jcr.pdf

In this example, we listed the files we want to create a checksum for.

If we were to add a 3rd file say tux.png then we can do this and append the new checksum with

md5sum tux.png >> md5sums

Then example our new md5sums file

cat md5sums
502d4f9dacab9240f2622c032e6ea11f  southwest1.png
b98edf4dd0afd0ede8ac6dabce66f522  jcr.pdf
d52cd5e7164eccd6da50e10d5a9c3dbf  tux.png

So now that we have 3 files and a md5sums file, we can check all the files listed

md5sum -c md5sums
southwest1.png: OK
jcr.pdf: OK
tux.png: OK

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash,#BashScripting,#Bashscripting,#md5sums,#md5sum,#file, #integrity,#check


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Fun bash stuff

While these are not bash commands, there are a few fun tools.

One is fortune, this displays a random quote on the console or terminal.

fortune
Many pages make a thick book, except for pocket Bibles which are on very
very thin paper.

Another is cowsay which takes an argument and prints a picture of a cow (there are options to change how it looks or to other characters)

cowsay hello
 _______
< hello >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

As with other commands these can be combined

fortune | cowsay
 ________________________________________
/ I don't know half of you half as well  \
| as I should like; and I like less than |
| half of you half as well as you        |
| deserve.                               |
|                                        |
\ -- J. R. R. Tolkien                    /
 ----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Options

cowsay -f tux hello
 _______
< hello >
 -------
   \
    \
        .--.
       |o_o |
       |:_/ |
      //   \ \
     (|     | )
    /'\_   _/`\
    \___)=(___/

cowsay -f daemon hello
 _______
< hello >
 -------
   \         ,        ,
    \       /(        )`
     \      \ \___   / |
            /- _  `-/  '
           (/\/ \ \   /\
           / /   | `    \
           O O   ) /    |
           `-^--'`<     '
          (_.)  _  )   /
           `.___/`    /
             `-----' /
<----.     __ / __   \
<----|====O)))==) \) /====
<----'    `--' `.__,' \
             |        |
              \       /
        ______( (_  / \______
      ,'  ,-----'   |        \
      `--{__________)        \/

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash.#BashScripting,#Bashscripting,#Cowsay,#Fortune


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Removing users

To remove a user, you can issue the following, as we are no longer setting up a gopher server on the pi, I will just remove the user.

sudo deluser gopher

Which will remove the user, but not that users /home directory, which for the most part is fine if you want to keep hold of the user account data.

For more information and options, consult the man page

man deluser

Chat

I am on the Devon and Cornwall Linux user group mailing list and also their matrix channel as zleap, it is better to ask there, that way others can answer too.

Tags

#Bash,#Bashscripting,#BashScripting#users,#removing


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash scripting 24 – firewalls

I am still experimenting with gopher, however this got me thinking about system security, so I have installed.

If you are not sure what you are doing with this, please consult the man page

man ufw

So this describes ufw as

ufw - program for managing a netfilter firewall

As I am ssh'd in to the pi, then the firewall needs to be enabled, however, first make sure that that port 22 (or which ever port you are using for ssh) is allowed. I have to do this as being logged in via ssh, emabling the firewall could block ssh preventing access.

sudo ufw allow 22

Then check the status

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere                  
22 (v6)                  ALLOW       Anywhere (v6)             

To confirm that port 22 is allowed, then enable

sudo ufw enable

Links

Tags

#Bash,#Bashscripting,#BashScripting#Security,#Firewall


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash scripting 23 – users

To add new users to a system, we can use the command. You will need to have root privileges for this, so you need to prefix with sudo, e.g.

**Permissions, the initial user you create when setting up the pi, has sudo or admin privileges. Additional users will not. Unless you take steps to give that user those permissions.**

sudo useradd <user>

The rest of the process is interactive, in this example I am adding the user gopher to the raspberry Pi4 system we are also using for testing cron.

sudo adduser gopher
Adding user `gopher' ...
Adding new group `gopher' (1001) ...
Adding new user `gopher' (1001) with group `gopher (1001)' ...
Creating home directory `/home/gopher' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for gopher
Enter the new value, or press ENTER for the default
	Full Name []: Gopher
	Room Number []: 0
	Work Phone []: 0
	Home Phone []: 0
	Other []: 0
Is the information correct? [Y/n] y
Adding new user `gopher' to supplemental / extra groups `users' ...
Adding user `gopher' to group `users' ...

I can now login to the user gopher on the Raspberry Pi with

$ ssh gopher@192.168.1.202
gopher@192.168.1.202's password: 
Linux raspberrypi 6.12.20+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.12.20-1+rpt1~bpo12+1 (2025-03-19) aarch64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
gopher@raspberrypi:~ $ 

To change the password on a GNU/Linux system, use

passwd

To delete a user, use the userdel command, I am not going into this here, so as with other commands, there is a man page

man userdel
userdel - delete a user account and related files

Tags

#Bash,#Bashscripting,#BashScripting#users,#adding


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash scripting 22 – passwords

Having a secure password is really important, but thinking of one can be difficult, you can use three random words such as

CanBookBoard2024%

I have added some numbers and a special symbol here as some websites will insist on more than just upper and lower case letters.

Tbankfully bash has a tool for this

apg - generates several random passwords

You can find more information in the man page

man apg

The example below

apg -m 15 -M SNC

which produces

thuhuHersyoywik2
SindEjpiTrildaf3
Biabodke0skyudha
JeKneapFelu5gDom
neHydishGijcis02
NefNan2JeymDabas

So looking at this

-m 15 – tells apg min password length should be 15 characters -M SNC

Forces apg to use (you need SNC in capitals to force this) * special symbols e.g: %^&* * numerical characters, e.g 1234 * capital letters

Using -M SNC will give the same result.

To change the password on a GNU/Linux system use

passwd

Tags

#Bash,#Bashscripting,#BashScripting#apg,#Security,#Password,#Generation


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay

Bash scripting 21 – cron

One of the important things you need to do as a system admin is run tasks that perform specific tasks on the system. This is fine when you are sitting in front of the computer, or logged in remotely, but what happens if you need to run a task when you are not there, or you just want to automate the running of these tasks.

You can schedule tasks using the cron command. There is an easy-to-use tutorial for this at [1]. I am not going to reproduce this here, but may include some of my own specific use cases in a later post.

According to [1] the following

*/5  * * * * /home/paul/crontest.sh

runs every 5 minutes, where as

*/10  * * * * /home/paul/crontest.sh

So putting ls -l in crontest.sh will run that command every 5 or 10 minutes.

However, I am logged into my pi (where I am running this) so the script will run, but I don't see any output.

If I add in ls -l > dirlist.txt

It will produce a directory listing text file when run, by running ls manually you can see it in the listing.

I have a post on Ansiweather scheduled fora few days, in the meantime

putting this in the crontest.sh file

echo $(date), $(ansiweather -l Paignton -a false) >> weather.csv

It will run at the specified time and append the information to weather.csv which could be a good way to monitor weather over time.

This can be examined with

cat weather.csv 

if you use the watch command

watch cat weather.csv

the contents of weather.csv will be displayed and updated in real time as the file is appended with new information.

References

  1. https://stackoverflow.com/questions/12786410/run-cron-job-every-n-minutes-plus-offset

Tags

#Bash,#Bashscripting,#Task,#Schedule,#cron,#crontab,#Admin, #Automation


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay