Pages

Shell Scripting Basics

Shell Scripting:
Automates tasks on Unix/Linux by executing a series of commands in a text file (shell script).
Popular Shells: Bash (most common), sh, ksh, csh.
Features:
Variables, loops, conditionals, functions
File manipulation, program execution, text output
Automation: Can schedule scripts using cron for periodic execution.
Use Cases: System administration, backups, monitoring, configuration management, DevOps tasks.
Benefits: Saves time, reduces errors, enables efficient management without GUI.

Shell:
Command-line interface (CLI) that interacts with the OS, executing user commands and managing processes.
Role: Acts as an intermediary between user and OS kernel.
Popular Shells:
Bourne Shell (sh)
C Shell (csh)
Korn Shell (ksh)
Bash (Bourne Again SHell – most widely used)
Features:
Command execution, program running, process management
Scripting for automation: file handling, backups, monitoring, user management
Bash adds loops, conditionals, functions, arrays, arithmetic
Command Processing: Breaks commands into tokens; supports chaining, redirection, and process control.
Customization: Environment configured via .bashrc, .bash_profile, etc.

Bash (Bourne Again Shell)
Powerful Unix shell and scripting language; default on most Linux and macOS systems.
Enhancements over sh: Command history, line editing, tab completion, aliases, advanced scripting.
Core Features:
Command Execution: Run commands interactively or via scripts
Scripting: Supports variables, conditionals, loops, functions, arithmetic, arrays
Environment Management: Set and manipulate environment variables
Job Control: Manage foreground and background jobs
Pipelines & Redirection: Chain commands and control I/O streams
Aliases: Create shortcuts for common commands
Compatibility: Portable across Linux, macOS, and Windows (via WSL).
Productivity Features: Auto-completion, advanced string handling, associative arrays; suitable for beginners and advanced users.

Korn Shell (ksh)
Unix shell developed by David Korn; enhanced, backward-compatible successor to Bourne shell (sh).
Features:
Compatibility: Runs Bourne shell scripts without modification
Interactive Use: Job control, command history, aliasing (from C shell)
Command Line Editing: vi, Emacs, or Gosling Emacs styles
Advanced Scripting: Associative arrays, floating-point arithmetic (ksh93), enhanced loops, function search
Process Handling: Substitution and redirection
Programming Features: Reference variables, hierarchical/object-oriented variable structures
Performance: Faster execution of scripts and commands than some other shells
Use Case: Suitable for interactive use and complex script development in system administration and programming

Zsh (Z Shell)
Highly customizable and powerful Unix shell; extends Bash features for enhanced command-line experience.
Enhanced Auto-Completion: Context-aware suggestions for commands, filenames, and options.
Scripting: Robust scripting like Bash with improved array and function handling.
Themes & Customization: Supports visually appealing prompts, often via Oh My Zsh.
Plugin Support: Git integration, syntax highlighting, autosuggestions, and more through plugins.
History Management: Commands stored with timestamps; advanced search and recall.
Advanced Globbing & Pattern Matching: Recursive searches and flexible patterns beyond Bash.
Interactive Features: Spell correction and suggestions for mistyped commands or files.
---------------------------------------------------------------------------------------------------------------------
Shebang (#!):
Special sequence #! at the very beginning of a script that tells the OS which interpreter to use to execute the script.
Purpose: Allows a script to be run directly without explicitly calling the interpreter.
Examples:
Bash:
#!/bin/bash
Portable version (finds interpreter in user’s PATH):
#!/usr/bin/env bash
Can specify other interpreters:
#!/usr/bin/python3
#!/usr/bin/perl
Notes:
Script must still be made executable:
chmod +x script.sh
The shebang line is generally ignored by the interpreter (treated as a comment).
Essential for portability and clarity in multi-interpreter environments.
---------------------------------------------------------------------------------------------------------------------
Variables:
Variables store data values (text, numbers, filenames) for reuse and dynamic operations in scripts.
Naming and Assignment
Can contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
Cannot start with a number or include special characters like ?, *, -.
Case-sensitive: VAR and var are different.
Assign values without spaces around =:
NAME="John"
COUNT=5
VAR="hello world"  # quotes needed for spaces
Reference a variable using $:
echo $NAME
Types of Variables
Type       Description         Example
Scalar Holds a single value NAME="Alice"
Array Holds multiple values indexed by number ARR=(apple banana orange)
Read-only Cannot be modified after assignment readonly VAR="Value"
Local Accessible only within functions local VAR="Inside"
Environment Global, inherited by child processes export PATH=$PATH:/new/path
User-defined vs System User-created vs predefined by OS USER, HOME, PATH
Unset Variables
Remove a variable:
unset VAR_NAME
Special Variables
Variable Description Example
$0 Script name echo $0
$1..$9, ${10} Positional arguments ./script.sh arg1 arg2
$# Number of arguments echo $#
$@ / $* All arguments echo $@
$$ Process ID of current shell echo $$
$? Exit status of last command echo $?
$RANDOM Random integer 0–32767 echo $RANDOM
$SECONDS Seconds since shell started sleep 3; echo $SECONDS
$IFS Internal Field Separator (default space, tab, newline) echo $IFS
Variable Scope
Global: Accessible anywhere, can be exported to child processes:
MY_VAR="Hello"
export MY_VAR
Local: Accessible only within a function:
my_func() {
    local VAR="Inside function"
    echo $VAR
}
my_func
echo $VAR   # empty
Arithmetic Variables
Use (( )) for calculations and comparisons:
a=5
b=10
((sum = a + b))
echo $sum   # 15
((a++))     # increment
((b--))     # decrement
((c = a * b / 2))

Also usable in conditions:
(( a < b )) && echo "a is less than b"
Parameter Expansion
Provides defaults, substitutions, and variable manipulations:
Syntax Description Example
${VAR:-default} Use default if VAR is unset/empty echo ${NAME:-"Guest"}
${VAR:=default} Set VAR to default if unset echo ${NAME:="Guest"}
${VAR:+value} Use value only if VAR is set echo ${NAME:+Hello}
${VAR:?error} Show error if VAR unset echo ${NAME:?"Required"}
${VAR#pattern} Remove shortest match from front FILE="log.txt"; echo ${FILE#*.} → txt
${VAR%pattern} Remove shortest match from end FILE="log.txt"; echo ${FILE%.txt} → log
---------------------------------------------------------------------------------------------------------------------
Arguments
Arguments are inputs passed to a script from the command line.
Example:
./script.sh arg1 arg2 arg3
script.sh → Script name
arg1, arg2, arg3 → Arguments
Accessing Arguments Inside Script
Variable Meaning
$0 Script name
$1 First argument
$2 Second argument
$3 ... $9 Up to ninth argument
${10} Tenth argument (must use braces after 9)
$# Number of arguments
$@ All arguments (separately)
$* All arguments (as one string)
$$ Process ID of current script
$? Exit status of last command
Example Script
#!/bin/bash
echo "Script Name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Total arguments: $#"
echo "All arguments: $@"
echo "Process ID: $$"
Run:
./script.sh hello world
Output:
Script Name: ./script.sh
First argument: hello
Second argument: world
Total arguments: 2
All arguments: hello world
Process ID: 12345
$@ vs $* (Important Difference)
When quoted:
"$@"
Treats each argument separately.
for arg in "$@"
do
    echo "$arg"
done

If run:
./script.sh "hello world" test
Output:
hello world
test
"$*"
Treats all arguments as one single string.
Output:
hello world test
In most cases, use "$@".
Handling Arguments With Spaces
Use quotes when passing:
./script.sh "arg with spaces"
Inside script:
echo "$1"
Always quote variables to avoid errors:
echo "$1"
NOT:
echo $1   # risky
Checking If Arguments Are Missing
if [ $# -eq 0 ]; then
    echo "No arguments provided"
    exit 1
fi
---------------------------------------------------------------------------------------------------------------------
Conditional Statements
if Statement:
Executes commands only if the condition is true.
Syntax
if [ condition ]; then
    commands
fi
Example
num=10
if [ $num -gt 5 ]; then
    echo "Number is greater than 5"
fi

if-else Statement:
Executes one block if true, otherwise another block.
Syntax
if [ condition ]; then
    commands_if_true
else
    commands_if_false
fi
Example
num=3
if [ $num -gt 5 ]; then
    echo "Greater than 5"
else
    echo "5 or less"
fi

if-elif-else (Multiple Conditions):
Used when checking multiple conditions.
Syntax
if [ condition1 ]; then
    commands1
elif [ condition2 ]; then
    commands2
else
    commands_default
fi
Example
num=10
if [ $num -lt 5 ]; then
    echo "Less than 5"
elif [ $num -eq 10 ]; then
    echo "Equal to 10"
else
    echo "Other value"
fi

Nested if:
An if inside another if for complex logic.
Example
num=10
if [ $num -gt 5 ]; then
    if [ $num -lt 20 ]; then
        echo "Number is between 5 and 20"
    fi
fi

Common Test Conditions
Numeric Comparisons
Operator Meaning
-eq        Equal
-ne        Not equal
-lt        Less than
-le        Less than or equal
-gt        Greater than
-ge        Greater than or equal

Example:
if [ $a -eq $b ]; then

File Tests
Test Meaning
-f Regular file exists
-d Directory exists
-e File exists
-r Readable
-w Writable
-x Executable

Example:
if [ -f "file.txt" ]; then

String Comparisons
Operator Meaning
=         Equal
!=         Not equal
-z         String is empty
-n         String is not empty

Example:
if [ "$name" = "Alice" ]; then

Important Notes
Always put spaces inside [ ].
Correct: [ $num -gt 5 ]
Wrong: [$num -gt 5]
Quote variables in string comparisons to avoid errors:
if [ "$var" = "value" ]; then

Difference Between [ ], [[ ]], and ( )
[ ] → Test Command (POSIX Standard)

[ ] is actually a command (same as test).
It is supported in all shells.

Features
Used for numeric, string, and file comparisons.
Must leave spaces inside brackets.
Limited pattern matching.

Example
if [ "$a" -gt 5 ]; then
    echo "Greater than 5"
fi
Limitations
< and > must be escaped.
Pattern matching is weak.
Logical operators use:
-a (AND)
-o (OR)
Example:
if [ "$a" -gt 5 -a "$b" -lt 10 ]; then
[[ ]] → Advanced Test (Bash/Ksh Feature)
[[ ]] is a keyword, not a command.
It is more powerful and safer than [ ].
Advantages
No need to escape < and >.
Better pattern matching.
Supports regex matching.
Safer with unquoted variables.
Example
if [[ $a -gt 5 ]]; then
    echo "Greater than 5"
fi
Pattern Matching
if [[ $name == A* ]]; then
    echo "Starts with A"
fi
Regex Matching
if [[ $name =~ ^[A-Z] ]]; then
    echo "Starts with capital letter"
fi
Logical Operators
&& (AND)
|| (OR)
if [[ $a -gt 5 && $a -lt 20 ]]; then
Recommended for Bash scripts
( ) → Subshell
Parentheses are NOT for testing conditions.
They create a subshell (a child shell process).
Example
(a=5; echo $a)
Variables inside ( ) do NOT affect the parent shell.
a=10
(a=20)
echo $a   # Still 10
---------------------------------------------------------------------------------------------------------------------
for Loop
Used when you know how many times you want to repeat something, or when iterating over a list.
Basic for Loop (List Style)
Syntax
for variable in list
do
    commands
done
Example
for name in Alice Bob Charlie
do
    echo "Hello $name"
done
Loop Through Files
for file in *.txt
do
    echo "Processing $file"
done
C-Style for Loop (Bash)
Syntax
for (( initialization; condition; increment ))
do
    commands
done
Example
for (( i=1; i<=5; i++ ))
do
    echo "Number $i"
done
---------------------------------------------------------------------------------------------------------------------
while Loop:
Used when you want to repeat commands while a condition is true.
Basic while Loop
Syntax
while [ condition ]
do
    commands
done
Example
count=1
while [ $count -le 5 ]
do
    echo "Count: $count"
    ((count++))
done
Infinite while Loop
while true
do
    echo "Running..."
done

Stop with Ctrl + C.

Read File Line by Line (Common Use)
while read line
do
    echo "$line"
done < file.txt
Difference Between for and while
Feature         for Loop                        while Loop
Best used when Iterating over list or fixed range Condition-based repetition
Counter needed Often built-in Usually manual
Common use Files, arrays, ranges User input, file reading
Loop Control Statements
break → Exit loop immediately
for i in 1 2 3 4 5
do
    if [ $i -eq 3 ]; then
        break
    fi
    echo $i
done
continue → Skip current iteration
for i in 1 2 3 4 5
do
    if [ $i -eq 3 ]; then
        continue
    fi
    echo $i
done
---------------------------------------------------------------------------------------------------------------------
until Loop:
The until loop runs until the condition becomes true.
It is the opposite of while.
while → runs while condition is true
until → runs while condition is false
Syntax
until [ condition ]
do
    commands
done
Example
count=1
until [ $count -gt 5 ]
do
    echo "Count: $count"
    ((count++))
done
This runs until count > 5 becomes true.
---------------------------------------------------------------------------------------------------------------------
select Loop:
select is used to create a menu-driven program.
It displays a numbered list and waits for user input.
Syntax
select variable in list
do
    commands
done
Example
select fruit in Apple Banana Mango Exit
do
    case $fruit in
        Apple) echo "You chose Apple";;
        Banana) echo "You chose Banana";;
        Mango) echo "You chose Mango";;
        Exit) break;;
        *) echo "Invalid choice";;
    esac
done
How it works:
Displays numbered menu.
User enters a number.
Selected value is stored in fruit.
Loop continues until break.
Mostly used in interactive scripts.
---------------------------------------------------------------------------------------------------------------------
Nested Loops:
A loop inside another loop.
Used for:
Matrix operations
Pattern printing
Comparing combinations
Example (Nested for)
for i in 1 2 3
do
    for j in A B
    do
        echo "i=$i j=$j"
    done
done
Output:
i=1 j=A
i=1 j=B
i=2 j=A
i=2 j=B
i=3 j=A
i=3 j=B
Example (Nested while)
i=1
while [ $i -le 3 ]
do
    j=1
    while [ $j -le 2 ]
    do
        echo "i=$i j=$j"
        ((j++))
    done
    ((i++))
done
---------------------------------------------------------------------------------------------------------------------
Shell Commands:

echo
echo is used to display text or variable values to the terminal (standard output).
It is one of the most commonly used commands in shell scripts.
Basic Syntax
echo [options] "text"
Common Uses
Print simple text
echo "Hello, World!"
Print variable values
name="Alice"
echo "Hello, $name"
Multiple arguments
echo Hello World
Common Options
Option Purpose
-n Do not print the trailing newline
-e Enable interpretation of backslash escapes
Escape Sequences (used with -e)
Escape Meaning
\n New line
\t Tab
\\ Backslash
\" Double quote
Examples
Without newline
echo -n "Enter your name: "
Using escape characters
echo -e "Line1\nLine2"
Output:
Line1
Line2
Redirect output to a file
echo "This is a line" > file.txt
Append instead of overwrite:
echo "Another line" >> file.txt
Example in Script
#!/bin/bash
echo "Starting script..."
echo "Process completed successfully."
---------------------------------------------------------------------------------------------------------------------
printf
printf is used for formatted output. It is more powerful and precise than echo and works similarly to the printf function in C.
Unlike echo, printf does not automatically add a newline unless you include \n.
Basic Syntax
printf "format_string" arguments
Common Format Specifiers
Specifier Meaning
%s String
%d Integer (decimal)
%f Floating-point number
%x Hexadecimal
%c Character
\n New line
\t Tab
Examples
Print a simple string
printf "Hello, World!\n"
Print variables
name="Alice"
age=25
printf "Name: %s\nAge: %d\n" "$name" "$age"
Floating point formatting
printf "Value: %.2f\n" 3.14159
(Output: Value: 3.14)
Alignment and spacing
printf "%-10s %5d\n" "John" 50
-10s → Left-align string in 10 spaces
5d → Right-align integer in 5 spaces
Zero padding
printf "%04d\n" 25
(Output: 0025)
Using in Scripts
#!/bin/bash
echo "Using echo:"
echo "Score: 90"
echo "Using printf:"
printf "Score: %d\n" 90
---------------------------------------------------------------------------------------------------------------------
cat
cat (concatenate) is used to display, combine, create, and append files. It reads files sequentially and writes their contents to standard output.
Basic Syntax
cat [options] file1 file2 ...
Common Uses
Display file content
cat filename.txt
Display multiple files
cat file1.txt file2.txt
Create a new file
cat > newfile.txt
(Type content, press Ctrl + D to save.)
Append to an existing file
cat >> existingfile.txt
Use with pipes
cat file.txt | grep "error"
---------------------------------------------------------------------------------------------------------------------
awk
awk is a powerful text-processing and pattern-scanning tool used to extract, manipulate, and format data from files or input streams. It works line by line and splits each line into fields.
Basic Syntax
awk 'pattern { action }' file
pattern → Condition to match (optional).
action → What to do when pattern matches.
If no pattern is given, action runs on every line.
Built-in Variables
Variable          Meaning
$0  Entire line
$1, $2, ... Fields (columns)
NF  Number of fields
NR  Current line number
FS  Field separator (default: space/tab)
OFS  Output field separator
Common Options
Option  Purpose
-F 'delimiter' Set field separator
-v var=value Define variable before execution
Examples
Print first column
awk '{print $1}' file.txt
Print first and third columns
awk '{print $1, $3}' file.txt
Use comma as field separator (CSV file)
awk -F ',' '{print $2}' file.csv
Print lines containing "error"
awk '/error/ {print}' logfile.txt
Numeric condition
awk '$3 > 50 {print $1, $3}' marks.txt
Print line numbers
awk '{print NR, $0}' file.txt
Formatted output
awk '{printf "Name: %s, Score: %d\n", $1, $2}' file.txt
Control Structures in awk
awk supports:
if-else
for loops
while loops
Variables and arithmetic operations
Example:
awk '{ sum += $2 } END { print "Total:", sum }' file.txt
---------------------------------------------------------------------------------------------------------------------
grep / egrep
grep and egrep are used to search for patterns in files or input streams.
grep: Uses Basic Regular Expressions (BRE). Special characters like + ? | () must be escaped.
egrep: Same as grep -E, uses Extended Regular Expressions (ERE), so meta-characters are treated as special without escaping.
Basic Syntax
grep [options] pattern file
egrep [options] pattern file
# egrep ≡ grep -E
Common Options
Option                 Purpose
-i Ignore case while matching.
-v Invert match (show lines that do not match).
-c Count matching lines.
-n Show line numbers with matching lines.
-r Recursively search directories.
-l Show only filenames that match.
-E Use extended regex (equivalent to egrep).
Examples
Search for a word in a file
grep "error" logfile.txt
Case-insensitive search
grep -i "error" logfile.txt
Show line numbers
grep -n "error" logfile.txt
Invert match
grep -v "info" logfile.txt
Search recursively in directories
grep -r "TODO" /home/user/projects/
Using egrep for multiple patterns
egrep "error|warning" logfile.txt
# Equivalent to: grep -E "error|warning" logfile.txt
Count matches
grep -c "success" logfile.txt
Tip:
egrep is deprecated on some systems in favor of grep -E, but they behave the same.
Use grep for simple searches, egrep or grep -E for complex regex patterns.
---------------------------------------------------------------------------------------------------------------------
read
The read command is used to take input from the user or from a file/pipe and store it in one or more variables. It reads a single line of input by default and splits the line into fields based on the Internal Field Separator (IFS).
Basic Syntax
read [options] variable1 variable2 ...
If no variable is provided, input is stored in the default variable REPLY.
Common Options
Option                             Purpose
-p "prompt" Display a prompt message before reading input.
-t N Set a timeout of N seconds; command returns if no input.
-n N Read exactly N characters. Useful for single-character input.
-s Silent mode; input is not displayed (useful for passwords).
-a array Read words into an array variable.
-r Do not treat backslashes as escape characters (raw input).
Examples
Read a single value from the user:
echo "Enter your name:"
read name
echo "Hello, $name!"
Read multiple variables from one line:
echo "Enter first and last name:"
read first last
echo "First: $first, Last: $last"
Prompt inline using -p:
read -p "Enter your age: " age
echo "You are $age years old."
Read input silently (for passwords):
read -s -p "Enter password: " pass
echo
echo "Password entered."
Read with a timeout (5 seconds):
read -t 5 -p "Enter value within 5 seconds: " value
echo "Value: $value"
Read input into an array:
read -a fruits
echo "You entered: ${fruits[0]}, ${fruits[1]}"
Tips:
read is interactive but can also read input from files or pipelines.
Commonly used in loops for processing lines from a file:
while read line; do
  echo "$line"
done < file.txt
---------------------------------------------------------------------------------------------------------------------
sed
sed (stream editor) is used for text transformations on files or input streams without opening an interactive editor. It works line by line, allowing substitution, deletion, insertion, and other text processing tasks.
Basic Syntax
sed [options] 'script' file
script contains commands like s/pattern/replacement/ for substitution.
Common Options
Option                                Purpose
-i Edit the file in-place (modifies the original file).
-n Suppress automatic printing (use with p to selectively print lines).
-e Add multiple editing commands.
-f Read editing commands from a file.
-r / -E Enable extended regular expressions.
Common Commands
Command                             Purpose
s/pattern/replacement/ Substitute first occurrence in a line.
s/pattern/replacement/g Substitute all occurrences in a line.
/pattern/d Delete lines matching a pattern.
i\text Insert text before the current line.
a\text Append text after the current line.
y/abc/xyz/ Translate characters (like tr).
Examples
Replace first occurrence of “hello” with “world”:
sed 's/hello/world/' file.txt
Replace all occurrences of “old” with “new”:
sed 's/old/new/g' file.txt
Delete lines containing “error”:
sed '/error/d' file.txt
Insert a line before line 3:
sed '3i\This is a new line' file.txt
Append a line after line 2:
sed '2a\Appended line' file.txt
Use in-place editing to update the file directly:
sed -i 's/foo/bar/g' file.txt
Read multiple commands from a file:
sed -f commands.sed file.txt
Tips:
sed is ideal for automated file edits, text stream transformations, and pattern-based replacements.
For very complex text processing, combine with awk for field-based operations.
Use -i.bak with sed for safe in-place edits, which keeps a backup of the original file.
---------------------------------------------------------------------------------------------------------------------
cut
The cut command extracts specific sections, fields, or characters from each line of a file or input stream. It’s useful for processing structured text like CSV, TSV, or fixed-width files.
Basic Syntax
cut [options] <file>
Common Options
Option                           Purpose
-c N Select specific characters by position (e.g., 1-5 for characters 1 to 5).
-b N Select specific bytes (useful for fixed-width data).
-f N Select fields (columns) from each line.
-d 'DELIM' Specify delimiter for fields (default is tab).
--complement Select all except the specified fields/characters.
-s Suppress lines without delimiters (useful with -f).
Examples
Extract first 5 characters from each line:
cut -c 1-5 filename.txt
Extract the second field from a CSV file (comma as delimiter):
cut -d ',' -f 2 file.csv
Extract multiple fields (first and third columns):
cut -d ',' -f 1,3 file.csv
Extract fields from a space-separated file:
cut -d ' ' -f 1,4 data.txt
Combine with pipes for processing output:
cat data.txt | cut -d ':' -f 1,3
Tips:
Use -f with -d for column extraction, -c for character positions.
Combine cut with sort, uniq, grep for advanced text processing.
Avoid cut for very complex parsing; tools like awk or sed are more flexible.
---------------------------------------------------------------------------------------------------------------------
find
The find command is used to search for files and directories in a directory hierarchy based on conditions like name, type, size, permissions, and more. It is very powerful for automating file management tasks.
Basic Syntax
find <path> [options] [expression]
<path>: Directory where the search starts (e.g., /home/user, .).
[options] and [expression]: Criteria to filter the search.
Common Options / Expressions
Option               Purpose
-name "pattern" Search by filename (supports wildcards * and ?).
-type [f/d] Search by type: f for files, d for directories.
-size [+/-]N Search by size: + greater than, - smaller than, N units (default is 512-byte blocks, use c for bytes, k for KB, M for MB).
-mtime N Modified exactly N days ago.
-mmin N Modified N minutes ago.
-perm MODE Search by file permissions (e.g., -perm 644).
-empty Find empty files or directories.
-exec command {} \; Execute a command on each found file.
-print Print the path of matching files (default in many systems).
Examples
Find by name:
find /home/user -name "file.txt"
Find all .log files:
find /var/log -name "*.log"
Find directories only:
find /home/user -type d
Find files larger than 10 MB:
find /data -size +10M
Delete .tmp files:
find /tmp -name "*.tmp" -exec rm -f {} \;
Find empty files:
find /tmp -type f -empty
Tips:
Combine multiple conditions with -and, -or, or ! for NOT.
Example: Find .txt files not larger than 1MB:
find . -name "*.txt" ! -size +1M
Always test with -print before using -exec rm to avoid accidental deletions.
---------------------------------------------------------------------------------------------------------------------
cmp
The cmp command is used to compare two files byte by byte. It tells you whether files are identical or where the first difference occurs. It is useful for precise comparisons of both text and binary files.
Basic syntax
cmp [options] file1 file2
Common Flags / Options
Flag                                  Purpose
-b Prints differing bytes in octal along with their positions.
-l Lists all differing bytes with byte numbers and values.
-s Silent mode: no output, just returns exit status.
-i N Ignores the first N bytes of each file when comparing.
Exit Codes
cmp uses exit codes to signal comparison results:
Exit Code Meaning
0 Files are identical
1 Files differ
>1 An error occurred (e.g., file not found)
Examples
Simple comparison:
cmp file1.txt file2.txt
Output:
file1.txt file2.txt differ: byte 10, line 1
Silent check in scripts:
if cmp -s file1.txt file2.txt; then
    echo "Files are identical"
else
    echo "Files differ"
fi
Show all differing bytes:
cmp -l file1.bin file2.bin
Ignore first 5 bytes:
cmp -i 5 file1.txt file2.txt
Key difference from diff:
cmp checks byte-by-byte, ideal for binary files.
diff checks line-by-line, better for text files.
---------------------------------------------------------------------------------------------------------------------
diff
Compare text files line by line.
Basic syntax
diff [options] file1 file2
Common diff flags
Flag                                 Purpose
-u Unified format: shows a few lines of context around changes; widely used for patches.
-c Context format: shows more context lines before and after changes.
-i Ignore case differences between lines.
-w Ignore all whitespace differences.
-r Recursive: compare directories and subdirectories.
-q Quiet mode: only reports whether files differ, no line details.
--strip-trailing-cr Ignores carriage return differences (useful for Windows vs Linux files).
Exit codes
diff also uses numeric exit codes to signal result:
Exit Code Meaning
0 Files are identical
1 Files differ
>1 An error occurred (e.g., file not found)
Examples
Simple comparison:
diff file1.txt file2.txt
Unified diff (good for patches):
diff -u file1.txt file2.txt
Ignore whitespace:
diff -w file1.txt file2.txt
Recursively compare directories:
diff -r dir1/ dir2/

Tip: When scripting, you can use the exit code of diff to check if files differ:
if diff -q file1.txt file2.txt; then
    echo "Files are identical"
else
    echo "Files differ"
fi
---------------------------------------------------------------------------------------------------------------------
exit [n]
n is an integer exit status (optional).
Meaning of codes:
0 → Success (everything worked fine)
1–255 → Error or abnormal termination (non-zero = failure)
If you don’t provide n, exit uses the status of the last command automatically ($?).
Example usage
#!/bin/bash
echo "Starting script..."
if [ ! -f myfile.txt ]; then
    echo "Error: myfile.txt not found."
    exit 1   # exit with failure code
fi
echo "Script completed successfully."
exit 0       # exit with success code

Deploying Partition Templates in HMC

Managing multiple IBM POWER systems can be time-consuming and error prone when every deployment is done manually. The Hardware Management Console (HMC) Enhanced UI simplifies and standardizes system provisioning through System Templates and Partition Templates.

HMC (Hardware Management Console):
Centralized management interface for IBM Power servers.
Functions:
  • Creates, configures, and manages logical partitions (LPARs) and VIOS partitions.
  • Monitors system health, events, and firmware updates.
  • Connects to the Managed Host over a Management Network (TCP/IP).
  • UI: Provides both graphical (GUI) and command-line (CLI) tools.
Managed System (Power Server):
This is the physical Power system that houses processors, memory, and network adapters. It’s managed by the HMC and partitioned into multiple LPARs (logical systems).

Physical NICs:
SR-IOV Adapters: Allow direct assignment of virtual NICs to LPARs for high performance.
HEA (Host Ethernet Adapter): Firmware-level virtual adapter that provides network connectivity to LPARs through firmware virtualization.
Dedicated Physical I/O: Entire network or storage adapters can be assigned to a single partition.

CPU / Memory:
LMBs (Logical Memory Blocks): Basic unit of memory allocation to partitions.
Shared Processor Pools: Allow dynamic CPU sharing among partitions.
Dedicated Processors: Assigned exclusively to one LPAR.

VIOS (Virtual I/O Server) Partitions:
VIOS partitions act as intermediaries between the physical hardware and client LPARs. They virtualize network and storage I/O, allowing client partitions to share physical resources.

Virtual Network:
Virtual Ethernet Adapters: Internal network interfaces connecting client LPARs through the PowerVM hypervisor.
Link Aggregation (EtherChannel): Combines multiple physical NICs for redundancy and bandwidth.
SEA (Shared Ethernet Adapter): Bridges the virtual Ethernet (from clients) to the physical network, enabling external connectivity.

Virtual Storage:
Virtual SCSI (vSCSI): Exports physical disks or SAN LUNs from VIOS to client LPARs.
Shared Storage Pools (SSP): Pool of storage shared among multiple systems for flexible allocation.
NPIV (N-Port ID Virtualization): Allows client LPARs to have direct Fibre Channel (SAN) access via virtual WWPNs through VIOS.

Type of Template:

System Template — a blueprint for a managed system + its VIOS partitions. Includes processor pools, Logical Memory Blocks (LMBs), physical I/O capture, VIOS definitions, virtual network and storage settings.

Partition Template — a blueprint for client partitions (AIX, Linux, IBM i): CPU, memory, vNICs, vSCSI, and IPL parameters.

Starter templates — built into HMC; convenient base templates (read‑only). You can copy them to create editable user templates.

User‑captured templates — capture a working system or partition to reproduce identical configurations on other hardware.

Benefits: consistency across environments, faster deployments, fewer manual errors, documented configurations for dev/test/prod.

You can launch the wizard from two places:
Template Library (HMC Management → Templates and OS Images → System Template) — right‑click a template → Deploy.
All Systems View — select an existing managed system → Action → View All Actions → Deploy System From Template.

The wizard runs through four stages:
System Configuration — CPU, LMBs, physical I/O mapping, VIOS creation.
VIOS Installation — Install VIOS via NIM, From HMC or Local NIM.
Virtual Network Configuration — Create bridges, link aggregation, assign IPs.
Virtual Storage Configuration — Assign VIOS to clusters, Create device pools and media repositories.

The Deploy System Template wizard

You can launch the wizard from two places:
  1. Template Library (HMC Management → Templates and OS Images → System Template) — right‑click a template → Deploy.
  2. All Systems View — select an existing managed system → Action → View All Actions → Deploy System From Template.
The wizard runs through four stages:
  1. System Configuration — CPU, LMBs, physical I/O mapping, VIOS creation.
  2. VIOS Installation — install VIOS via NIM, Local MC, or Local NIM.
  3. Virtual Network Configuration — create bridges, link aggregation, assign IPs.
  4. Virtual Storage Configuration — assign VIOS to clusters, create device pools and media repositories.
Create, copy, or import templates:
  1. Open HMC Management → Templates and OS Images → Template Library.
  2. Choose the System Template or Partition Template tab.
  3. To create an editable template from a starter: right‑click a starter template → Copy → give it a descriptive name (e.g., vios-sriov-template, webserver-prod-v1).
  4. To capture an existing system: use the Capture function on a managed system or partition to create a user template.
  5. Edit the copied or captured template: adjust CPU pools, LMB sizes, virtual network definitions, virtual storage configuration, SR‑IOV/HEA settings, and IPL parameters. Save with a version name (e.g., v1.0‑prod).
  6. Export / Import if you need to transfer templates between HMCs.
Tip: maintain named versions for different environments (dev/test/prod) and include a short description of intended hardware families and compatibility notes.

Stage 1 — System Configuration (detailed)
Validate before you start. Use the wizard’s Reset or validation checks to ensure the target system is compatible. Be aware that starting the workflow may delete existing LPARs or VIOS on the target—take backups.
1. Select Template — pick your prepared system template.
2. Select Target System — choose the managed host to configure. Click Reset to re‑validate current partitions and adapters.
3. Captured I/O option:
Enabled: the template’s physical I/O mapping is applied verbatim. Fast but prone to Hardware Mismatch errors if target server hardware differs.
Disabled: manually map adapters to template VIOS definitions so you can adapt to differing hardware.
4. SR‑IOV Adapter settings:
If captured I/O is enabled, SR‑IOV mappings are read‑only.
If disabled, you may select physical ports, set speeds/duplex, create logical SR‑IOV ports and map them to VIOS.
Note: HMC may default to shared mode for SR‑IOV ports — change to dedicated only if your hardware and use case require it.
5. VIOS Configuration Summary — review VIOS names and key settings. Only the VIOS name is changeable at this point.
6. Physical I/O Mapping — map physical adapters (including SR‑IOV logical ports and HEA ports) to the VIOS definitions in the template.
7. Start System Configuration — click Start. HMC creates partitions, assigns CPU/memory (LMB), and configures basic VIOS objects.

Failure modes: if the wizard cannot obtain adapter information the process halts with "Unable to obtain network adapter information". Fix adapter assignments or finish VIOS setup manually.

Stage 2 — VIOS Installation
  1. Choose installation method: NIM, Local MC, or Local NIM. NIM is common for automated network installs.
  2. NIM server parameters (if using NIM): NIM server IP, network port, target VIOS IP, netmask, and gateway.
  3. Start Installation — click Start and monitor progress in the wizard.
  4. After install completes, check Accept License and continue.
Troubleshooting: If install fails, verify network connectivity to the NIM server, correct image selection, and that routing/firewall rules permit access.

Stage 3 — Virtual Network Configuration
  1. Configure a network bridge for each VIOS by selecting assigned network interface(s).
  2. Optionally enable Link Aggregation — Choose Create Link Aggregation Device and add member NICs.
  3. Assign IPs to VIOS if required by the chosen installation method.
  4. Click Start to apply network configuration and watch the progress.
Note: the wizard requires a suitable adapter assigned to each VIOS. If none is available, the wizard won’t progress.

Stage 4 — Virtual Storage Configuration
  1. Assign VIOS to a cluster (optional) or choose Do not assign now.
  2. Create / Reserve a Device Pool if needed for dedicated virtual disk provisioning.
  3. Configure the media repository for storing virtual images and vSCSI mappings.
  4. Click Start and monitor the wizard for I/O creation and device assignment messages.
Post‑deploy validation checklist:
  • Confirm logical partitions exist and match the template (CPU, LMB, storage).
  • Validate each VIOS boots correctly and that bridges and vNICs are present.
  • From the HMC, verify assigned physical adapters and SR‑IOV logical ports are mapped to the expected VIOS.
  • Check device pools and media repository accessibility for virtual storage.
  • Apply any OS updates, firmware patches, and HMC updates as necessary.
Deploy a VIOS with SR‑IOV logical ports:
  1. Copy a starter System Template and name it vios-sriov-template.
  2. Edit the template: enable 1 VIOS, add 4 SR‑IOV logical ports under the VIOS → Physical I/O tab.
  3. Save and export to the target HMC if you operate multiple HMCs.
  4. From All Systems view → Select System → Action → Deploy System From Template → choose vios-sriov-template.
  5. On Physical IO mapping: map the correct physical port(s) to the SR‑IOV logical ports and pick shared/dedicated mode.
  6. Proceed with VIOS install (NIM) and configure network bridge + storage.
Checklist before Start: backups, template version, adapter mapping, NIM accessibility, firmware and HMC compatibility.