Shell scripting is the process of writing programs called scripts that leverage the power of the command-line interface (CLI) to automate tasks on Unix-based operating systems like Linux and macOS. These scripts essentially act as a series of commands bundled together for efficient execution.
Why Use Shell Scripting?
The primary purpose of shell scripting is to streamline repetitive tasks. Imagine manually copying and renaming dozens of files every day. A shell script can automate this process, saving you time and effort. Here are some key benefits:
- Automation: Shell scripts eliminate the need to repeatedly type the same commands, reducing human error and saving valuable time.
- Efficiency: Complex tasks involving multiple commands can be executed with a single script, improving workflow efficiency.
- Customization: Scripts can be tailored to specific needs, making them highly adaptable for various scenarios.
- Portability: In general, shell scripts written for one Unix-like system can run on others with minimal modifications.
- Resource Management: Scripts can automate system administration tasks like managing files, processes, and user accounts.
Common Shells for Scripting
Several popular shells provide environments for creating and running scripts. Here are a few:
- Bash (Bourne Again SHell): The most widely used shell, offering a rich set of features for scripting, including variables, conditional statements (like if/else), and functions. It's the default shell for many Linux distributions.
- sh (Bourne SHell): The original Unix shell, considered simpler and lighter than Bash. It's often used for scripting on systems requiring a minimalistic approach.
- zsh (Z SHell): An extension of Bash with additional features for customization and a powerful auto-completion function to suggest commands as you type. Popular among power users and developers.
- ksh (Korn SHell): Offers advanced scripting capabilities like working with associative arrays (flexible data structures) and complex mathematical operations. Primarily used in Unix environments.
Example: Automating File Backups
Here's a simplified example of a shell script that automatically compresses and backs up a directory named "documents" to a folder named "backups":
#!/bin/bash
# Script to backup documents directory
# Get the current date
date=$(date +%Y-%m-%d)
# Create a compressed backup archive with the date in the filename
tar -czvf backups/documents_$date.tar.gz documents/
echo "Documents backed up successfully to backups/documents_$date.tar.gz"This script demonstrates the basic structure of a shell script with comments explaining each step.
By learning shell scripting, you can unlock the power of automation and simplify your workflow on Unix-based systems.
This guide equips you with the basics of creating and running shell scripts on Unix-based systems.
Choosing a Text Editor:
Before diving in, you'll need a text editor to write your scripts. Here are some popular options, including a beginner-friendly choice:
- Classic Editors:
viandvim: Powerful but require some learning curve for beginners.emacs: Another powerful option with extensive customization possibilities.
- Modern Editors:
- Visual Studio Code: Free, user-friendly editor with excellent features for scripting.
- Sublime Text: Popular editor with a clean interface and scripting plugins.
- Beginner-Friendly:
- nano: A user-friendly editor pre-installed on most Unix systems, ideal for beginners.
Creating Your First Script:
-
Create a new file: Open your terminal and use the
nanocommand followed by the desired filename (ending in.sh):Bash
vim hello.sh
-
Write your script: Inside the nano editor, type the commands you want the script to execute, one line per command.
Example: Printing a Message
Here's a basic script named hello.sh that prints a message:
Bash
#!/bin/bash
echo "Hello, world!"-
Make it executable: Shell scripts need execution permission. Use the
chmodcommand:Bash
chmod +x hello.sh
This grants execute permission for the script owner.
-
Run the script: There are multiple ways to execute your script (same as before):
-
From the current directory:
Bash
./hello.sh
-
Using the full path:
Bash
/path/to/hello.sh
-
Specifying the interpreter (optional):
Bash
sh hello.sh # Assuming Bash is the default shell /bin/bash hello.sh
Each command above will execute the script and print "Hello, world!" on your terminal.
-
The first line of a shell script, often called the shebang line, starts with #! followed by the path to the interpreter. This tells the system which program to use to run the script. For Bash scripts, it's typically:
Bash
#!/bin/bash
Permissions Explained:
chmod +x script.sh: This grants execute permission (x) to the script owner, allowing it to be run.- Unix systems have various permissions: Read (
r), Write (w), and Execute (x). These can be set for the owner, group, and others.
- It's the command-line interpreter acting as your interface to interact with Unix-like operating systems.
- It provides a prompt (
$) where you type commands to execute. - Example:
$ lslists files and directories in the current directory.
Shell Variables:
- Store information that can be accessed by name.
- Assigned values using
=(no spaces). - Example:
$ my_var="Hello, World!" - Accessed with a
$prefix:$ echo $my_varechoes "Hello, World!"
Environment Variables:
- Special shell variables accessible to child processes.
- Store system-wide settings or configuration information.
- Examples:
PATH(directories to search for commands),HOME(user's home directory),USER(current user's username). - Set Environment Variables with
export - Example:
export VARIABLE_NAME=variable_value
Command Substitution:
- Incorporates a command's output as an argument to another command.
- Enclose the command within
$(). - Example:
$ echo "Today is $(date)"prints the current date.
Command-Line Arguments:
- Input values provided when executing a script.
- Accessed within the script using
$1,$2, etc. - Example:
Bash
#!/bin/bash
echo "The first argument is $1"
echo "The second argument is $2"$ ./my-script Hello World # Output: "The first argument is Hello" and "The second argument is World"
Working with Data in Shell Scripting Shell scripting empowers you to manipulate various data types, including: Strings: Textual data like names, messages, or file paths. Integers: Whole numbers used for calculations or counting. Files: Collections of data stored on disk. This guide explores how to handle data in your scripts effectively. Reading and Writing Files: Reading data: Use cat to display a file's contents entirely or tools like head and tail for specific portions. Bash cat input.txt # Read the entire content of input.txt head input.txt # Display the first few lines of input.txt
Writing data: Use echo or printf to create new files or append content to existing ones. Bash echo "Hello, World!" > output.txt # Create output.txt with "Hello, World!" echo "This is appended content." >> output.txt # Append text to output.txt
User Input and Output: Input: Capture user input using read. Bash echo "Enter your name:" read name echo "Hello, $name!" # Use the captured name in a greeting
Output: Provide informative messages for the user with echo or the more versatile printf. Bash printf "You entered: %s\n" "$name" # Formatted output with variable insertion
Text Manipulation: Shell scripting offers tools to transform text data: Substrings: Extract specific portions of a string using string slicing syntax "${variable:start:length}". Bash name="Hello, World!" echo "${name:0:5}" # Extract the first 5 characters (Hello)
Note: if you're referring to the position or length of a substring, those indices start from 0 Replacement: Modify parts of a string with string substitution "${variable/old/new}". Bash message="This is an ERROR message." echo "${message/ERROR/WARNING}" # Replace "ERROR" with "WARNING"
Case conversion: Change the case of text using parameter expansion.
Bash
name="HeLlO WoRlD"
echo
Performing Calculations: Shell scripting provides several ways to perform arithmetic operations on integers: let command: Assign the result of an expression to a variable. Bash let sum=10+20 echo $sum # Prints: 30
expr command: Evaluate an expression and display the result. Bash difference=$(expr 50 - 30) echo $difference # Prints: 20
Double parentheses: Perform arithmetic directly within an echo statement. Bash echo $((2 * 5 + 3)) # Prints: 13
Working with Arrays:
Arrays store collections of values in an ordered manner. Bash offers two ways to create arrays:
declare -a command: Explicitly declare an array and assign values.
Bash
declare -a fruits=("apple" "banana" "cherry")
echo
Direct assignment: Assign multiple values to an array variable. Bash colors=("red" "green" "blue") echo ${colors[0]} # Access the first element (red)
Shell Scripting
Controls Structure This guide dives deeper into controlling the flow of your shell scripts using conditional statements and loops. Conditional Statements (if, elif, else): Conditional statements allow your script to make decisions based on specific conditions. Here's the syntax: Bash #!/bin/bash if [ condition ]; then # Code to execute if condition is true elif [ condition2 ]; then # Code to execute if condition1 is false and condition2 is true (optional) else # Code to execute if both conditions are false (optional) fi
Example: Bash #!/bin/bash
number=20 if [[ $number -gt 15 ]]; then echo "The number is greater than 15." elif [[ $number -eq 15 ]]; then echo "The number is equal to 15." else echo "The number is less than 15." fi
Loops (for, while, until): Loops enable repeated execution of code blocks. Here's an overview of common loops: for loop: Iterates over a sequence of elements. Bash #!/bin/bash fruits=("apple" "banana" "cherry") for fruit in "${fruits[@]}"; do echo "I like to eat $fruit." done
while loop: Executes code as long as a condition is true.
Bash
#!/bin/bash
count=1
while [[ $count -le 5 ]]; do
echo "Iteration:
until loop: Executes code until a condition becomes true. Bash #!/bin/bash file="data.txt" until [[ -f $file ]]; do echo "Waiting for file: $file" sleep 5 # Wait 5 seconds done echo "File $file found!"
Case Statements: Case statements provide another way to handle multiple conditions. Bash #!/bin/bash
dayOfWeek=$(date +%u) # Get day of week (1-7) echo $dayOfWeek
case $dayOfWeek in 1 ) echo "Today is Monday." ;; 2 ) echo "Today is Tuesday." ;;
- ) # Default case (any other day) echo "It's a weekday." ;; esac
Remember: Use square brackets [] for condition evaluations in if statements. Double square brackets [[ ]] offer more flexibility for string comparisons. Indentation is crucial for defining code blocks within conditionals and loops. Consider using exit statements to signal script termination with specific codes (e.g., exit 0 for success, exit 1 for error). By effectively using these control flow constructs, you can create well-structured and dynamic shell scripts!
Functions and Modularization This guide explores functions, a cornerstone of well-organized and reusable shell scripts. Modularizing Code with Functions Functions are self-contained blocks of code that perform specific tasks. They enhance script maintainability and readability by: Reusability: A function can be called multiple times from different parts of your script or even from other scripts. Organization: Functions break down complex tasks into smaller, manageable units. Readability: Clear function names improve script understanding. Defining Functions: The function keyword followed by the function name and parentheses defines a function. Code within curly braces { } executes when the function is called. Bash #!/bin/bash function greet { echo "Hello, World" }
greet # Call the function
Function Parameters and Arguments
Functions can accept arguments (inputs) passed when you call them.
Inside the function, access these arguments using positional parameters like $1 (first argument), local for local variables
echo $sum
}
result=$(add 5 10) # Call the function and capture the "return" value echo $result # Prints: 15
Simulating Return Values: While Bash functions don't have built-in return values, you can simulate them by: Echoing the desired value within the function. Capturing the output using command substitution $(function_name arguments). Function Scope and Variable Visibility: Variables in Bash can be global (accessible throughout the script) or local (limited to the function's scope). By default, variables are global. Use the local keyword to declare variables local to a function, preventing them from affecting the global scope. Example: Bash #!/bin/bash var="global"
function test_var { local var="local" echo $var # Prints: local }
test_var echo $var # Prints: global (original value preserved)
Key Points: Functions promote code reusability, organization, and readability. Pass arguments to functions for customization. Simulate return values using echo and command substitution. Control variable scope using local for better data management. By mastering functions, you can create robust and modular shell scripts that tackle complex tasks efficiently.
Shell Scripting
File Handling Shell Scripting: File I/O and Management This guide equips you with essential file handling techniques in shell scripting: Creating and Deleting Files: touch: Creates an file. Bash #!/bin/bash touch new_file.txt
rm: Deletes a file (use with caution!). Bash #!/bin/bash rm important_file.txt # Be mindful of what you delete!
Reading and Writing Files: Reading: cat: Displays the entire content of a file. Bash #!/bin/bash cat message.txt
head: Shows the first few lines of a file. Bash #!/bin/bash head -n 3 data.csv # Display the first 3 lines
tail: Displays the last few lines of a file. Bash #!/bin/bash tail -n 2 log.txt # Display the last 2 lines
Directory Operations: Creating directories: mkdir: Creates a new directory. Bash #!/bin/bash mkdir new_directory
Navigating directories: cd: Changes the current working directory. Bash #!/bin/bash cd new_directory
Deleting directories: rmdir: Deletes an directory. Bash #!/bin/bash rmdir new_directory # Ensure the directory is before deletion
Remember: Be cautious when using rm to avoid accidental data loss. Consider using mv (move) to rename or relocate files within the filesystem. File permissions play a crucial role in system security. Understand permission settings before modifying them. By mastering these file handling techniques, you can effectively manage data in your shell scripts!
achieve this:
- Every shell command returns an exit code upon termination.
- By convention:
- Exit code of 0 indicates successful execution.
- Non-zero code signifies an error.
- The special variable
$?stores the exit code of the last executed command.
Bash
#!/bin/bash
command # This command might succeed or fail
exit_status=$?
if [[ $exit_status -eq 0 ]]; then
echo "Command executed successfully."
else
echo "Error: Command failed with exit code $exit_status."
fiWhile Bash doesn't have a native try-catch block, you can achieve similar functionality:
Bash
#!/bin/bash
{
# Code block to try (put potentially risky commands here)
command1 # This command might fail
} || {
# Code block to execute if command1 fails (catch block)
echo "An error occurred while executing command1."
# Log the error or take corrective actions
}In this example, if command1 fails, the code within the || block (catch block) executes.
Using command with set -e to check if a command inside a command fails
This approach utilizes the command builtin with set -e:
Bash
#!/bin/bash
set -e
# Use `command` instead of backticks or double quotes for stricter execution
command date
echo "Time is $(date)" # This echo will only execute if `date` succeededHere, command ensures the enclosed command (date) is executed directly by the shell, bypassing potential shell interpretations. If date fails, set -e will trigger the script to exit with a non-zero code.
Debugging Techniques:
set -x: Enables debug mode, printing each command before execution.echo: Print variables and command output to inspect script state at different points.trap: Captures signals likeSIGINT(Ctrl+C) for cleanup actions before termination.set -e: Exits the script immediately if any command returns a non-zero exit code (strict mode).set -u: Treats unset variables as errors, causing immediate script termination.set -o pipefail: Ensures pipelines exit with a non-zero code if any command within the pipeline fails.
Additional Tips:
- Use meaningful variable and function names to improve script readability.
- Add comments to explain complex logic or code sections.
- Test your scripts with various inputs and scenarios to uncover potential errors.
- Consider using logging mechanisms to record script execution details for further analysis.
By effectively implementing error handling and debugging practices, you can create reliable and robust shell scripts!
Advanced Scripting Techniques
Shell Scripting Advanced Techniques with Practical Examples
This guide empowers you with advanced shell scripting techniques, along with practical examples to elevate your scripts:
Functions and Modules: Break down complex logic into reusable functions and modular components for better organization and maintainability.
Input/Output Redirection: Master redirection operators (>, >>, <, |) to control data flow between commands, files, and the shell.
Command-Line Argument Parsing: Effectively process arguments passed to your script, enabling dynamic behavior based on user input.
String Manipulation: Utilize techniques like cut, sed, awk, and parameter expansion for advanced text processing and manipulation.
Regular Expressions (RegEx): Unleash the power of RegEx patterns to match complex text patterns within files and data streams.
Arrays and Associative Arrays: Store and manage collections of data using indexed arrays and associative arrays (key-value pairs) for efficient data organization.
Scripts(Part-1) 1. Introduction to Shell Scripting: System Info Script #!/bin/bash
echo "System Information"
echo "=================="
echo "Hostname:
echo "Welcome to Shell Scripting!"
echo "You are currently in the directory:
- Create the Script: Copy the above script into a text editor.
- Save the Script: Save it as welcome_script.sh or any other name you prefer, with a .sh extension.
- Make it Executable: Before running the script, you need to make it executable. This is done via the command line with the command chmod +x welcome_script.sh.
- Run the Script: After making it executable, you can run the script by typing ./welcome_script.sh in the terminal. This script is a basic example to get you started with shell scripting, demonstrating the essential components like the shebang line and simple output generation. Let me know if you need scripts for other topics from your list!
Shell Basics: A script showing the use of shell variables and command-line arguments. This script will illustrate the use of shell variables and how to handle command-line arguments in a script. #!/bin/bash
greeting="Hello"
echo "$greeting, $1!"
if [ -n "$2" ]; then echo "You also mentioned '$2'." else echo "No second argument was provided." fi
echo "Total number of arguments: $#"
Explanation:
• Shell Variable (greeting): A variable greeting is defined with the value "Hello".
• First Command-Line Argument ($1): The script uses $1 to refer to the first argument passed to it. For example, if you run ./script.sh World, World becomes $1.
• Conditional Statement (if [ -n "$2" ]): This checks if a second argument is provided.
- Save the Script: Copy and save this script in a file, for example, greeting_script.sh.
- Make it Executable: Use the command chmod +x greeting_script.sh to make it executable.
- Run the Script: Execute the script with command-line arguments, like ./greeting_script.sh User OptionalSecondArg. This script provides a basic introduction to using shell variables and handling command-line arguments, both of which are fundamental concepts in shell scripting. Feel free to reach out if you need examples for more advanced topics or other areas from your guide! 4 Control Structures: A script with conditional statements and loops. #!/bin/bash
read -p "Enter a number (1-5): " number
if [ "$number" -ge 1 ] && [ "$number" -le 5 ]; then echo "Your number is within the range 1-5." else echo "Your number is out of range." exit 1 fi
echo "Countdown from $number to 1:" while [ $number -gt 0 ]; do echo "$number" ((number--)) done
echo "Multiplication table for 3:"
for i in {1..5}; do
echo "3 x
- Create and Save the Script: Copy this script into a text file, for example, control_structures.sh.
- Make the Script Executable: Change the script's permissions with chmod +x control_structures.sh.
- Run the Script: Execute the script by typing ./control_structures.sh and following the prompt to enter a number. This script offers a basic understanding of how conditional statements and loops work in shell scripting. These structures are essential for creating dynamic and interactive scripts. Let me know if you need scripts covering other topics from your list!
Working with Data: Script Demonstrating Arrays and Arithmetic Operations This script will show how to use arrays and perform basic arithmetic operations in shell scripting. #!/bin/bash
numbers=(10 20 30 40 50)
echo "Array elements: ${numbers[@]}"
sum=0 for num in "${numbers[@]}"; do sum=$((sum + num)) done echo "Sum of array elements: $sum"
factor=2
echo "Array elements multiplied by
average=$((sum / ${#numbers[@]})) echo "Average of array elements: $average" Explanation: • Array Initialization: The script begins by defining an array numbers with a set of integers. • Display Array Elements: Using ${numbers[@]}, the script prints all elements of the array. • Summing Array Elements: A for loop iterates over the array, adding each element to sum. • Multiplying Elements: Another loop multiplies each element by a specified factor (2 in this case). • Average Calculation: The script calculates the average of the array elements. How to Use:
- Save the Script: Copy the above script into a text file, like data_operations.sh.
- Make It Executable: Use the command chmod +x data_operations.sh.
- Run the Script: Execute it with ./data_operations.sh. This script offers a basic introduction to handling arrays and performing arithmetic operations in shell scripting, which are key for data manipulation and processing tasks. Feel free to ask for more examples or scripts covering other topics in your guide!
| 6. | Functions and Modularization: A script defining and using a function with parameters. | ||
|---|---|---|---|
| This script will demonstrate how to define and use a function with parameters in shell scripting. | |||
| #!/bin/bash |
greet_user() { local name=$1 echo "Hello, $name!" }
calculate_square() { local number=$1 echo "$((number * number))" }
greet_user "Alice"
square=$(calculate_square 4) echo "Square of 4 is: $square" | | | | | Explanation: • Function Definition: The script defines two functions: greet_user and calculate_square. • local Keyword: Inside the functions, local keyword is used to declare function-local variables. • Function Parameters: The first parameter passed to the function is accessed via $1. • Calling Functions: The functions are then called with specific arguments. • Capturing Function Output: The output of calculate_square is captured in a variable square. | | | | | How to Use:
- Create the Script: Copy the script into a file, say functions_demo.sh.
- Make it Executable: Run chmod +x functions_demo.sh to make it executable.
- Execute the Script: Run it using ./functions_demo.sh. This script is a basic introduction to using functions in shell scripts, an essential aspect of script modularization and reuse. Functions make scripts more organized and easier to maintain, especially for complex tasks. If you have any more specific requirements or need scripts for other topics, feel free to ask! | | | | 1. | File Handling: Script for Reading from and Writing to a File | | | | | This script will demonstrate how to read from and write to a file, along with performing some basic file operations in shell scripting. | | | | | #!/bin/bash
filename="sample.txt"
if [ ! -f "$filename" ]; then echo "Creating $filename as it does not exist." touch "$filename" fi
echo "Writing to $filename." echo "This is a sample text file." > "$filename" echo "It contains some sample text." >> "$filename"
echo "Reading from $filename:" while IFS= read -r line; do echo "$line" done < "$filename"
echo "Appending more text to $filename." echo "This is another line of text." >> "$filename"
echo "Updated content of $filename:" cat "$filename"
| | Explanation: • File Check and Creation: The script first checks if the specified file exists. If not, it creates the file using touch. • Writing to a File: It writes and appends text to the file using > and >> redirection operators. • Reading from a File: The script reads and displays the file content using a while loop and read command. • Appending Text: It appends additional text to the file. • Displaying Updated Content: The updated file content is displayed using cat. • File Deletion: The script includes a commented out command to delete the file. Uncommenting rm "$filename" will delete the file. | | | | | How to Use:
- Save the Script: Copy this into a file, for instance, file_handling.sh.
- Make It Executable: Use chmod +x file_handling.sh to make it executable.
- Run the Script: Execute it with ./file_handling.sh. This script provides a basic understanding of file handling operations in shell scripting, such as creating, reading, writing, and appending files. If you need more complex examples or scripts for other topics, feel free to let me know! | | | | 8. | Error Handling and Debugging: Script with Error Trapping and Exit Status | | | | | #!/bin/bash
read_file() { local file=$1 if [ ! -f "$file" ]; then echo "Error: File '$file' not found." return 1 # Return with an error status fi
echo "Reading file '$file':"
cat "$file"
return 0 # Return with a success status
}
filename="example.txt"
trap 'echo "An error occurred."; exit 1' ERR
read_file "$filename"
if [
- Create the Script: Copy the script into a file, for example, error_handling.sh.
- Make it Executable: Change the script's permissions with chmod +x error_handling.sh.
- Run the Script: Execute the script by typing ./error_handling.sh. This script provides a basic introduction to error handling and debugging in shell scripts, particularly focusing on trapping errors and working with exit statuses. Advanced scripts often include more sophisticated error handling mechanisms to ensure robustness and reliability. If you need further examples or have other topics to cover, feel free to ask! | | | | 9. | Advanced Scripting Techniques: Script Utilizing Regular Expressions and Pattern Matching | | | | | This script will demonstrate the use of regular expressions and pattern matching in shell scripting, particularly for text processing tasks. | | | | | #!/bin/bash
validate_email() { local email=$1 local regex="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$"
if [[ $email =~ $regex ]]; then
echo "Valid email format."
else
echo "Invalid email format."
fi
}
extract_phone_numbers() { local text=$1 local phone_regex="([0-9]{3}-[0-9]{3}-[0-9]{4})"
echo "Extracted phone numbers:"
while read -r line; do
if [[ $line =~ $phone_regex ]]; then
echo "${BASH_REMATCH[1]}"
fi
done <<< "$text"
}
email="example@email.com" echo "Validating email: $email" validate_email "$email"
sample_text="Call me at 123-456-7890 or at 987-654-3210." echo "Extracting phone numbers from text:" extract_phone_numbers "$sample_text" | | | | | Explanation: • Function validate_email: This function uses a regular expression to validate the format of an email address. It employs the =~ operator for pattern matching in bash. • Function extract_phone_numbers: This function demonstrates how to extract phone numbers from a given text using regular expressions. It reads through lines of text and matches the pattern defined in phone_regex. | | | | | How to Use:
- Save the Script: Copy this script into a file, like regex_demo.sh.
- Make It Executable: Use chmod +x regex_demo.sh to make it executable.
- Run the Script: Execute it with ./regex_demo.sh. This script offers a glimpse into advanced scripting techniques involving regular expressions, which are powerful for text processing and pattern matching. Shell scripting provides robust tools for these tasks, making it useful for a wide range of applications. If you're interested in more advanced examples or scripts for other topics, feel free to let me know! | | | | 1. | Working with External Commands: A script showing the use of command execution and pipelines. | | | | | #!/bin/bash
display_processes() { local command_name=$1 echo "Processes running '$command_name':" ps aux | grep "$command_name" | grep -v grep }
count_file_lines() { local file=$1 local line_count=$(wc -l < "$file") echo "The file '$file' contains $line_count lines." }
command_to_check="bash" display_processes "$command_to_check"
file_to_check="sample.txt" count_file_lines "$file_to_check" | | | | | Explanation: • Function display_processes: This function uses ps aux to list all running processes, then filters them with grep to find processes related to a specific command. The grep -v grep part is used to exclude the grep command itself from the results. • Pipelines (|): The script uses pipelines to pass the output of one command (ps aux) as input to another command (grep). • Function count_file_lines: This function demonstrates how to use command substitution ($(...)) to capture the output of a command (wc -l) and then uses it in a script. | | | | | How to Use:
- Save the Script: Copy this script into a file, like external_commands.sh.
- Make It Executable: Change the permissions with chmod +x external_commands.sh.
- Run the Script: Execute it with ./external_commands.sh. This script provides a basic overview of using external commands and pipelines, which are fundamental concepts in shell scripting for processing and manipulating data streams. If you need more detailed scripts or have other topics you'd like to cover, feel free to ask! | | |
| 1. | System Health Check Script | |
|---|---|---|
| #!/bin/bash |
echo "CPU and Memory Usage:" top -bn1 | head -5 echo "Disk Usage:" df -h echo "Active Network Connections:" netstat -tuln | | | | Purpose: Regular health checks on servers for CPU, memory, disk, and network. | | | 2. | Backup Script | | | | #!/bin/bash
tar -czf /backup/$(date +%Y%m%d_%H%M%S)_backup.tar.gz /important/data | | | | Purpose: Automated backup of critical directories. | | | 3. | 3. Log File Cleaner | | | | #!/bin/bash
find /var/log -name "*.log" -type f -mtime +30 -exec rm -f {} ; | | | | Purpose: Cleanup old log files and free up space. | | | 1. | Deployment Script | | | | #!/bin/bash
git pull origin main npm install npm run build systemctl restart myapp | | | | Purpose: Pull the latest code, build, and restart the application. | | | 1. | 5. SSL Certificate Expiry Check | | | | #!/bin/bash
echo | openssl s_client -servername example.com -connect example.com:443 2>/dev/null | openssl x509 -noout -dates | | | | Purpose: Check SSL certificate expiry dates. | | | 1. | Docker Container Health Check | | | | #!/bin/bash
docker ps -q | xargs docker inspect --format '{{ .Id }}: Health={{ .State.Health.Status }}' | | | | Purpose: Monitor the health status of all running Docker containers. | | | 1. | Network Latency Test | | | | #!/bin/bash
ping -c 4 google.com | | | | Purpose: Simple network latency test to an external site. | | | 8. | Database Backup Script | | | | #!/bin/bash
mysqldump -u root -p mydatabase > /backup/mydatabase_$(date +%Y%m%d).sql | | | | Purpose: Backing up a MySQL database | | | 1. | User Account Audit | | | | #!/bin/bash
cut -d: -f1 /etc/passwd | | | 1. | Service Monitoring Script | | | | #!/bin/bash
systemctl status myservice | | | | Purpose: Check the status of a specific service | | | | | |
| 11. Checking Disk Space | |||
|---|---|---|---|
| #!/bin/bash |
df -h | grep -vE '^Filesystem|tmpfs|cdrom' | | | | | 12. Monitoring CPU Load | | | | | #!/bin/bash
uptime | | | | | 13. Restarting a Service if Not Running | | | | | #!/bin/bash
service=my_service if ! systemctl is-active --quiet $service; then systemctl restart $service fi | | | | | 14. Checking for Open Ports | | | | | #!/bin/bash
netstat -tulnp | | | | | 15.Updating System Packages | | | | | #!/bin/bash
sudo apt-get update && sudo apt-get upgrade -y | | | | | 16.Syncing Files with Remote Server | | | | | #!/bin/bash
rsync -avz /local/directory/ user@remote:/remote/directory/ | | | | | 17. Checking Website Availability | | | | | #!/bin/bash
curl -Is http://www.example.com/ | head -n 1 | | | | | 18. Database Health Check | | | | | #!/bin/bash
mysqladmin -u root -p status | | | | | 19. Extracting Logs for a Specific Date | | | | | #!/bin/bash
grep '2023-03-15' /var/log/myapp.log | | | | | 20.Listing Large Files | | | | | #!/bin/bash
find / -type f -size +100M | | |
| 21. Monitoring Memory Usage | |||
|---|---|---|---|
| #!/bin/bash |
free -m | | | | | 22. Checking HTTP Headers of a Website | | | | | #!/bin/bash
curl -I http://www.example.com/ | | | | | 23. Automating SSH into Server | | | | | #!/bin/bash
ssh user@server 'bash -s' < local_script.sh | | | | | 24. Creating a User Account | | | | | #!/bin/bash
sudo useradd -m newuser | | | | | 25. Killing a Process by Name | | | | | #!/bin/bash
pkill -f process_name | | | | | 26. Checking Inode Usage | | | | | #!/bin/bash
df -i | | | | | 27. Displaying Open Files by a Process | | | | | #!/bin/bash
lsof -p PID | | | | | 28. Automated FTP Upload | | | | | #!/bin/bash
ftp -n <<EOF open http://ftp.example.com/ user username password put local_file.txt remote_file.txt quit EOF | | | | | 29. Checking Server Uptime | | | | | #!/bin/bash
uptime -p | | | | | 30. Archiving Old Logs | | | | | #!/bin/bash
tar -czf logs-$(date +%Y%m%d).tar.gz /var/log/myapp/ | | | | | | | |
-
Time Announcer: Write a script that displays a personalized greeting along with the current time every minute. For example, "Good morning, Alice! It's Wednesday, 27 March 2024 11:15 AM." (Use
dateand a loop to update every minute). -
System Uptime Monitor: Write a script that displays the system uptime and refreshes it every 5 seconds. Use the
uptimecommand and a loop to achieve real-time updating. -
Interactive File Size Check: Write a script that prompts the user for a file path and displays the file size in real-time as the user types (using backspace for corrections). Employ the
readcommand and thedu -scommand to check size. -
Countdown with User Input: Write a script that asks the user for a duration in seconds and displays a countdown timer that updates every second until it reaches zero. Combine a loop with the
sleepcommand for real-time updates. -
Interactive Currency Converter: Write a script that prompts the user for an amount in their local currency and displays the equivalent value in another currency. Use a loop for continuous input and consider APIs for real-time conversion rates (may require additional research).
-
Simple File Change Monitor: Write a script that monitors a specific file for changes. If the file modification time changes, the script displays a message indicating the file has been modified. Use the
statcommand for comparisons. -
Real-Time Fortune Teller (Simple): Write a script that displays a random fortune message. Use a loop and pre-defined fortunes stored in an array for real-time display.
-
Simple System Info Display: Write a script that displays essential system information like the current user, hostname, and operating system version in real-time. Update the script to display this information periodically (e.g., every minute). (Use commands like
whoami,hostname, anduname -rto retrieve the information). -
Free Disk Space Monitor: Write a script that checks the free disk space on a specific partition (e.g.,
/) and displays the available space in megabytes (MB) or gigabytes (GB). Update the information periodically (e.g., every minute) using a loop. -
Simple File Watcher: Write a script that monitors a specific file for changes (modifications). Use a loop to check the file's last modified time and display a message if it has been updated since the script started.
-
Network Health Monitoring Tool
Objective: Create a script that checks the network health of your server. It should ping a set of predefined IPs or domain names and report any failures along with the timestamp. The script should log this information and send an alert (e.g., via email) if any of the pings fail.
- Log File Analyzer
Develop a script that analyzes a specified log file (e.g., Apache or Nginx access log) and extracts information such as the most frequent IP addresses and request URLs. The script should also identify any error codes (like 404 or 500) and count their occurrences.
- User Account and Permission Audit Script
Objective: Create a script that audits user accounts and file permissions in a system. The script should list all users, their last login time, and check for any files in sensitive directories (like /etc or /var) with permissions that are too permissive (e.g., world-writable files).
- Interactive System Resource Monitor (Graphical): Write a script that monitors CPU, memory, and network usage. Utilize libraries like
ncursesto display them in a user-configurable graphical format (e.g., bar charts, gauges). Allow users to choose the refresh rate and potentially switch between different display modes.
2. Service Monitoring Script: Develop a script that monitors critical system services (e.g., Apache, SSH, MySQL) and restarts them automatically if they are found to be down. Include logging functionality to record service status changes.
3. User Account Management Tool: Build a shell script for managing user accounts on a Linux system. The script should allow administrators to add, modify, and delete user accounts, set password policies, and manage user permissions.
- File Encryption/Decryption Tool: Develop a shell script that encrypts and decrypts files using symmetric encryption algorithms (e.g., AES). Ensure that the script prompts the user for a passphrase and supports options for both encryption and decryption.
5. Database Backup and Restore: Develop a shell script that automates the backup and restore process for databases such as MySQL or PostgreSQL. The script should dump the database contents, compress them, and store them securely. It should also be able to restore databases from backup files.
- Traffic Monitoring:
- Task: Run a script every 5 minutes to collect network traffic statistics. If the incoming traffic exceeds a certain threshold for three consecutive checks, send an email alert to the network administrator.
- Focus: Scripting logic, conditional cron execution, persistence for traffic tracking.
- Emailing System Logs:
- Task: Set up a cron job to compress system logs daily at 2:30 AM, delete compressed log files older than 30 days, and email a report of successful and failed compressions to the system administrator.
- Focus: File manipulation, error handling, email integration