Table of contents
Other If Statement
If the output is either Monday or Tuesday
if [ "$a" = Monday ] || [ "$a" = Tuesday ]
Test if the error.txt file exist and its size is greater than zero
if [ $? -eq 0 ] // If input is equal to zero (0)
if [ -e /export/home/filename ] //If file is there
if [ "$a" != "" ] //If variable does not match
if [ error_code != "0" ] //If file not equal to zero (0)
Comparisons:
-eq equal to for numbers
\== equal to for letters
-ne not equal to
!== not equal to for letters
-lt less than
-le less than or equal to
-gt greater than
-ge greater than or equal to
File Operations:
-s file exists and it not empty
-f file exists and is not a directory
-d directory exists
-x file is executable
-w file is writable
-r file is readable
Shell script programs
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to create multiple files with different names.
# Date Modified: 02/26/2024
echo
for i in {1..10}
do
touch shounak_$i
done
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to create multiple file upon input
# Date Modified: 02/26/2024
echo
echo "How many files do you want?"
read t
echo
echo "Files name should start with?"
read n
for i in $(seq 1 $t)
do
touch $n.$i
done
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to built simple counting.
# Date Modified: 02/26/2024
echo
for i in {1..25}
do
sleep 1
echo $i
done
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to assign write permission to files
# Date Modified: 02/26/2024
echo
for i in Shounak*
do
echo Assigning write permission to $i
chmod a+w $i
sleep 1
done
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to assign write permissions to files with total time it will take
# Date Modified: 02/26/2024
echo
total=`ls -l shounak* | wc -l`
echo "It will take $total seconds to assign permissions..."
echo
for i in shounak*
do
echo Assigning write permission to $i
chmod a+w $i
sleep 1
done
do-while Scripts
- do while The while statement continually executes a block of statements while a particular condition is true or met
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to run for a number of seconds
# Date Modified: 02/26/2024
echo
count=0
num=10
while [ $count -lt 10 ]
do
echo
echo $num seconds left to stop this process $1
echo
sleep 1
num=`expr $num - 1`
count=`expr $count + 1`
done
echo
echo $1 process is stopped!!!
echo
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to run for a number of times
# Date Modified: 02/26/2024
c=1
while [ $c -le 5 ]
do
echo "Welcome $c times"
((c++))
done
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script will look at your current day and tell you the state of the backup.
# Date Modified: 02/26/2024
echo
NOW=$(date +"%a")
case $NOW in
Mon)
echo "Full backup";;
Tue|Wed|Thu|Fri)
echo "Partial backup";;
Sat|Sun)
echo "No backup";;
*) ;;
esac
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to check other if statement.
# Date Modified: 02/26/2024
echo
echo Please chose one of the options below
echo
echo 'a = Display Date and Time'
echo 'b = Lsit file and directories'
echo 'c = List users logged in'
echo 'd = Check the System uptime'
echo
read choices
case $choices in
a) date;;
b)ls;;
c)who;;
d)uptime;;
*)echo Invalid choice - Bye.
esac
Exit Status
0 = OK or Successful
1 = Minor problem
2 = Serious trouble
3-255 = Everything else
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to check other if statement.
# Date Modified: 02/26/2024
for i in {1..5}
do
rm $i
done
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to check other if statement.
# Date Modified: 02/26/2024
i=1
for username in `awk -F: '{print $1}' /etc/passwd`
do
echo "Username $((i++)) : $username"
done
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to check other if statement.
# Date Modified: 02/26/2024
ls -l /home/oracle/hello
if [ $? -eq 0 ]
then
echo File exist
else
echo Files does not exist
fi
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to check other if statement.
# Date Modified: 02/26/2024
echo hello
echo $?
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to check other if statement.
# Date Modified: 02/26/2024
echi hello
echo $?
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to check other if statement.
# Date Modified: 02/26/2024
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to check other if statement.
# Date Modified: 02/26/2024
for i in khulape repo shell scripting
do
echo See Shounak $i
done
#!/bin/bash
# Date Created: 02/26/2024
# Author: Shounak Khulape
# Description: This script is to check other if statement.
# Date Modified: 02/26/2024
i=1
for day in Mon Tue Wed Thu Fri
do
echo "Weekday $((i++)) : $day"
done
That's great if you have make till here you have covered Basic Shell Scripting Practice Hands-On.
If you liked what you read, do follow and any feedback for further improvement will be highly appreciated!
Thank you and Happy Learning!👏