Advanced Linux Shell Scripting Techniques
Variable Usage
1. Arrays for Multiple Values:
Use arrays to store multiple values in a single variable.
fruits=(“apple” “banana” “cherry”)
echo “First fruit: ${fruits[0]}”
2. Associative Arrays:
Utilize associative arrays to map keys to values for advanced data storage.
declare -A colors
colors[“red”]=”#FF0000″
echo “Color code for red: ${colors[“red”]}”
Command Line Arguments
1. Parsing Long Options with `getopts`:
Extend `getopts` to handle long options in your scripts.
while getopts “:a:b:” opt; do
case $opt in
a)
arg_a=”$OPTARG”
;;
b)
arg_b=”$OPTARG”
;;
\?)
echo “Invalid option: -$OPTARG”
;;
esac
done
2. Processing Flag Arguments:
Process flags using a loop to handle multiple options effectively.
while [ $# -gt 0 ]; do
case “$1” in
-a)
echo “Option -a detected”
;;
-b)
echo “Option -b detected”
;;
*)
echo “Unknown option: $1”
;;
esac
shift
done
Debugging and Error Handling
1. Exit Traps for Cleanup:
Ensure cleanup operations with exit traps before script termination.
cleanup() {
echo “Cleaning up…”
}
trap cleanup EXIT
2. Checking Command Success with `if` and `$?`:
Use `$?` to check the exit status of a command for success or failure.
some_command
if [ $? -eq 0 ]; then
echo “Command succeeded”
else
echo “Command failed”
fi
Advanced Flow Control
1. Creating a Menu with `select`:
Employ the `select` loop to build interactive menus for user choices.
options=(“Option 1” “Option 2” “Quit”)
select choice in “${options[@]}”; do
case $choice in
“Option 1”)
echo “You selected Option 1”
;;
“Option 2”)
echo “You selected Option 2”
;;
“Quit”)
break
;;
*)
echo “Invalid option”;;
esac
done
2. Looping Over Files with Spaces:
Use `while` and `read` to iterate over files with spaces in their names.
find . -type f -print0 | while IFS= read -r -d $’\0′ file; do
echo “File: $file”
done
Advanced `sed` Examples
1. In-place Editing with Backup:
Edit a file in place while creating a backup of the original file with a `.bak` extension.
sed -i.bak ‘s/pattern/replacement/’ file.txt
2. Using Extended Regular Expressions (ERE):
Use `-E` flag to enable extended regular expressions for more advanced pattern matching.
sed -E ‘s/(pattern1|pattern2)/replacement/’ file.txt
3. Extracting URLs from HTML:
Extract all URLs from an HTML file.
sed -n ‘s/.*href=”\([^”]*\)”.*/\1/p’ index.html
Advanced `awk` Examples
1. Summing Column Values:
Sum the values in the second column of a space-separated file.
awk ‘{ sum += $2 } END { print sum }’ data.txt
2. Printing Fields Based on Condition:
Print lines where the second column value is greater than 10.
awk ‘$2 > 10 { print }’ file.txt
3. Grouping and Aggregating Data:
Group data by the first column and calculate the sum of the second column for each group.
awk ‘{ group[$1] += $2 } END { for (item in group) print item, group[item] }’ data.txt
Advanced Options
1.Parallel Execution with `xargs`:
Use `xargs` to run tasks in parallel using multiple CPU cores.
cat files.txt | xargs -P4 -n1 process_file.sh
2. Process Substitution:
Use process substitution to pass the output of a command as a file to another command.
diff <(command1) <(command2)
3. Creating Command Pipelines:
Combine multiple commands using pipes for efficient data processing.
cat access.log | grep “404” | awk ‘{ print $7 }’ | sort | uniq -c
4. Advanced `find` Usage:
Search for files modified within the last 7 days and remove them.
find /path/to/directory -type f -mtime -7 -exec rm {} +
5. Using `parallel` for Parallel Tasks:
Use the `parallel` command to execute multiple instances of a command in parallel.
parallel -j 4 echo ::: one two three four
File Manipulation and Advanced Commands
1. Finding and Deleting Empty Directories:
Use `find` to locate and delete empty directories.
find /path/to/directory -type d -empty -delete
2. Batch Renaming Files:
Rename files in a directory using a prefix and a counter.
counter=1
for file in *.txt; do
mv “$file” “newfile_$counter.txt”
counter=$((counter + 1))
done
3. Creating and Extracting Archives:
Create a compressed archive using `tar` and `gzip`.
tar czvf archive.tar.gz files/
Script Optimization and Best Practices
1. Optimizing with Functions and Modularity:
Use functions to modularize scripts and improve readability.
process_data() {
# Logic for processing data
}
process_data
2. Using Constants with `readonly`:
Declare constants using the `readonly` command.
readonly PI=3.14159
3. Using `#!/bin/bash -e` for Error Handling:
Make the script exit immediately on error.
#!/bin/bash -e
4. Commenting for Clarity:
Add comments to explain complex logic and enhance script readability.
# Loop through files and perform operations
for file in *.txt; do
# Process each file here
done
These advanced Linux shell scripting techniques provide a solid foundation for creating more sophisticated and efficient scripts. Experiment with these concepts to enhance your scripting skills further.
You may also like:
https://hackedyou.org/mastering-sed-in-linux-scripting/
https://hackedyou.org/mastering-awk-in-linux-scripting/
Advanced Linux Shell Scripting
Advanced Linux Shell Scripting Techniques
Variable Usage
1. Arrays for Multiple Values:
Use arrays to store multiple values in a single variable.
fruits=(“apple” “banana” “cherry”)
echo “First fruit: ${fruits[0]}”
2. Associative Arrays:
Utilize associative arrays to map keys to values for advanced data storage.
declare -A colors
colors[“red”]=”#FF0000″
echo “Color code for red: ${colors[“red”]}”
Command Line Arguments
1. Parsing Long Options with `getopts`:
Extend `getopts` to handle long options in your scripts.
while getopts “:a:b:” opt; do
case $opt in
a)
arg_a=”$OPTARG”
;;
b)
arg_b=”$OPTARG”
;;
\?)
echo “Invalid option: -$OPTARG”
;;
esac
done
2. Processing Flag Arguments:
Process flags using a loop to handle multiple options effectively.
while [ $# -gt 0 ]; do
case “$1” in
-a)
echo “Option -a detected”
;;
-b)
echo “Option -b detected”
;;
*)
echo “Unknown option: $1”
;;
esac
shift
done
Debugging and Error Handling
1. Exit Traps for Cleanup:
Ensure cleanup operations with exit traps before script termination.
cleanup() {
echo “Cleaning up…”
}
trap cleanup EXIT
2. Checking Command Success with `if` and `$?`:
Use `$?` to check the exit status of a command for success or failure.
some_command
if [ $? -eq 0 ]; then
echo “Command succeeded”
else
echo “Command failed”
fi
Advanced Flow Control
1. Creating a Menu with `select`:
Employ the `select` loop to build interactive menus for user choices.
options=(“Option 1” “Option 2” “Quit”)
select choice in “${options[@]}”; do
case $choice in
“Option 1”)
echo “You selected Option 1”
;;
“Option 2”)
echo “You selected Option 2”
;;
“Quit”)
break
;;
*)
echo “Invalid option”;;
esac
done
2. Looping Over Files with Spaces:
Use `while` and `read` to iterate over files with spaces in their names.
find . -type f -print0 | while IFS= read -r -d $’\0′ file; do
echo “File: $file”
done
Advanced `sed` Examples
1. In-place Editing with Backup:
Edit a file in place while creating a backup of the original file with a `.bak` extension.
sed -i.bak ‘s/pattern/replacement/’ file.txt
2. Using Extended Regular Expressions (ERE):
Use `-E` flag to enable extended regular expressions for more advanced pattern matching.
sed -E ‘s/(pattern1|pattern2)/replacement/’ file.txt
3. Extracting URLs from HTML:
Extract all URLs from an HTML file.
sed -n ‘s/.*href=”\([^”]*\)”.*/\1/p’ index.html
Advanced `awk` Examples
1. Summing Column Values:
Sum the values in the second column of a space-separated file.
awk ‘{ sum += $2 } END { print sum }’ data.txt
2. Printing Fields Based on Condition:
Print lines where the second column value is greater than 10.
awk ‘$2 > 10 { print }’ file.txt
3. Grouping and Aggregating Data:
Group data by the first column and calculate the sum of the second column for each group.
awk ‘{ group[$1] += $2 } END { for (item in group) print item, group[item] }’ data.txt
Advanced Options
1.Parallel Execution with `xargs`:
Use `xargs` to run tasks in parallel using multiple CPU cores.
cat files.txt | xargs -P4 -n1 process_file.sh
2. Process Substitution:
Use process substitution to pass the output of a command as a file to another command.
diff <(command1) <(command2)
3. Creating Command Pipelines:
Combine multiple commands using pipes for efficient data processing.
cat access.log | grep “404” | awk ‘{ print $7 }’ | sort | uniq -c
4. Advanced `find` Usage:
Search for files modified within the last 7 days and remove them.
find /path/to/directory -type f -mtime -7 -exec rm {} +
5. Using `parallel` for Parallel Tasks:
Use the `parallel` command to execute multiple instances of a command in parallel.
parallel -j 4 echo ::: one two three four
File Manipulation and Advanced Commands
1. Finding and Deleting Empty Directories:
Use `find` to locate and delete empty directories.
find /path/to/directory -type d -empty -delete
2. Batch Renaming Files:
Rename files in a directory using a prefix and a counter.
counter=1
for file in *.txt; do
mv “$file” “newfile_$counter.txt”
counter=$((counter + 1))
done
3. Creating and Extracting Archives:
Create a compressed archive using `tar` and `gzip`.
tar czvf archive.tar.gz files/
Script Optimization and Best Practices
1. Optimizing with Functions and Modularity:
Use functions to modularize scripts and improve readability.
process_data() {
# Logic for processing data
}
process_data
2. Using Constants with `readonly`:
Declare constants using the `readonly` command.
readonly PI=3.14159
3. Using `#!/bin/bash -e` for Error Handling:
Make the script exit immediately on error.
#!/bin/bash -e
4. Commenting for Clarity:
Add comments to explain complex logic and enhance script readability.
# Loop through files and perform operations
for file in *.txt; do
# Process each file here
done
These advanced Linux shell scripting techniques provide a solid foundation for creating more sophisticated and efficient scripts. Experiment with these concepts to enhance your scripting skills further.
You may also like:
https://hackedyou.org/mastering-sed-in-linux-scripting/
https://hackedyou.org/mastering-awk-in-linux-scripting/
Latest blogs
Bug Bounty mistakes and how to avoid them
Speedify VPN Review: Pricing | Security | Performance
Bug Bounty ROI: Stop Breaches, Slash Costs
Tyche: Making Medical Scans Clearer for Better Diagnoses
A Detailed Guide on RustScan – Hacking Articles
Best Open Source Password Managers for Windows in 2024
Are You Subscribed?