0% found this document useful (0 votes)
3 views10 pages

Shell Programming Practical File

The document outlines a syllabus for Shell Programming with bash, detailing essential topics such as shell introduction, pipes, variables, control structures, and debugging. It includes 20 example programs demonstrating various concepts, along with their outputs. Additionally, it provides instructions on running shell programs and highlights important keywords related to shell scripting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Shell Programming Practical File

The document outlines a syllabus for Shell Programming with bash, detailing essential topics such as shell introduction, pipes, variables, control structures, and debugging. It includes 20 example programs demonstrating various concepts, along with their outputs. Additionally, it provides instructions on running shell programs and highlights important keywords related to shell scripting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Yes — according to your syllabus, Shell Programming (with bash) should include programs

on:

 shell introduction
 pipes and redirection
 running shell scripts
 meta characters
 file name substitution
 shell variables
 command substitution
 environment
 quoting
 test command
 control structures
 arithmetic in shell
 functions
 interrupt processing
 debugging shell scripts

Below are 15 shell programming programs with output as per syllabus.

Shell Programming Programs with Output


1. Simple shell script to print a message
#!/bin/bash
echo "Welcome to Shell Programming"
echo "Operating System Lab"

Output

Welcome to Shell Programming


Operating System Lab

2. Shell script using variables


#!/bin/bash
name="Aman"
course="MCA"
echo "Student Name: $name"
echo "Course: $course"

Output
Student Name: Aman
Course: MCA

3. Taking input from user


#!/bin/bash
echo "Enter your name:"
read name
echo "Enter your age:"
read age
echo "Hello $name, your age is $age"

Sample Output

Enter your name:


Rahul
Enter your age:
21
Hello Rahul, your age is 21

4. Arithmetic operations in shell


#!/bin/bash
a=20
b=10
sum=$((a+b))
sub=$((a-b))
mul=$((a*b))
div=$((a/b))
echo "Sum = $sum"
echo "Subtraction = $sub"
echo "Multiplication = $mul"
echo "Division = $div"

Output

Sum = 30
Subtraction = 10
Multiplication = 200
Division = 2

5. Using test command for checking greater number


#!/bin/bash
echo "Enter first number:"
read a
echo "Enter second number:"
read b
if [ $a -gt $b ]
then
echo "$a is greater"
else
echo "$b is greater"
fi

Sample Output

Enter first number:


12
Enter second number:
7
12 is greater

6. Even or odd number using if-else


#!/bin/bash
echo "Enter a number:"
read n

if [ $((n % 2)) -eq 0 ]


then
echo "$n is Even"
else
echo "$n is Odd"
fi

Sample Output

Enter a number:
15
15 is Odd

7. For loop program


#!/bin/bash
for i in 1 2 3 4 5
do
echo "Number: $i"
done

Output

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
8. While loop program
#!/bin/bash
i=1
while [ $i -le 5 ]
do
echo "Value of i = $i"
i=$((i+1))
done

Output

Value of i = 1
Value of i = 2
Value of i = 3
Value of i = 4
Value of i = 5

9. Case statement program


#!/bin/bash
echo "Enter a character:"
read ch

case $ch in
[a-z]) echo "Lowercase alphabet" ;;
[A-Z]) echo "Uppercase alphabet" ;;
[0-9]) echo "Digit" ;;
*) echo "Special character" ;;
esac

Sample Output

Enter a character:
A
Uppercase alphabet

10. Command substitution


#!/bin/bash
today=$(date)
current_dir=$(pwd)
echo "Today's date is: $today"
echo "Current directory is: $current_dir"

Sample Output
Today's date is: Tue Apr 22 10:30:15 IST 2025
Current directory is: /home/user

11. File existence check


#!/bin/bash
echo "Enter file name:"
read file

if [ -f "$file" ]
then
echo "File exists"
else
echo "File does not exist"
fi

Sample Output

Enter file name:


[Link]
File exists

12. Redirection and pipes


#!/bin/bash
echo "This is shell programming" > [Link]
echo "Linux and OS lab" >> [Link]
cat [Link] | sort

Output

Linux and OS lab


This is shell programming

13. Function in shell script


#!/bin/bash
sum() {
s=$(( $1 + $2 ))
echo "Sum = $s"
}

sum 12 8

Output

Sum = 20
14. Quoting example
#!/bin/bash
name="Shell"

echo 'Welcome to $name Programming'


echo "Welcome to $name Programming"

Output

Welcome to $name Programming


Welcome to Shell Programming

15. Debugging shell script


#!/bin/bash
set -x
a=5
b=3
c=$((a+b))
echo "Sum = $c"
set +x

Output

+ a=5
+ b=3
+ c=8
+ echo 'Sum = 8'
Sum = 8
+ set +x

Extra important syllabus-based programs


16. Environment variable display
#!/bin/bash
echo "Home Directory: $HOME"
echo "User Name: $USER"
echo "Shell Name: $SHELL"
echo "Present Working Directory: $PWD"

Sample Output
Home Directory: /home/student
User Name: student
Shell Name: /bin/bash
Present Working Directory: /home/student

17. Filename substitution using wildcard


#!/bin/bash
echo "List of .txt files:"
ls *.txt

Sample Output

List of .txt files:


[Link]
[Link]
[Link]

18. Menu-driven calculator


#!/bin/bash
echo "Enter first number:"
read a
echo "Enter second number:"
read b
echo "1. Add"
echo "2. Subtract"
echo "3. Multiply"
echo "4. Divide"
echo "Enter choice:"
read ch

case $ch in
1) echo "Sum = $((a+b))" ;;
2) echo "Difference = $((a-b))" ;;
3) echo "Product = $((a*b))" ;;
4) echo "Quotient = $((a/b))" ;;
*) echo "Invalid choice" ;;
esac

Sample Output

Enter first number:


20
Enter second number:
4
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice:
4
Quotient = 5

19. Signal handling / interrupt processing


#!/bin/bash
trap "echo 'Interrupt signal received'; exit" SIGINT

echo "Press Ctrl+C to stop"


while true
do
echo "Running..."
sleep 2
done

Sample Output

Press Ctrl+C to stop


Running...
Running...
Running...
^CInterrupt signal received

20. Factorial of a number


#!/bin/bash
echo "Enter a number:"
read n
fact=1
i=1

while [ $i -le $n ]
do
fact=$((fact*i))
i=$((i+1))
done

echo "Factorial = $fact"

Sample Output

Enter a number:
5
Factorial = 120

Best 15 shell programs for your practical file


If your teacher asks for 15 shell programs, use these:

1. Simple shell script


2. Variables
3. User input
4. Arithmetic operations
5. Greater number using test
6. Even/odd
7. For loop
8. While loop
9. Case statement
10. Command substitution
11. File existence check
12. Pipes and redirection
13. Function in shell
14. Quoting example
15. Signal handling / interrupt processing

How to run a shell program


Save program in a file, for example [Link]

chmod +x [Link]
./[Link]

or

bash [Link]

Viva important syllabus keywords


You should also remember these terms:

 #!/bin/bash → shebang line


 read → input from user
 echo → display output
 $var → variable value
 $(command) → command substitution
 > → overwrite file
 >> → append file
 | → pipe
 -f → file exists
 case → multiple choice control structure
 trap → interrupt/signal handling
 set -x → debugging

I can format all of this into a practical notebook style with Aim, Program, Output, and
Result.

You might also like