I/O Redirection Fundamentals
Three Standard Streams
| Stream | File Descriptor | Default | Purpose |
|---|---|---|---|
| stdin | 0 | Keyboard | Input to programs |
| stdout | 1 | Screen | Normal output |
| stderr | 2 | Screen | Error messages |
Key Concept: stdout and stderr are separate streams, allowing you to handle errors differently from normal output.
Redirection Operators
Standard Output Redirection
| Operator | Function | Example | Result |
|---|---|---|---|
> | Redirect stdout (overwrite) | ls > file.txt | Overwrites file.txt with output |
>> | Redirect stdout (append) | ls >> file.txt | Adds to end of file.txt |
2> | Redirect stderr (overwrite) | ls 2> err.txt | Sends errors to err.txt |
2>> | Redirect stderr (append) | ls 2>> err.txt | Appends errors to err.txt |
&> | Redirect both stdout and stderr | ls &> all.txt | Both output and errors to file |
Important Notes
>OVERWRITES existing files completely>>APPENDS to existing files- If command fails with
>, file becomes 0 bytes >only redirects stdout, NOT stderr
Examples
ls -l /usr/bin > output.txt # Save directory listing to file
echo "Hello" >> output.txt # Add line to existing file
ls /nonexistent 2> error.txt # Capture error message
command > out.txt 2> err.txt # Separate output and errors
command &> all.txt # Everything to one filePipelines
Pipe Operator: |
Purpose: Connect stdout of one command to stdin of another
Syntax:
command1 | command2
Multiple Pipes:
command1 | command2 | command3 | ... | commandN
Common Pipeline Examples
ls -l | less # View long listing with paging
ps aux | grep firefox # Find Firefox processes
cat file.txt | sort | uniq # Display unique sorted lines
history | tail -20 # Show last 20 commands
ls -l | wc -l # Count files in directoryAnalogy: Pipelines are like assembly lines—output from one station becomes input for the next.
Text Editors
nano (Command-line Editor)
nano filename # Open/create file in nanoKey Controls:
| Key | Action |
|---|---|
CTRL + X | Exit nano |
CTRL + O | Save (Write Out) |
CTRL + K | Cut line |
CTRL + U | Paste (Uncut) |
CTRL + W | Search |
CTRL + \ | Replace |
CTRL + G | Help |
Typical Workflow:
- Type/edit content
- Press
CTRL + Xto exit - Press
Yto save changes - Press
Enterto confirm filename
Output: Creates/modifies text file
gedit (GUI Editor)
gedit filename # Open GUI text editor- Works like Notepad/TextEdit
- Has menus, buttons, syntax highlighting
- More user-friendly for GUI users
Text Processing Commands
cat - Concatenate and Display
cat file # Display file contents
cat file1 file2 # Display multiple files
cat file1 file2 > combined # Combine files into one
cat > newfile # Create file from keyboard input (CTRL+D to end)Example Output:
$ cat Ex1.txt
I am a student in the School of ICT.sort - Sort Lines
sort file # Sort and display
sort file > output # Sort and save
sort -r file # Reverse sort
sort -n file # Numerical sortInput (unsorted.txt):
c
c
a
b
b
2
Output of sort unsorted.txt:
2
a
b
b
c
c
uniq - Remove Duplicates
uniq file # Remove adjacent duplicates
uniq -c file # Count occurrences
uniq -d file # Show only duplicates
uniq -u file # Show only unique linesCRITICAL: uniq only removes adjacent duplicates! Always use: sort file | uniq
Example:
$ sort file.txt | uniq # Remove all duplicates
2
a
b
c
$ sort file.txt | uniq -c # Count duplicates
1 2
1 a
2 b
2 cwc - Word Count
wc file # Show lines, words, bytes
wc -l file # Lines only
wc -w file # Words only
wc -c file # Bytes onlyExample Output:
$ wc Ex4.txt
6 6 12 Ex4.txtInterpretation: 6 lines, 6 words, 12 bytes, filename
Other Useful Commands
| Command | Description | Example |
|---|---|---|
grep pattern file | Find lines matching pattern | grep "error" log.txt |
head -n file | Show first n lines | head -10 file.txt |
tail -n file | Show last n lines | tail -20 log.txt |
tail -f file | Monitor file in real-time | tail -f app.log |
cut -d: -f1 file | Extract columns | cut -d: -f1 /etc/passwd |
Important for Quiz: tail -f monitors log files with real-time updates!
Process Management
What is a Process?
- Process: Instance of a running program
- Each process has unique PID (Process ID)
- Linux kernel manages all processes
- Modern systems are multitasking (run multiple processes)
Process States
| State | Description |
|---|---|
| Running | Actively executing |
| Sleeping | Waiting for event |
| Stopped | Suspended (paused) |
| Zombie | Finished but not cleaned up |
Viewing Processes
ps - Process Status
ps # Show your processes
ps aux # Show ALL processes (detailed)
ps -ef # Alternative full listing
ps aux | grep process_name # Find specific processExample Output of ps aux:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 12345 6789 ? Ss 10:00 0:01 /sbin/init
student 1234 2.5 1.2 45678 12345 pts/0 R+ 10:30 0:05 firefox
Key Fields:
- USER: Process owner
- PID: Process ID (use for kill command)
- %CPU: CPU usage percentage
- %MEM: Memory usage percentage
- STAT: Process state (R=running, S=sleeping, Z=zombie)
- COMMAND: Program name
top/htop - Interactive Process Viewer
top # Real-time process monitor
htop # Enhanced version (if installed)- Press
qto quit - Shows live CPU and memory usage
- Updates automatically
Running Processes
Foreground vs Background
Foreground Process:
command # Runs in foreground (blocks terminal)- Takes over terminal until complete
- You see all output
- Must wait for completion
Background Process:
command & # Runs in background- Returns control immediately
- Can run other commands
- Process continues running
Example:
sleep 100 & # Sleep for 100 seconds in background
[1] 12345 # Output: [job#] PIDProcess Control Keys
| Key | Action | Result |
|---|---|---|
CTRL + C | Terminate (SIGTERM) | Stops foreground process immediately |
CTRL + Z | Stop/Suspend (SIGSTOP) | Pauses foreground process (still exists) |
Important Distinction:
CTRL + C= Kill the process (gone forever)CTRL + Z= Pause the process (can resume later)
Job Control
jobs - List Background Jobs
jobs # Show background/stopped jobs
jobs -l # Show with PIDsExample Output:
[1]+ Running sleep 100 &
[2]- Stopped nano file.txt
fg - Bring to Foreground
fg # Resume most recent job in foreground
fg %1 # Resume job [1] in foreground
fg %2 # Resume job [2] in foregroundbg - Resume in Background
bg # Resume most recent stopped job in background
bg %1 # Resume job [1] in backgroundWorkflow Example:
$ nano file.txt # Start editing
[Press CTRL+Z] # Suspend nano
[1]+ Stopped nano file.txt
$ ls # Do other work
$ fg # Return to nanoKilling Processes
kill - Terminate Process
kill PID # Terminate process by PID
kill -9 PID # Force kill (SIGKILL)
kill %1 # Kill job [1]Common Signals:
| Signal | Number | Effect |
|---|---|---|
| SIGTERM | 15 (default) | Polite termination request |
| SIGKILL | 9 | Force kill (cannot be ignored) |
| SIGSTOP | 19 | Stop/pause process |
| SIGCONT | 18 | Continue stopped process |
Examples:
kill 8050 # Terminate process with PID 8050
kill -9 8050 # Force kill PID 8050
killall firefox # Kill all firefox processesShell Scripting Fundamentals
Script Structure
Basic Template
#!/bin/bash
# Comment: Description of script
# Your commands here
echo "Hello World"Required Elements:
- Shebang:
#!/bin/bash(MUST be first line) - Comments: Lines starting with
# - Commands: Actual executable statements
Making Scripts Executable
chmod 755 script.sh # Make executable
chmod +x script.sh # Alternative method
./script.sh # Execute scriptWhy ./?
- Current directory not in PATH by default
./tells shell to look in current directory
Command Line Arguments
Accessing Arguments:
| Variable | Meaning |
|---|---|
$0 | Script name itself |
$1 | First argument |
$2 | Second argument |
$3, $4, etc. | Additional arguments |
$# | Number of arguments |
$@ | All arguments |
$* | All arguments as single string |
Example Script (script.sh):
#!/bin/bash
echo "Script name: $0"
echo "First arg: $1"
echo "Second arg: $2"
echo "Total args: $#"Execution:
$ ./script.sh ABC 20
Script name: ./script.sh
First arg: ABC
Second arg: 20
Total args: 2Variables
Variable Assignment
Syntax: VarName=value
CRITICAL RULES:
- NO SPACES around
=sign - No data type declaration needed
- Can store strings or numbers
Examples:
X=1 # Number
Y="NetLab" # String
Name="John Doe" # String with spaces
Path=/home/student # Path without quotesWRONG:
X = 1 # ERROR: spaces around =
Y= "NetLab" # ERROR: space after =
Z ="value" # ERROR: space before =Using Variables
Syntax: $VarName
echo "Value of X is $X"
echo "Hello $Name"
Result=$X # Assign variable to anotherExample Script:
#!/bin/bash
X=1
Y="NetLab"
echo "The value of X is $X."
echo "The value of Y is $Y."Output:
The value of X is 1.
The value of Y is NetLab.
if-then Statements
Syntax
if [ CONDITION ] ; then
commands
else
commands
fiCRITICAL SPACING RULES:
- Space after
[ - Space before
] - Space around operators
fiends the if statement
Comparison Operators
For Strings:
| Operator | Meaning | Example |
|---|---|---|
= | Equal | [ "$var1" = "$var2" ] |
!= | Not equal | [ "$var1" != "$var2" ] |
For Numbers:
| Operator | Meaning | Example |
|---|---|---|
-eq | Equal | [ $num1 -eq $num2 ] |
-ne | Not equal | [ $num1 -ne $num2 ] |
-gt | Greater than | [ $num1 -gt $num2 ] |
-ge | Greater than or equal | [ $num1 -ge $num2 ] |
-lt | Less than | [ $num1 -lt $num2 ] |
-le | Less than or equal | [ $num1 -le $num2 ] |
CRITICAL: Use = for strings, use -eq for numbers!
String Comparison Example
#!/bin/bash
St="Friday"
if [ "$St" = "Friday" ] ; then
echo "It is Friday."
echo "Let us have a party."
else
echo "Not Friday."
fiOutput:
It is Friday.
Let us have a party.
Number Comparison Example
#!/bin/bash
Age=20
if [ $Age -gt 18 ] ; then
echo "You are an adult."
else
echo "You are a minor."
fiOutput:
You are an adult.
Common Mistake (Quiz Question!)
#!/bin/bash
X=5
Y=3
if [ $X = $Y ] ; then # WRONG! X and Y are numbers
echo "Equal"
fiProblem: Using = for numbers instead of -eq
Correct Version:
if [ $X -eq $Y ] ; then # CORRECT!
echo "Equal"
fiQuiz Key Topics
1. Text Editor in Terminal?
Answer: nano
- Other options: vi, vim, emacs
- nano is simplest for beginners
2. Terminate Process with PID 8050?
Answer: kill 8050
- Use PID (Process ID) to kill specific process
3. List All Files (Including Hidden) and Save to list.txt?
Answer: ls -a > list.txt
-ashows hidden files (start with.)>redirects output to file
4. Suspend Foreground Process and Verify Still Running?
Answer:
- Press
CTRL + Zto suspend (stop/pause) - Run
ps auxto verify process still exists
- Alternatively:
jobsshows background/stopped jobs
5. Input Required for kill Command?
Answer: PID (Process ID)
- Not PIP, DIP, or VSIZ
- Get PID from
ps auxcommand
6. Monitor Log File in Real-Time?
Answer: tail -f app.log
-f= follow (watch for new entries)- Shows appended data as file grows
7. Redirect Standard Error to File?
Answer: 2>
2is file descriptor for stderr- Example:
command 2> error.txt
8. Script Error - String vs Number Comparison?
Problem: Using = for number comparison Solution: Use -eq for numbers, = for strings
9. Display File One Screen at a Time (No Editing)?
Answer: less
- Not: ls (lists files), cat (shows all at once), less (allows viewing/paging)
- less is read-only pager
10. Remove Duplicate Lines from Sorted Output?
Answer: uniq
- Must be used with sorted input:
sort file | uniq
Common Command Patterns
File Operations
cat file1 file2 > combined.txt # Combine files
sort file.txt > sorted.txt # Sort file
sort file | uniq > unique.txt # Remove duplicates
sort file | uniq -c > count.txt # Count duplicates
ls -a > list.txt # List all files to fileProcess Management
ps aux | grep firefox # Find Firefox process
kill 1234 # Kill process 1234
command & # Run in background
jobs # List background jobs
fg %1 # Bring job 1 to foregroundPipeline Combinations
cat file | sort | uniq # View unique lines
ls -l | wc -l # Count files
history | tail -20 # Last 20 commands
ps aux | grep process | wc -l # Count matching processes
tail -f /var/log/syslog | grep error # Monitor errorsScript Writing Checklist
Before Writing
- Plan what arguments are needed
- Identify required variables
- Determine conditional logic
Script Structure
- Start with
#!/bin/bash - Add comments explaining script
- Use proper variable assignment (no spaces around
=) - Use correct comparison operators (strings vs numbers)
- Ensure proper spacing in if statements
After Writing
- Make executable:
chmod 755 script.sh - Test with valid inputs
- Test with invalid inputs (error cases)
- Verify output matches requirements
Common Mistakes to Avoid
❌ WRONG: X = 5 (spaces around =) ✓ CORRECT: X=5
❌ WRONG: if [$X -eq 5]; then (no spaces around brackets) ✓ CORRECT: if [ $X -eq 5 ] ; then
❌ WRONG: if [ $X = 5 ] ; then (using = for numbers) ✓ CORRECT: if [ $X -eq 5 ] ; then
❌ WRONG: if [ $St -eq "Friday" ] ; then (using -eq for strings) ✓ CORRECT: if [ "$St" = "Friday" ] ; then
❌ WRONG: command > file (only redirects stdout, not stderr) ✓ CORRECT: command 2> file (for stderr) or command &> file(for both)
Quick Reference Summary
I/O Redirection
> # Redirect stdout (overwrite)
>> # Redirect stdout (append)
2> # Redirect stderr
&> # Redirect both stdout & stderr
| # Pipe output to next commandText Processing
cat file # Display file
sort file # Sort lines
uniq file # Remove adjacent duplicates
wc file # Count lines, words, bytes
grep pattern file # Search for pattern
head -n file # First n lines
tail -n file # Last n lines
tail -f file # Monitor file in real-timeProcess Control
ps aux # List all processes
kill PID # Terminate process
CTRL+C # Kill foreground process
CTRL+Z # Suspend foreground process
command & # Run in background
jobs # List jobs
fg %N # Foreground job N
bg %N # Background job NShell Scripting
#!/bin/bash # Shebang (first line)
$0, $1, $2, ... # Arguments
VarName=value # Assign variable (NO SPACES!)
$VarName # Use variable
chmod 755 script # Make executable
./script # Run script
# String comparison
[ "$var1" = "$var2" ] # Equal
[ "$var1" != "$var2" ] # Not equal
# Number comparison
[ $num1 -eq $num2 ] # Equal
[ $num1 -ne $num2 ] # Not equal
[ $num1 -gt $num2 ] # Greater than
[ $num1 -ge $num2 ] # Greater or equal
[ $num1 -lt $num2 ] # Less than
[ $num1 -le $num2 ] # Less or equalMemory Aids
Redirection: Think of > as an arrow pointing where output goes Pipeline: | looks like a pipe connecting commandsBackground: & at end = "and continue with terminal" PID: Process ID is the unique number (like ID card) CTRL+C: Cfor Cancel/Close (terminate) CTRL+Z: Z for snooZe (suspend) uniq: Only removes adjacent duplicates (must sort first!)Strings use =: Easy to remember: String = text Numbers use -eq: EQual with minus for math
Final Exam Tips
- Space matters in shell scripts! Especially in
ifstatements - NO spaces around
=in variable assignments - Use
2>for stderr, not just> - Always
sortbeforeuniq killneeds PID, get it fromps auxtail -ffor real-time log monitoring- String comparison:
=, Number comparison:-eq - Make scripts executable:
chmod 755 script - Shebang
#!/bin/bashmust be first line - Test your scripts with both valid and invalid inputs!
Good luck on your exam! 🚀