Creating Your First Shell Script
This tutorial demonstrates how to build a basic interactive shell script covering essential programming concepts including conditional statemants, fille operations, and permission management.
Setting Up the Script Environment
Create and open a new script file using Vim:
vim ./demo.sh
Vim operates in two primary modes: Command mode for executing instructions and Insert mode for typing content. Press i to enter Insert mode, then type your script. Save and exit by pressing Esc followed by :wq.
Quick Vim tips:
Esc yycopies a lineEsc ppastes a line
Make the script executable:
chmod +x ./demo.sh
./demo.sh
Understanding the If Statement
The if statement controls program flow based on conditions. Each conditional test using [ ] requires spaces around the brackets:
if [ "$variable" = "value" ]; then
echo "Condition met"
elif [ "$variable" = "other" ]; then
echo "Alternative condition"
else
echo "Default action"
fi
Script Features
The following script implements four menu options:
- Display hostname and IP address
- Create directories or files
- Modify file permissions
- Exit the script
Displaying System Information
hostname # Shows the system hostname
hostname -I # Shows the IP address
Creating Directories and Files
The script prompts for a working directory, validates it exists, then creates either a file or directory based on whether the name contains a file extension:
# Check if path exists
if [ -d "$workspace" ]; then
# Create file if name matches pattern (contains dot)
if [[ "$name" =~ \..+ ]]; then
touch "$name"
else
mkdir "$name"
fi
fi
Modifying File Permissions
The chmod command changes access permissions using numeric or symbolic notation. Linux permissions consist of nine characters representing three permission sets:
- Owner permissions (characters 1-3): read, write, execute
- Group permissions (characters 4-6): read, write, execute
- Other permissions (characters 7-9): read, write, execute
Numeric values:
- Read (r): 4
- Write (w): 2
- Execute (x): 1
Examples:
chmod 755 file.shsets rwxr-xr-xchmod 721 file.shsets rwx-w---xchmod 777 file.shsets rwxrwxrwx
Verify permissions with ls -l.
Complete Script Example
#!/bin/bash
echo $(date)
printf "%-8s %-20s\n" 'Option' 'Description'
printf "%-8s %-20s\n" '1' 'Show Hostname and IP'
printf "%-8s %-20s\n" '2' 'Create Directory or File'
printf "%-8s %-20s\n" '3' 'Change Permissions'
printf "%-8s %-20s\n" '4' 'Exit'
echo "Enter your selection:"
read choice
if [ "$choice" -eq 1 ]; then
printf "%-12s %s\n" 'Hostname:' $(hostname)
printf "%-12s %s\n" 'IP Address:' $(hostname -I)
elif [ "$choice" -eq 2 ]; then
echo "Enter the absolute path for your workspace:"
read workspace
if [ -d "$workspace" ]; then
cd "$workspace"
echo "Working directory: $(pwd)"
ls
echo "Enter name for directory or file to create:"
read itemname
if [[ "$itemname" =~ \..+ ]]; then
touch "$itemname"
else
mkdir "$itemname"
fi
echo "Updated workspace contents:"
ls
else
echo "Error: Specified path does not exist"
fi
elif [ "$choice" -eq 3 ]; then
echo "Enter the absolute path for your workspace:"
read workspace
if [ -d "$workspace" ]; then
cd "$workspace"
echo "Working directory: $(pwd)"
ls -l
echo "Enter directory or file name:"
read itemname
if [ -e "$itemname" ]; then
echo "Enter permission number (e.g., 755, 644):"
read permission
chmod "$permission" "$itemname"
echo "Permissions updated:"
ls -al "$itemname"
else
echo "Error: Specified item does not exist"
fi
else
echo "Error: Specified path does not exist"
fi
else
echo "Exiting script"
exit 0
fi
Running the Script
Execute the script and select options from the menu. Each option performs the corresponding operation:
- Option 1 displays system network information
- Option 2 creates new filesystem objects
- Option 3 changes access permissions
- Option 4 terminates the script