Overview
This lecture series introduces core concepts of shell scripting, including operating system basics, scripting syntax, file and process management, text manipulation, and command-line utilities, with practical script examples.
Kernel and Shell Basics
- The kernel is the core of an OS, managing CPU, memory, storage, and system resources.
- The shell is a program that translates user commands for the operating system to execute.
- Shell scripts allow writing multiple commands in a file to automate tasks.
- Shell scripts use features like variables, loops, and conditionals.
Variables in Shell Scripts
- Variables do not require type definition and can be system-defined (environment) or user-defined.
- System variables can be listed with
printenv
; user variables exist only during script execution.
- Assign a variable with
name=value
and access it with $name
.
- The
readonly
keyword makes a variable immutable.
Control Structures: Conditionals and Loops
- Comparison operators include
-eq
, -ne
, -gt
, -lt
, -ge
, -le
, =
, and !=
.
- Conditional statements use
if
, else
, and case
for decision making.
- Loops include
for
, while
, and until
; break
exits the loop, continue
skips to next iteration.
- Arrays can be iterated within loops.
Script Arguments and Parameters
- Positional arguments (
$1
, $2
, ...) are passed to scripts during execution.
- Special parameters:
$0
(script name), $#
(argument count), $@
or $*
(all arguments), $?
(last command exit status).
shift
command removes the first positional argument in a loop.
File Operations in Shell Scripts
- Common file operations: read (
cat
), append, move (mv
), rename, permission change (chmod
), delete (rm
).
- Check file existence with
-e
, and check size with -s
.
- Read files line by line using
read
.
Input/Output, Redirection, and Piping
- Output redirection (
>
overwrites, >>
appends), input redirection (<
).
- Combine outputs and errors with
2>&1
; discard output with /dev/null
.
- Pipe operator (
|
) sends output of one command as input to another.
- Here documents (
<<EOF ... EOF
) allow multi-line string input.
Text Processing Tools
grep
searches and filters text; supports options like -i
(ignore case), -n
(line numbers), -v
(invert match), etc.
sed
edits text streams for replacing, inserting, deleting lines.
- Regular expressions (regex) assist with flexible pattern matching.
Advanced Scripting Features
- Aliases create shortcuts for commands; enable alias expansion in scripts with
shopt -s expand_aliases
.
wait
synchronizes script execution with background processes.
trap
handles signals (e.g., SIGINT) for custom script responses.
- Debugging scripts with
set -x
to trace execution.
Common User and Process Management
- Check users, last logins, and group info using system commands in scripts.
- Automate removal of zero-byte files and other process automation using shell script.
Key Terms & Definitions
- Kernel — The OS core managing system resources.
- Shell — Interface translating user commands for the OS.
- Variable — Stores data within scripts.
- Redirection — Changing default I/O destinations in commands.
- Pipe — Connects command outputs/inputs.
- Here document — Multi-line input block for a command.
- Alias — Shortcut for a command.
- Trap — Command to handle signals.
- Positional Arguments — Variables for command-line arguments in scripts.
- Exit Status — Numeric code indicating command success (0) or failure.
Action Items / Next Steps
- Practice creating shell scripts for each topic (variables, loops, conditionals, file ops).
- Experiment with
grep
, sed
, and regular expressions on sample files.
- Complete any assigned hands-on tasks from the lesson.
- Review and test scripts using debugging techniques discussed.