Tech Tutorial: 105.2 Customize or write simple Bash scripts #
Introduction #
Bash (Bourne Again SHell) is a powerful shell and scripting language used extensively in the Linux environment. It offers extensive capabilities to automate tasks, manipulate files, and manage system operations. This tutorial aims to provide an understanding of how to customize existing scripts and write simple new Bash scripts, a critical skill for any Linux system administrator or developer.
Key Knowledge Areas: #
- Basic Bash scripting syntax
- Using variables and loops
- Conditional statements
- Input and output handling
- Common text processing utilities
Step-by-Step Guide #
1. Basic Bash Scripting Syntax #
Every Bash script starts with a shebang (#!
) followed by the path to the Bash interpreter:
#!/bin/bash
echo "Hello, World!"
This script outputs Hello, World!
. The echo
command is used for displaying lines of text or variables.
2. Using Variables and Loops #
Variables #
In Bash, you can create variables to store data such as strings, numbers, or the output of a command.
#!/bin/bash
greeting="Welcome"
user=$(whoami)
echo "$greeting, $user!"
This script stores a greeting message in a variable and retrieves the current user’s name, outputting a personalized welcome message.
Loops #
Loops allow you to execute commands repeatedly. Here’s an example using a for
loop:
#!/bin/bash
for number in {1..5}
do
echo "Iteration $number"
done
This loop will print “Iteration” followed by the numbers 1 through 5.
3. Conditional Statements #
You can make decisions in your scripts with if
, else
, and elif
.
#!/bin/bash
read -p "Enter a number: " num
if [[ $num -gt 10 ]]
then
echo "The number is greater than 10."
elif [[ $num -eq 10 ]]
then
echo "The number is equal to 10."
else
echo "The number is less than 10."
fi
This script prompts the user to enter a number and displays a message based on the number entered.
4. Input and Output Handling #
Handling user input and file output is crucial for interactive scripts.
Reading User Input #
#!/bin/bash
echo "What is your name?"
read name
echo "Hello, $name!"
Redirecting Output to a File #
#!/bin/bash
echo "Logging you in..."
date > login.txt
echo "Login date recorded in login.txt"
This script writes the current date to login.txt
.
5. Common Text Processing Utilities #
Bash has several utilities for text processing:
grep
#
Searches for patterns in text.
echo -e "apple\nbanana\ncarrot" | grep "banana"
awk
#
Pattern scanning and processing language.
echo -e "John Doe 5000\nJane Doe 6000" | awk '{print $1 " earns " $3}'
sed
#
Stream editor for filtering and transforming text.
echo "Welcome to Linux" | sed 's/Linux/UNIX/'
Detailed Code Examples #
- Using
grep
to Filter Log Entries:
cat /var/log/system.log | grep "ERROR"
- Generating Reports with
awk
:
cat sales.txt | awk '{total += $3} END {print "Total sales: " total}'
- Modifying Configuration Files with
sed
:
sed -i 's/localhost/127.0.0.1/g' /etc/hosts
Conclusion #
Bash scripting is an essential skill for automating tasks in a Linux environment. By understanding how to customize and write simple scripts, you can significantly enhance your productivity and system management capabilities. Practice regularly and explore more complex scripts to deepen your expertise.