Shebang - The first line is called shebang. It tells the script which interpreter it should use. Even if the shell is not bash, it will be executed under bash with the help of shebang.
#!/bin/bash
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Variables
var1="This is how you create a variable. Keep in mind to not leave any space in front and back of equal to sign\n"
echo $var1 # This is how you print the content of the variable
var2="Akash"
echo "You cannot use single spaces in echo if you want to print wants inside a variable, $var2 \n"
echo 'Lets use single quotes and you will see that it does not print whats inside variable, $var2 \n'
var3 = $(command) # this stores the output of a command
var3=$(ls)
echo "$var3"
Predefined Variables - date
time=$(date)
echo "The system time and date is: $time \n"
Default variables (type env to find the environment variables)
echo "Your username is: $USER \n"
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Performing Basic Math
expr 30 + 10
echo "Addition expr 30 + 10 \n"
expr 30 - 10
echo "Subtraction expr 30 - 10 \n"
expr 30 / 10
echo "Division expr 30 + 10 \n"
expr 30 \* 10
echo "Multiplication expr 30 \* 10, you have to escape asterisk as it is wild card in linux \n"
echo "Math using variables, echo \$num1 + \$num2"
num1=100
num2=200
expr $num1 + $num2
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If statements
mynum=200
# eq stands for equal to
if [ ! $mynum -eq 200 ]
then
echo "The condition is true"
else
echo "The variable is equal to 200"
fi
# ne stands for not equal to, gt stands for greater than, lt stands for less than
if [ $mynum -ne 200 ]
then
echo "The condition is true"
else
echo "The variable is equal to 200"
fi
# -f checks the existence of a file, if its directory that you want to search for, then change it to -d
if [ -f ~/myfile ]
then
echo "The file exists"
else
echo "The file does not exist"
fi
# to check if a command exists or not
command1=/usr/bin/find
if [ -f $command1 ]
then
echo "$command1 is available to use"
else
echo "$command1 is NOt available to use, lets install it"
# sudo apt update && sudo apt install -y find, -y is used to skip or bypass prompts
fi
$command1
# Easier way to check for command existence
command2=find
if command -v $command2 # brackets are only used if we are running the test command, even though we are not using it directly, it runs in default, do man test in terminal to understand more about it, the command variable in the if statement checks for the commands
then
echo "$command2 is available to use"
else
echo "$command2 is NOt available to use, lets install it"
# sudo apt update && sudo apt install -y find, -y is used to skip or bypass prompts
fi
$command2
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Exit Codes, 0 output means successfull and anything else means the command failed, we can get the exit code by $?
package=find
sudo apt install $package
echo "The exit code for the package install is $?"
var3="hi"
echo "$var3"
echo "The exit code for the var3 is $?"
package=akash
$package
echo "The exit code for the package is $?" # The exit code will be something other than 0 as there is no package names akash.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
while loop
# -le is less than or equal to
myvar=1
while [ $myvar -le 2 ]
do
echo $myvar
myvar=$(( $myvar +1 ))
sleep 0.01
done
while [ -f ~/testfile ]
do
echo "As of $(date), the test file exists"
done
echo "As of $(date), the test file has gone missing"
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
For loops
for current_number in 1 2 3 4
do
echo $current_number
sleep 0.01
done
echo "This is outside of the for loop 1"
for current_number in {1..4}
do
#echo $current_number
sleep 0.01
done
echo "This is outside of the for loop 2"
for file in logfiles/*.log
do
tar -czvf $file.tar.gz $file
done
# /dev/null is a place where you can send anything and it will dissapear forever, its like a black hole, so the reason of the below command is we want to view everything except the ones that are giving errors so 2 stands for erros and we are redirecting all the errors to /dev/null after which it will dissapear
find /etc -type f 2> /dev/null
# the below command will only print out errors
find /etc -type f > /dev/null
find /etc -type f 1> /dev/null
find /etc -type f 1> file.txt
# 1 stands for standard output and 2 stands for standard error
# With the below command, we can send the standard output and standard error at once to the file
find /etc -type f &> file.txt
find /etc -type f 1>error_result.txt 2>success_result.txt
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Functions
# Update function
release_file=/etc/os-release
logfile=/var/log/updater.log
errorlog=/var/log/updater_errors.log
check_exit_status() {
if [ $? -ne 0 ]
then
echo "An error occurred, please check the $errorlog file"
fi
}
if grep -q "Arch" $release_file
then
# The host is based on Arch, run the pacman update command
sudo pacman -Syu 1>>$logfile 2>>$errorlog
check_exit_status
fi
if grep -q "Pop" $release_file || grep -q "Ubuntu" $release_file
then
# The host is based on Ubuntu
# Run the apt version of the command
sudo apt update 1>>$logfile 2>>$errorlog
check_exit_status
sudo apt dist-upgrade -y 1>>$logfile 2>>$errorlog
check_exit_status
fi
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Case Statements
finished=0
while [ $finished -ne 1 ]
do
echo "What is your favourite Linux Distribution?"
echo "1 - Arch"
echo "2 - CentOS"
echo "3 - Debian"
echo "4 - Mint"
echo "5 - Ubuntu"
echo "6 - Something else..."
echo "7 - Exit the script"
read distro;
case $distro in
1) echo "Arch is a rolling release";;
2) echo "CentOS is popular on servers";;
3) echo "Debian is a community distribution";;
4) echo "Mint is popular on desktops and laptops";;
5) echo "Ubuntu is popular on both servers and computers";;
6) echo "There are many distributions out there";;
7) finished=1 ;;
*) echo "You didnt enter an appropriate choice"
esac
done
echo "Thank You for using the script"
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Scheduling Jobs
# sudo apt install at
# at is used to run a script at a later time
logfile=job_results.log
echo "The script ran at the following time: $(date)" > $logfile
# at 15:32 -f ./script.sh
# the below command will show what jobs are in queue
atq
# the below command will remove the job, we just have to specify the job number
atrm 3
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Arguments
echo "You have entered the arguments: $1 $2 $3 $4 $5"
lines=$(ls -lh $1 | wc -l)
# "$#" represents the number of arguments the user has passed to the script
if [ $# -ne 1 ]
then
echo "This script requires exactly one directory path passed to it"
echo "Please try again"
exit 1
fi
echo "You have $(($lines-1)) objects in the $1 directory"