Ah a new Ubuntu, what a breath of fresh air. What new here then? Not sure right now, the version that was installed from bootable usb is Ubuntu 20.04.2 LTS Focal Fossa.

On Ubuntu:

Some of the code used in this session is stored here, ssh was configured towards Github.

https://github.com/spawnmarvel/azure-arm-bash

Files

https://help.ubuntu.com/community/LinuxFilesystemTreeOverview

Main directories

Packages and Package Management

https://help.ubuntu.com/community/InstallingSoftware

Bash Scripting Tutorial

https://linuxconfig.org/bash-scripting-tutorial-for-beginners

Create a new file called task.sh containing all the above commands, each on a separate line. Once ready, make your new file executable using chmod command with an option +x. Lastly, execute your new script by prefixing its name with ./.

file.sh # create, touch
chmod +x file # make your new file executable using
./file # to execute it

Check the bash, type echo $SHELL

The result should be /bin/bash

There are various other shell interpreters available, such as Korn shell, C shell and more. From this reason, it is a good practice to define the shell interpreter to be used explicitly to interpret the script’s content.

File names and permission

By default any newly created file are not executable before permission is set.

chmod +x filename

The file extension on GNU/Linux systems mostly does not have any meaning apart from what we see with ls for example.

Relative vs absolute path

pwd # current location
cd / # root
ls # list files
cd /home/espen/snap/ # absolute path
cd ../test/ #relative path
pwd /home/espen/test
cd ../
pwd /home/espen

Simple backup script

# Use man command to display manual page of any desired command. 
man ls

echo "Performing bck"
tar -czf /home/espen/test_bck.tag.gz /home/espen/test
echo "Backup done"
tar: Removing leading `/' from member names

# Despite the message's informative nature, it is sent to stderr descriptor. In a nutshell, the message is telling us that the absolute path has been removed thus extraction of the compressed file not overwrite any existing files. 

Variables

#!/bin/bash
greeting="Welcome"
user=$(whoami) # command substitution. Meaning that the output of the whoami command will be directly assigned to the user variable. 
day=$(date +%A)
echo "$greeting mister $user, today it is $day"
echo "Bash version: $BASH_VERSION"
a=10 # assign var
b=2
echo $a
echo $[$a + $b] # stdout
tot=$(($a + $b)) # assign var a + b
echo $tot

Simple backup so far

#!/bin/bash

#This script is used to backup a user's home dir to /tmp
echo "Starting backup"
user=$(whoami)
input=/home/$user # /test
output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz

tar -czf $output $input
echo "Backup of $input completed. Details:"
ls -l $output

tar: Removing leading `/’ from member names
Despite the message’s informative nature, it is sent to stderr descriptor. In a nutshell, the message is telling us that the absolute path has been removed thus extraction of the compressed file not overwrite any existing files.

Functions

One tab right or 4 spaces

tar command

Moving on…

https://ss64.com/bash/find.html

#!/bin/bash

#This script is used to backup a user's home dir to /tmp
echo "Starting backup"
user=$(whoami)
# to much data here
# input=/home/$user
# using the test folder to simulate

input=/home/$user/test
output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz

# func total files in dir
function total_files {
    find $1 -type f | wc -l
}

# func total dirs
function total_directories {
    find $1 -type d | wc -l
}

tar -czf $output $input 2> /dev/null

echo -n "Files to include:"
total_files $input

echo -n "Dirs to include:"
total_directories $input

echo "Backup of $input completed. Details:"
ls -l $output

And the output:

Starting backup
Files to include:4
Dirs to include:2
Backup of /home/espen/test completed. Details:
-rw-rw-r– 1 espen espen 274 feb. 24 20:50 /tmp/espen_home_2021-02-24_205040.tar.gz

Remember, tar: Removing leading `/’ from member names
Now that we have a basic understanding of the output redirection we can eliminate this unwanted stderr message by redirecting it with 2> notation to /dev/null. Imagine /dev/null as a data sink, which discards any data redirected to it.

Side note…multiple args in function

# function multiple args
function mult_args {
    echo $1
    echo $2
}
var1="var1"
var2=2
mult_args $var1 $var2

Numeric and String Comparisons

The rest of the tutorial is at the repos…

https://github.com/spawnmarvel/azure-arm-bash/tree/master/bash-snippets