Is production: true

Shell Scripting

Shell scripting refers to writing scripts for command-line interpreters (shells) such as Bash, Zsh, or Sh. Shell scripts automate tasks, manage system operations, and streamline workflows in Unix-like environments.

Shell scripting is valued for:

  • Automation: Enables repetitive tasks to be automated, saving time and reducing errors.
  • Portability: Scripts can often run on any Unix-like system with minimal changes.
  • Integration: Easily integrates with other command-line tools and utilities.
  • Text Processing: Powerful for manipulating and processing text files using tools like awk, sed, and grep.
  • System Administration: Widely used for managing users, processes, backups, and deployments.
  • Rapid Prototyping: Quick to write and modify for testing ideas or workflows.
  • Community Support: Extensive documentation and a large community provide abundant resources and examples.

Shell scripting continues to be a fundamental skill for developers, system administrators, and DevOps engineers, evolving with new shells and features.

Web Site

Tools

  • Bash
  • Zsh
  • Sh
  • Fish
  • ShellCheck
  • awk
  • sed
  • grep

Basic


Handle date format in shell

1
2
3
date +"%Y_%m_%d_%H_%M"
# -----
#   2026_03_12_04_10


Substring To slice a variable or string is to substring, we could use the cut command.

1
2
3
4
val="some value"
echo "echo $val | cut -c1-6"
# output:
# some v

Shell Script basic initializer

  • Script that change directory to the script location
  • Return to the origina directory when done (trap can do finally operation)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
SCRIPT_NAME="$(basename "$0")"
TIME_START=$(date +"%Y_%m_%d_%H_%M")
echo "Script started at: $TIME_START"
CUR_DIR="$( cd -P "$( dirname "$1" )" >/dev/null 2>&1 && pwd )"
SCRIPT_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
pushd $SCRIPT_DIR > /dev/null

cleanup() {
    TIME_END=$(date +"%Y_%m_%d_%H_%M")
    echo "Script ended at: $TIME_END" >&2
    popd > /dev/null
}
trap cleanup EXIT

# your script below
Snippet to Execute Script From Stdin
March 29, 2023 · Updated: March 29, 2023 · by Xeth · ~1 min read · English

Unlock the power of shell scripts with this comprehensive guide to using sh, bash, and source commands to execute your code. Discover how to pass parameters to your scripts, …

#shell
Linux crontab
October 12, 2019 · Updated: October 12, 2019 · by Xeth · ~1 min read · English

While using linux as application host, there are quite a lot chance that we need to schedule task to be executed on specific time, such as daily backup task and frequently executed …

#migrated #shell #crontab