Day - 1
1. Unix Operating System Structure:
The UNIX operating system is structured in a layered architecture, comprising several key
components that work together to provide a robust and efficient computing environment.
Below is a description of its structure:
1. Hardware Layer
This includes the physical components of the system:
1. CPU (Central Processing Unit)
2. Memory (RAM, Cache, Storage, etc.)
3. I/O Devices (Keyboard, Mouse, Hard Drive, Network, etc.)
The hardware executes machine-level instructions provided by the operating system.
2. Kernel (Core of UNIX OS)
The kernel is the heart of UNIX, responsible for managing system resources and
communication between hardware and software. It operates in the background and provides
essential services such as:
1. Process Management – Handles process creation, scheduling, and termination.
2. Memory Management – Allocates and manages system memory.
3. File System Management – Controls access to files and directories.
4. Device Management – Manages hardware devices via drivers.
5. Inter-Process Communication (IPC) – Allows processes to communicate and share
data.
6. System Calls Interface – Provides a bridge between user applications and the kernel.
3. Shell (Command Interpreter)
The shell acts as an interface between users and the kernel. It allows users to execute
commands, run scripts, and interact with the system. Popular UNIX shells include:
1. Bourne Shell (sh)
2. Bash (Bourne Again Shell)
3. C Shell (csh)
4. Korn Shell (ksh)
5. Z Shell (zsh)
4. File System (Hierarchical Structure)
UNIX organizes files in a hierarchical structure:
1. Root Directory (/) – The topmost directory in UNIX.
2. System Directories (/bin, /etc, /usr, /lib, etc.) – Contains system utilities, configuration
files, and shared libraries.
3. Home Directories (/home/username/) – Personal space for each user.
The file system follows a tree-like structure, where everything in UNIX (including hardware
devices) is represented as a file.
5. User Space (Applications & Utilities)
This layer consists of user applications and system utilities that interact with the kernel via
system calls. Examples include:
1. Text Editors – vim, pluma
2. Compilers – gcc, javac
3. Networking Tools – ping, ftp
4. System Utilities – ls, cp, mv, grep, ps, top
Users interact with UNIX through these applications, either via a graphical user interface
(GUI) or the command-line interface (CLI).
B. Writing Shell Scripts:
Basics of Shell Scripting: Learn the syntax and basic commands of shell scripting (e.g.,
#!/bin/bash, echo, sh).
Executing Scripts: Understand how to run a shell script using sh script_name.sh or
./script_name.sh.
Basic Commands:
ls: List. Lists files and directories in the current directory.
ls -l: List files with details
cd: Change Directory. Navigates to a different directory.
mkdir: Make Directory. Creates a new directory.
rmdir: Remove Directory. Deletes an empty directory.
touch: Creates a new empty file.
[touch [Link] – Create an empty file]
cp: Copy. Copies files or directories.
[cp file1 file2 – Copy file1 to file2]
mv: Move. Moves or renames files or directories.
[mv file1 file2 – Rename or move a file]
rm: Remove. Deletes files.
cat: Concatenate. Displays the content of a file.
[cat [Link] – Display file contents]
less: Views file content one page at a time.
head: Displays the first few lines of a file.
tail: Displays the last few lines of a file.
[head -n 5 [Link] – Show first 5 lines
tail -n 5 [Link] – Show last 5 lines]
chmod: Change Mode. Changes the file permissions.
[chmod 755 [Link] – Set file permissions (rwxr-xr-x)]
wc: Counts lines, words, and characters in a file.
[wc -l [Link] – Count the number of lines in a file]
date: Display the current date and time.
echo: echo "Hello World" – Print text to the terminal
clear – Clear the terminal screen
sleep: sleep 5 – Pause execution for 5 seconds
grep: grep "word" [Link] – Search for a word in a file
[read name – Take user input
echo "Your name is $name" – Display user input]
whoami: Display current user
man: Manual. Shows the manual for a command.
[man command_name]
C. Conditional Statements:
Conditional statements in shell scripts allow you to make decisions based on certain
conditions. Here are some examples of how to use conditional statements in shell scripts.
Logical Operators
Logical operators such as && (AND) and || (OR) can be used within conditions.
Using if Statements: Learn how to use if, elif, and else for conditional execution in shell
scripts.
If-Else Statement
The basic syntax for an if-else statement is:
if [ condition ]
then
# commands to execute if condition is true
else
# commands to execute if condition is false
fi
If-Elif-Else Statement
The if-elif-else statement allows you to check multiple conditions.
if [ condition1 ]
then
# commands to execute if condition1 is true
elif [ condition2 ]
then
# commands to execute if condition2 is true
else
# commands to execute if none of the conditions are true
fi
Case Statement
The case statement is used for pattern matching.
case $variable in
pattern1)
# Commands to execute if variable matches pattern1
;;
pattern2)
# Commands to execute if variable matches pattern2
;;
pattern3 | pattern4)
# Commands to execute if variable matches pattern3 or pattern4
;;
*)
# Default case (optional)
;;
Check if a number is even or odd
#!/bin/bash
# Read the number from the user
echo "Enter a number:"
read number
# Check if the number is even or odd
if [ $((number % 2)) -eq 0 ]; then
echo "The number $number is even."
else
echo "The number $number is odd."
fi
Compare three numbers and find the largest
#!/bin/bash
# Read three numbers from the user
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
echo "Enter the third number:"
read num3
# Compare the numbers and find the largest
if [ "$num1" -ge "$num2" ] && [ "$num1" -ge "$num3" ]; then
largest=$num1
elif [ "$num2" -ge "$num1" ] && [ "$num2" -ge "$num3" ]; then
largest=$num2
else
largest=$num3
fi
# Print the largest number
echo "The largest number is: $largest"
Practice:
Write a shell script code to check a year is leap year or not
Check if a number is within a range.
Determine the grade based on marks
Display the day of the week( Using switch case)
Perform basic arithmetic operations (Using Switch case)
Check a year is leap year or not
Determine if a number is positive, negative, or zero:
Basic calculator(BC command):
echo "scale=<decimal_places>; <expression>" | bc
where scale=n → Defines how many decimal places should be displayed.
<expression> → The mathematical expression to evaluate.
Example:
Adding two float number
num1=5.75
num2=2.25
sum=$(echo "scale=3; $num1 + $num2" | bc)
echo "Sum: $sum"