Shell Scripting
Shell Scripting
Shell Scripting
Shell Scripting
Power of linux command line
Disclaimer
Dear readers,
This document is provided by VIEH Group for educational purposes only.
While we strive for accuracy and reliability, we make no warranties or
representations regarding the completeness, accuracy, or usefulness of the
information presented herein. Any reliance you place on this document is at
your own risk. VIEH Group shall not be liable for any damages arising from
the use of or reliance on this document. We acknowledge and appreciate the
contribution of the source person.
also,
This document is not created by a professional content writer so any mistake
and error is a part of great design
Scan QR:
1. SHELL keywords
2. Arithmetic in SHELL script
3. Control Structures
i. Decision control
ii. Repetition control
4. More UNIX commands
5. Executing commands during login time
Handling shell variables:
The shell has several variables which are automatically set whenever you login. The
values
of some of these variables are stored in names which collectively are called your user
environment. Any name defined in the user environment, can be accessed from within a
shell script. To include the value of a shell variable into the environment you must export
it.
Suppose you have a shell variable called MY_VARIABLE with the value Hello World!, and
you
by want to use this variable in a shell script. You can access the value of a shell variable
putting a dollar sign ($) before the variable name.
Now, if you want to access the value of MY_VARIABLE within a shell script, you need
to export it:
This is where the shift command comes into play. It allows you to shift the positional
parameters, effectively "moving" them one position to the left. When you use shift, the
value of $2 becomes $1, the value of $3 becomes $2, and so on. The first positional
parameter $1 is lost in this process.
sum=0
sum=$((sum + arg))
done
Control Structures:
Every UNIX command returns a value on exit which the shell can interrogate. This value is
held in the read-only shell variable $? A value of 0 (zero) signifies success; anything other
than 0 (zero) signifies failure.
The test command in UNIX is used to check conditions and returns either a 0 (zero) if the
condition is true or a 1 (non-zero) if the condition is false. This result indicates success or
failure respectively.
1. Purpose of the test Command: The test command helps you evaluate conditions
in shell scripts. It checks whether a condition is true or false.
2. Exit Status: After running the test command, it sets its exit status based on whether
the condition is true or false. If the condition is true, the test returns a 0. If the
condition is false, it returns a non-zero value (typically 1).
3. Integration with if Statement: You can use the exit status of the test command
directly within an if statement. If the condition succeeds (exit status is 0), the
corresponding block of code within the if statement is executed. If the condition
fails (exit status is non-zero), the code within the else block (if present) is executed.
then
echo "$num1 is not eual to $num2"
fi
then
else
fi
else
fi
then
echo "$num1 is greater than or equal to $num2"
else
echo "$num1 is less than $num2"
fi
# Check if num1 is less than or equal to num2
$num2" else
fi
!= : not equal
-z : zero length string (i.e. string containing zero character i.e. null
Script:
#!/bin/bash
str2 empty_str=""
then
else
fi
then
else
fi
if test -z "$str1"
then
else
fi
if test -n "$str2"
then
else
fi
if test -z "$empty_str"
then
else
fi
Script:
#!/bin/bash
mydrive="/home/learn/Desktop/mydrive"
if test -f "$mydrive"
then
else
fi
if test -s "$mydrive"
then
Else
echo "The file $mydrive either does not exist or has zero size."
fi
if test -d "$mydrive"
then
fi
if test -r "$mydrive"
then
else
echo "The file $mydrive either does not exist or does not have read permission."
fi
if test -w "$mydrive"
then
else
echo "The file $mydrive either does not exist or does not have write permission."
fi
if test -x "$mydrive"
then
else
echo "The file $mydrive either does not exist or does not have execute permission."
fi
NOT
operators.
-a : logical AND
-o : logical OR
! : logical NOT
Script:
#!/bin/bash
# Check if the number is greater than 10 and less than 20(-a : logical AND)
if test "$num" -gt 10 -a "$num" -lt 20
then
echo "$num is greater than 10 AND less than 20"
fi
then
else
echo "$num is not less than 0 AND not greater than 100"
fi
then
else
fi
then
else
echo student is not available
fi
Explanation:
This lists who is currently logged on to the system and pipes the output through grep to
search for the username student.
The -s option causes grep to work silently and any error messages are directed to the file
/dev/null instead of the standard output.
If the command is successful i.e. the username student is found in the list of users
currently logged
in then the message student is logged on is displayed, otherwise the second message is
displayed.
while true; do
read choice
case $choice in
1)
;;
2)
echo "Calendar:"
cal
3)
ls -l
;;
4)
who
;;
5)
pwd
;;
6)
uname -a
;;
7)
df -h
;;
8)
ifconfig
;;
9)
ps aux
;;
10)
free -m
;;
11)
id
;;
12)
uname -r
;;
13)
du -h --max-depth=1 /
;;
14)
uptime
;;
15)
uptime -p
16)
dpkg --get-selections
;;
17)
echo "Hostname:"
hostname
;;
18)
netstat -tuln
;;
19)
df -T
;;
20)
;;
21)
break
;;
*)
;;
esac
done
Another Example:
Script:
#!/bin/bash
read response
case "$response" in
'done')
break
;;
"")
continue
;;
*)
eval "$response"
;;
esac
done
if [ -z "$1" ]; then
exit 1
fi
#grep -q "$1" "$file" searches for the keyword $1 in the file $file.
lines. #If the keyword is found in the file, grep exits with a success
status,
echo "$file"
fi
done
2)Write a shell script that takes a command line argument and reports whether it is
a directory, or a file or a link?
Script:
#!/bin/bash
if [ -z "$1" ]; then
echo "Please provide a filename or directory."
fi
if [ -d "$1" ]; then
exit 0
fi
if [ -f "$1" ]; then
exit 0
fi
if [ -L "$1" ]; then
exit 0
fi
directory? #!/bin/bash
if [ -z "$1" ]; then
exit 1
fi
count=0
if [ -d "$entry" ]; then
((count++))
fi
done
echo "Menu:"
if [ -e "$filename" ]; then
else
fi
else
fi
exit 0
else
fi
In this shell scripting lab, we covered the basics of writing Bash scripts, including user
input/output, conditional statements, loops, and file operations. We also explored
advanced topics such as process substitution and text processing. Through progressively
simplified examples, we demonstrated how to create a menu-driven program and
automate tasks efficiently.
Jai Hind!