This session builds upon basic shell operations by introducing the core concepts of writing executable Bash scripts.
Learning Objectives
- Understand the structure and execution requirements of a shell script.
- Learn how to use the shebang line, manage script permissions, and handle standard input/output streams.
- Master the use of variables, both local and environment-specific.
- Effectively utilize command substitution for capturing command output.
Topics Covered
1. The Anatomy of a Script
- The Shebang Line (
#!): Understanding#!/bin/bashand why it's essential for script execution context. - Script Permissions: Using
chmod +x script_name.shto make a script executable. - Comments: Using the
#symbol for documenting script logic.
2. Variables and Data Types
- Declaring Variables:
MY_VAR="hello"(No spaces!). Usereadonly MY_VARto prevent modification. - Referencing Variables: Use
${MY_VAR}to avoid ambiguity (e.g.,${USER}_file). - Arithmetic Expansion: Use
$(( ... ))for basic math.
bash
count=5
echo "Next is $((count + 1))"
- Parameter Expansion (Advanced):
${VAR:-default}: UsedefaultifVARis unset or empty.${VAR:0:5}: Extract the first 5 characters.${#VAR}: Get the length of the string.${VAR/old/new}: Replace the first occurrence ofoldwithnew.
3. Input and Output
- Reading User Input: Using the
readcommand to prompt the user for data dynamically. - Command Substitution: Capturing the output of a command using backticks (
`command`) or modern$()syntax (e.g.,CURRENT_DATE=$(date)).
4. Script Arguments (Positional Parameters)
- Accessing Arguments:
$0(script name),$1...$9. For arguments above 9, use${10}. - Special Parameters:
$#: Number of arguments passed.$@: All arguments as separate quoted strings (Recommended!).$*: All arguments as a single string.$$: Process ID (PID) of the current script.$!: PID of the last background job.
Lab/Assessment Focus
Goal: Create an enhanced user_info.sh script.
- Argument Handling: Check if a username was provided as
$1. Use parameter expansion${1:-$DEFAULT_USER}to set a default if none is provided. - Input: If no argument is provided, prompt for it with a 10-second timeout.
- Command Substitution: Capture the User ID (
id -u $USER) and the number of currently logged-in users (who | wc -l). - Math: Calculate how many more users can join if the limit is 100 using
$((100 - user_count)). - Output: Display a detailed summary including the script's own PID (
$$).
Advanced Topic References
ShellCheckGuide: Static analysis tool for shell scripts.- Advanced Bash-Scripting Guide - Variables: Detailed variable documentation.
- The difference between
$*and$@in Bash: Understanding argument expansion.