Understanding Shell Scripting Variables, File Checks, and AWK gsub
Shell scripting is a powerful tool in a developer's toolbox, enabling automation, system management, and data manipulation directly from the command line. In this post, we cover some essential shell scripting features: special variables, file checks, and AWK's gsub function.
๐ง Special Shell Variables
Shell scripts use $ to access the value of variables. Here are some special variables every scripter should know:
$0: Name of the current script file.$n: The nth positional parameter (e.g.,$1,$2).$#: Number of arguments passed to the script or function.$@or$*: All the arguments passed to the function.$?: Exit status of the last executed command (0 for success).$$: PID (process ID) of the current shell.$!: PID of the last background process.
These variables make scripting flexible and powerful, especially when handling arguments or processes.
๐ฆ Trap Command
The trap command is used to catch signals and handle them gracefully. It helps:
Prevent script interruptions from signals like
CTRL+C.Perform clean-up actions (like deleting temp files).
Example:
trap "echo 'Interrupted!'; rm -f /tmp/tempfile; exit" INT
This traps the INT (interrupt) signal and performs custom cleanup before exiting.
๐ File and Directory Checks
Use conditional expressions to test files and directories in shell scripts:
if [ -e "$file" ]; then
echo "$file exists"
fi
Common Checks:
| Flag | Description |
-e | File or directory exists |
-f | Exists and is a regular file |
-d | Exists and is a directory |
-s | Exists and is not empty |
-r | Is readable |
-w | Is writable |
-x | Is executable |
-gt | Greater than (numeric) |
-lt | Less than (numeric) |
-ne | Not equal (numeric) |
๐พ Disk Usage with df
Check file system disk space using df:
df -h: Show disk space of all mounted filesystems in human-readable format.df -h /: Show usage for root (/) filesystem only.df -h --total: Display total disk usage across all filesystems.
๐งน Data Cleaning with AWK gsub
The gsub function in awk is used to perform global substitution.
Use Case: Removing Commas from Numbers
Input CSV (salaries.csv):
Name,Salary
John Doe,"1,200,000"
Jane Smith,"900,500"
Command:
awk 'NR>1 { gsub(/,/, "", $2); print $1, $2 }' salaries.csv
Output:
John Doe 1200000
Jane Smith 900500
๐ Why Use gsub?
Remove formatting like commas for numeric processing.
Clean text for further automation or reporting.
๐ Conclusion
Shell scripting offers robust tools for system interaction, file handling, and text processing. By mastering special variables, file tests, and tools like awk, you gain the power to automate and manage systems efficiently.