Systems Programming Presentation
Systems Programming Presentation
PROGRAMMING
PRESENTATION
ICS: 2305
SHELL SCRIPT CONSTRUCTS
Shell scripts are a set of commands executed by the Unix/Linux 2
While Loop:
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo "Counter: $count"
((count++))
done
UNTIL LOOP:
6
#!/bin/bash
n=1
until [ $n -gt 5 ]
do
echo "Until loop number: $n"
((n++))
done
CASE STATEMENT
7
#!/bin/bash
echo "Enter a character:"
read char
case $char in
[a-z]) echo "Lowercase Letter";;
[A-Z]) echo "Uppercase Letter";;
[0-9]) echo "Digit";;
*) echo "Special Character";;
esac
CASE STATEMENT
#!/bin/bash 8
case "$2"
in
"+") ans=`expr $1 + $3`
printf "%d %s %d = %d\n" $1 $2 $3 $ans
;;
"-") ans=`expr $1 - $3`
printf "%d %s %d = %d\n" $1 $2 $3 $ans
;;
"\*") ans=`expr "$1 * $3"`
printf "%d %s %d = %d\n" $1 $2 $3 $ans
;;
"/") ans=`expr $1 / $3`
printf "%d %s %d = %d\n" $1 $2 $3 $ans
;;
# Notice this: the default case is a simple *
*) printf "Don't know how to do that.\n"
;;
INTERACTIVE SHELL SCRIPT
Simple Calculator:
9
#!/bin/bash
case $op in
+) result=$((a + b));;
-) result=$((a - b));;
\*) result=$((a * b));;
/)
if [ $b -ne 0 ]; then
result=$((a / b))
else
echo "Cannot divide by zero"
exit 1
fi
;;
*) echo "Invalid operator"; exit 1;;
esac
USER INFORMATION SCRIPT
• #!/bin/bash
• echo ""
• echo "=== User Information ==="
• echo "Name: $name"
• echo "Age: $age"
• echo "Country: $country"
FILE EXISTENCE CHECKER
#!/bin/bash
if [ -f "$filename" ]; then
echo "File exists."
else
echo "File does not exist."
fi
ADVANCED INTERACTIVE SHELL SCRIPT FOR
A MENU-DRIVEN FILE MANAGER 12
This script allows the user to perform file operations such as creating, viewing, renaming, deleting, and listing files
through an interactive menu.
• Menu-Driven File Manager (Advanced Shell Script)
• #!/bin/bash
• while true
• do
• echo ""
• echo "========= File Manager Menu ========="
• echo "1. Create a File"
• echo "2. View a File"
• echo "3. Rename a File"
• echo "4. Delete a File"
• echo "5. List All Files"
• echo "6. Exit"
• echo "====================================="
• echo -n "Choose an option [1-6]: "
• read choice
•
SECURE, INTERACTIVE SHELL SCRIPT FOR A USER LOGIN
AND PASSWORD CHECK SYSTEM 13
This script simulates a basic login system with:A predefined
username-password pair (stored in a file or hardcoded).Masked
password input using read -s.Login attempts limitation.
# Format: username:password
admin:admin123
guest:guest321
USERNAME="admin"
PASSWORD="pass123"
echo "========================="
echo "========================="
attempts=0
max_attempts=3
read input_user
read -s input_pass
echo ""
exit 0
else
((attempts++))
MACRO LANGUAGE, MACRO-PROCESSORS, AND
MACRO INSTRUCTIONS (SHELL SCRIPTING) 16
Macro Language:
Macro language allows you to define a sequence of commands or code blocks once
and reuse them multiple times with different arguments. It's used to automate
repetitive tasks and abstract complex sequences.
In shell scripting, functions and aliasing simulate macro behavior.
Macro Processor:
Replaces macro invocations with the corresponding block of code (known as macro
expansion)
Accepts parameters for substitution. In Unix/Linux:Shell itself acts as a macro
processor using functions, aliases, and eval.
Macro Instructions:
Macro Definitions: Set of commands defined once and reused.
Macro Invocation: Calling the macro with optional arguments.
Parameter Substitution: Replace placeholders with actual values during expansion.
SIMULATING MACRO BEHAVIOR IN SHELL SCRIPTS 17
Using Functions (Simulated Macros)
Example 1: Simple Macro Using Shell Function
#!/bin/bash
# Macro invocation
say_hello "Alice"
say_hello "Bob"
PARAMETERIZED MACROS
Example 2: Macro with Multiple Arguments 18
#!/bin/bash
greet_user() {
echo "Hello $1, your role is $2."
}
# Calling macro
greet_user "James" "Admin"
greet_user "Clara" "User"
USING ALIASES (VERY SIMPLE MACROS)
alias ll='ls -alF' 19
#!/bin/bash
macro="ls -lh"
echo "Running macro: $macro"
eval $macro
ADVANCED MACRO-LIKE USE CASE: FILE OPERATIONS
20
Example: Macro to Backup and Compress:
#!/bin/bash
backup_and_compress() {
src=$1
dest=$2
ts=$(date +"%Y%m%d_%H%M%S")
filename="backup_${ts}.tar.gz"
tar -czf "$dest/$filename" "$src"
echo "Backup of $src saved as $dest/$filename"
}
# Call macro
backup_and_compress "/home/user/Documents" "/home/user/Backups"
21
MACRO EXPANSION STEPS
Define a macro (e.g., function)
Invoke the macro (with/without parameters)
Expand: Replace the macro with actual commands
Execute: The shell runs the expanded commands
# User-defined variables
name="Alice"
age=21
# System-defined variables
echo "Username: $USER"
echo "Home directory: $HOME"
src_dir="/home/student/documents"
backup_dir="/home/student/backup"
timestamp=$(date +"%Y%m%d_%H%M%S")
• #!/bin/bash