Monday, 12 May 2025

Bash scripting 1 to 8

These are just my notes while following along with this Youtube course on bash scripting, which is very good. The link can be found here: Bash Scripting course. I'll probably be updating it and adding comments as I move on to the more advanced lessons. 

======================

test=$(pwd) -- this is defining the command as a variable. 

echo $test -- this will return the output of the pwd command.

expr 12 \* 3 -- we use expr to do sums at the bash command line. In this example, the multiplication symbol (*) is escaped because it's a wildcard in bash. 

Addition would simply look like expr 12 + 3 

===================

#!/bin/bash

name ="Jonee"
now=$(date)

echo "Hallo, $name"
echo "Your system time and date is:"
echo "$now"
echo "Your username is: $USER"

==================
==================

#!/bin/bash

mynum=200

if [ $mynum -eq 200 ]
then
       echo "The condition is true."

else
       echo "This is what's returned when the condition is NOT true."

fi 

======================

#!/bin/bash

if [ -f ~/somefile ]
then
    echo "File exists in Home directory."
else
    echo "That file cannot be found."
fi

======================

#!/bin/bash

cmd=/usr/bin/htop

if [ -f cmd]
then
    echo "Found. Running..."
else
    echo "Not Found. Installing..."

sudo apt install cmd$

fi

$cmd

=====================

cmd=/usr/bin/htop

if  command -v  $cmd
then
    echo "Found. Running..."
else
    echo "Not Found. Installing..."

sudo apt install cmd$

fi

$cmd

======================

Exit Only

echo $? -- after the command; this will provide the exit code.

====

package=something
sudo apt install $package
echo "The exit code is: $?"

package=something
sudo apt install $package
if [ $? -eq 0]
then
    echo "The installation of $package was successful." 
else
    echo "$package installation failed. Exit code is $?"
fi

=====

package=something >> results.log
sudo apt install $package
if [ $? -eq 0]
then
    echo "The installation of $package was successful." 
else
    echo "$package installation failed. Exit code is $?" >> failure.log
fi

=================

#!/bin/bash

dir=/etc

if  [ -d $dir]
then
    echo "Directory exists."
else
    echo "Directory does not exist."
fi

echo "The exit code for this script run is $?"

//The above script is flawed since the exit code will be 0 even in the event of a failure. 

==================

#!/bin/bash

myvar=1

while [ $myvar -le 110 ]

do 

    echo $myvar

    myvar=$(( $myvar +3))

    sleep 0.5

done

=================

#!/bin/bash
while [ -f testfile ]
do

    echo "As of $(date) Testfile exists."
    sleep 5.6

done
echo "As of $(date) the file does NOT exist."

==================

#!/bin/bash
if [ -d /etc/dnf ]
then 
    sudo dnf update

fi

if [ -d /etc/pacman.d]
then 
    sudo pacman -Syu

fi

if [ -d /etc/apt ]
then 
    sudo apt update
    sudo apt dist-upgrade

fi

===================

#!/bin/bash

release_file=/etc/os-release

if  grep - q "Fedora" $release_file 
then
    sudo dnf update 
fi

if  grep -q "Mint" $release_file  || grep -q "Ubuntu" $release_file 

then
    sudo apt update
    sudo apt dist-upgrade
fi

========================

“Gigi” a go-go: LMDE 7

🐧Linux Mint has been the go-to distribution for users who want an out-of-the-box experience and an easy transition from Windows. It's w...