Lab 2

Updated 4 Oct 2026

I/O Redirection Fundamentals

Three Standard Streams

StreamFile DescriptorDefaultPurpose
stdin0KeyboardInput to programs
stdout1ScreenNormal output
stderr2ScreenError messages

Key Concept: stdout and stderr are separate streams, allowing you to handle errors differently from normal output.


Redirection Operators

Standard Output Redirection

OperatorFunctionExampleResult
>Redirect stdout (overwrite)ls > file.txtOverwrites file.txt with output
>>Redirect stdout (append)ls >> file.txtAdds to end of file.txt
2>Redirect stderr (overwrite)ls 2> err.txtSends errors to err.txt
2>>Redirect stderr (append)ls 2>> err.txtAppends errors to err.txt
&>Redirect both stdout and stderrls &> all.txtBoth 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 file

Pipelines

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 directory

Analogy: 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 nano

Key Controls:

KeyAction
CTRL + XExit nano
CTRL + OSave (Write Out)
CTRL + KCut line
CTRL + UPaste (Uncut)
CTRL + WSearch
CTRL + \Replace
CTRL + GHelp

Typical Workflow:

  1. Type/edit content
  2. Press CTRL + X to exit
  3. Press Y to save changes
  4. Press Enter to 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 sort

Input (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 lines

CRITICAL: 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 c

wc - Word Count

wc file                           # Show lines, words, bytes
wc -l file                        # Lines only
wc -w file                        # Words only
wc -c file                        # Bytes only

Example Output:

$ wc Ex4.txt
6  6 12 Ex4.txt

Interpretation: 6 lines, 6 words, 12 bytes, filename

Other Useful Commands

CommandDescriptionExample
grep pattern fileFind lines matching patterngrep "error" log.txt
head -n fileShow first n lineshead -10 file.txt
tail -n fileShow last n linestail -20 log.txt
tail -f fileMonitor file in real-timetail -f app.log
cut -d: -f1 fileExtract columnscut -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

StateDescription
RunningActively executing
SleepingWaiting for event
StoppedSuspended (paused)
ZombieFinished 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 process

Example 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 q to 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#] PID

Process Control Keys

KeyActionResult
CTRL + CTerminate (SIGTERM)Stops foreground process immediately
CTRL + ZStop/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 PIDs

Example 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 foreground

bg - Resume in Background

bg                                # Resume most recent stopped job in background
bg %1                             # Resume job [1] in background

Workflow Example:

$ nano file.txt                   # Start editing
[Press CTRL+Z]                    # Suspend nano
[1]+  Stopped                nano file.txt
$ ls                              # Do other work
$ fg                              # Return to nano

Killing Processes

kill - Terminate Process

kill PID                          # Terminate process by PID
kill -9 PID                       # Force kill (SIGKILL)
kill %1                           # Kill job [1]

Common Signals:

SignalNumberEffect
SIGTERM15 (default)Polite termination request
SIGKILL9Force kill (cannot be ignored)
SIGSTOP19Stop/pause process
SIGCONT18Continue stopped process

Examples:

kill 8050                         # Terminate process with PID 8050
kill -9 8050                      # Force kill PID 8050
killall firefox                   # Kill all firefox processes

Shell Scripting Fundamentals

Script Structure

Basic Template

#!/bin/bash
# Comment: Description of script
 
# Your commands here
echo "Hello World"

Required Elements:

  1. Shebang: #!/bin/bash (MUST be first line)
  2. Comments: Lines starting with #
  3. Commands: Actual executable statements

Making Scripts Executable

chmod 755 script.sh               # Make executable
chmod +x script.sh                # Alternative method
./script.sh                       # Execute script

Why ./?

  • Current directory not in PATH by default
  • ./ tells shell to look in current directory

Command Line Arguments

Accessing Arguments:

VariableMeaning
$0Script name itself
$1First argument
$2Second 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: 2

Variables

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 quotes

WRONG:

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 another

Example 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
fi

CRITICAL SPACING RULES:

  • Space after [
  • Space before ]
  • Space around operators
  • fi ends the if statement

Comparison Operators

For Strings:

OperatorMeaningExample
=Equal[ "$var1" = "$var2" ]
!=Not equal[ "$var1" != "$var2" ]

For Numbers:

OperatorMeaningExample
-eqEqual[ $num1 -eq $num2 ]
-neNot equal[ $num1 -ne $num2 ]
-gtGreater than[ $num1 -gt $num2 ]
-geGreater than or equal[ $num1 -ge $num2 ]
-ltLess than[ $num1 -lt $num2 ]
-leLess 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."
fi

Output:

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."
fi

Output:

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"
fi

Problem: Using = for numbers instead of -eq

Correct Version:

if [ $X -eq $Y ] ; then  # CORRECT!
    echo "Equal"
fi

Quiz 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

  • -a shows hidden files (start with .)
  • > redirects output to file

4. Suspend Foreground Process and Verify Still Running?

Answer:

  1. Press CTRL + Z to suspend (stop/pause)
  2. Run ps aux to verify process still exists
  • Alternatively: jobs shows background/stopped jobs

5. Input Required for kill Command?

Answer: PID (Process ID)

  • Not PIP, DIP, or VSIZ
  • Get PID from ps aux command

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>

  • 2 is 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 file

Process 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 foreground

Pipeline 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 errors

Script 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 command

Text 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-time

Process 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 N

Shell 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 equal

Memory 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

  1. Space matters in shell scripts! Especially in if statements
  2. NO spaces around = in variable assignments
  3. Use 2> for stderr, not just >
  4. Always sort before uniq
  5. kill needs PID, get it from ps aux
  6. tail -f for real-time log monitoring
  7. String comparison: =, Number comparison: -eq
  8. Make scripts executable: chmod 755 script
  9. Shebang #!/bin/bash must be first line
  10. Test your scripts with both valid and invalid inputs!

Good luck on your exam! 🚀