Skip to main content

Command Palette

Search for a command to run...

Understanding Shell Scripting Variables, File Checks, and AWK gsub

Published
โ€ข3 min readโ€ขView as Markdown

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:

FlagDescription
-eFile or directory exists
-fExists and is a regular file
-dExists and is a directory
-sExists and is not empty
-rIs readable
-wIs writable
-xIs executable
-gtGreater than (numeric)
-ltLess than (numeric)
-neNot 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.