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   
___________________________________________

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
___________________________________________

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
___________________________________________
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
___________________________________________

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. 
___________________________________________
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."

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

finished=0
while [ $finished -ne 1 ]
do

echo "What distro do you prefer?"
echo "1 - Arch"
echo "2 - Debian"
echo "3 - Debian/Ubuntu"
echo "4 - Red Hat/Fedora"
echo "5 - something else..."

read distro;

case $distro in

	1) echo "Arch is a rolling release.";;
	2) echo "Debian is a community release.";;
	3) echo "Lots of distros based on Ubuntu, which is based on Debian.";;
	4) echo "Red Hat is business based, but Fedora is a good community choice.";;
	5) echo "Well, it is all about choice.";;
	*) echo "You didn't enter an appropriate choice."

esac  #this is a case statement
done
___________________________________________
#!/bin/bash

lines=$(ls -lh $1 | wc -l)

if [ $# -ne 1 ]

then
    echo "This script requires that exactly one directory is passed to it."
    echo "Please try again."
    exit 1
fi

echo "You have $(($lines-1)) objects in the $ directory."
___________________________________________