Thoughts on Tech

Bash Scripting

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. All of this will seem completely pointless and unrelatable to anyone not interested in the subject


########################################################################################
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
########################################################################################
Exit Only

echo $? — this will provide the exit code of the last command run; if the command was a success
you will get 0; anything else is a failure.

########################################################################################
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
########################################################################################
#!/bin/bash

for file madeupfolder/*.log
do
    tar -czvf $file.tar.gz $file
done
########################################################################################
#!/bin/bash
for cn in {1..11}
do
    echo $cn
    sleep 1
done

echo "We're out of the loop."
########################################################################################
find /etc/ -type f 2> /dev/null — find files in directory etc and output them to the screen; if there are 
any standard errors, redirect them to /dev/null (with this command you won't see any errors on the screen). 
########################################################################################
#!/bin/bash

echo "Please enter your name: "
read myname
echo "Your name is $myname"
########################################################################################
if [ $? -ne 0 ]
then 
	   echo "An error occurred. Please consult the appropriate log"
fi #This is a function
########################################################################################