📘 PAPER 5 – OPERATING SYSTEM (UNIT 5 – SHELL PROGRAMMING & FILTER COMMANDS) university of allahabad

 

🔴 UNIT 5 – SHELL PROGRAMMING & FILTER COMMANDS


🟢 1. Introduction to Shell

✅ What is a Shell?

A Shell is a command-line interpreter that:

  • Accepts user commands

  • Executes them

  • Acts as an interface between user and OS


🔹 Types of Shells in Linux

ShellDescription
shBourne shell
bashBourne Again Shell (most used)
cshC shell
kshKorn shell
zshAdvanced shell

👉 Bash is most commonly used.


🟢 2. Shell Programming

✅ Definition

Shell programming is writing a script to automate tasks using shell commands.


🔹 Advantages

✔ Saves time
✔ Automates tasks
✔ Easy to write
✔ Used in system administration


🟢 3. Structure of Shell Script

#!/bin/bash echo "Hello World"

Explanation:

  • #!/bin/bash → Shebang line

  • echo → Print output


🔹 Running a Shell Script

chmod +x file.sh ./file.sh

🟢 4. Variables in Shell

User Defined Variable

name="Amit" echo $name

System Variables

$HOME $USER $PATH

🟢 5. Read Command

echo "Enter name:" read name echo "Hello $name"

🟢 6. Conditional Statements


🔹 if Statement

if [ $a -gt $b ] then echo "A is greater" fi

🔹 if-else

if [ $a -eq $b ] then echo "Equal" else echo "Not equal" fi

🔹 Case Statement

case $choice in 1) echo "One";; 2) echo "Two";; *) echo "Invalid";; esac

🟢 7. Looping Statements


🔹 for Loop

for i in 1 2 3 do echo $i done

🔹 while Loop

i=1 while [ $i -le 5 ] do echo $i i=$((i+1)) done

🟢 8. Command Line Arguments

echo "File name: $0" echo "First arg: $1" echo "Second arg: $2"

🟢 9. Filter Commands

Filter commands process text input and give output.


🔹 1. grep

Search text in file

grep "hello" file.txt

🔹 2. sed

Stream editor (search & replace)

sed 's/old/new/' file.txt

🔹 3. awk

Used for report generation

awk '{print $1}' file.txt

🔹 4. cut

Extract columns

cut -d ":" -f1 file.txt

🔹 5. sort

Sort data

sort file.txt

🔹 6. uniq

Remove duplicate lines

uniq file.txt

🟢 10. Pipes

Used to connect commands.

ls | grep ".txt"

🟢 11. Shell Keywords

KeywordUse
ifCondition
thenExecution
fiEnd if
forLoop
doLoop start
doneLoop end

🟢 12. Shell Script Example

Program: Check Even or Odd

echo "Enter number:" read n if [ $((n % 2)) -eq 0 ] then echo "Even" else echo "Odd" fi

📌 IMPORTANT EXAM QUESTIONS (UNIT 5)

Explain shell scripting
Write shell program for even/odd
Explain grep, sed, awk
✔ Explain pipes & filters
Write notes on shell variables
Explain case statement

Comments

Popular Posts