0
点赞
收藏
分享

微信扫一扫

写了一个简单易用的 shell 框架(上篇)

四月Ren间 2022-05-06 阅读 41
linuxbash

Easy Bash

Easy Bash - a simple bash library to make scripting easier.
在这里插入图片描述

How to use it

Here is a step by step example to illustrate how easybash makes scripting easier.

Create your script

You can simply clone examples/template.sh.

git clone git@github.com:DBADaily/easybash.git
cd easybash
mkdir echo_dates
cp -a examples/template.sh echo_dates/echo_dates.sh
cd echo_dates/
vi echo_dates.sh

Step 1 - add your options as below

add_main_options() {
  ## custom options
  # add your options here
  add_options "s:" "start-date:" "START_DATE" "Y" "start date"
  add_options "e:" "end-date:" "END_DATE" "Y" "end date"
  ...
}

Step 2 - write your custom function

# custom function
echo_dates() {
  local lv_start_date="$1"
  local lv_end_date="$2"
  while [[ "${lv_start_date}" -le "${lv_end_date}" ]]; do
    echo_blue_bold "Processing for date ${lv_start_date}"
    log_trace "Work for date ${lv_start_date} done."
    lv_start_date=$(date -d "${lv_start_date} + 1 days" +'%Y%m%d')
  done
}

Step 3 - implement main function

main() {
  add_main_options
  parse_args "$@"

  # add your logic here
  if [[ "$CHECK_MODE" != "Y" ]]; then
    echo_dates "${START_DATE}" "${END_DATE}"
  elif [[ "$CHECK_MODE" == "Y" ]]; then
    log_warning "CHECK MODE. Skip echoing dates."
  fi
}

Here are the complete codes:

#!/usr/bin/env bash
################################################################################
#
# Author: Alvin
# License: MIT
# GitHub: https://github.com/dbadaily/easybash
#
# echo dates
################################################################################

VERSION_STR="1.0.0"

current_dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"

lib_dir="$(dirname "${current_dir}")"

source "${lib_dir}/lib/easybash.sh"

add_main_options() {
  ## custom options
  # add your options here
  add_options "s:" "start-date:" "START_DATE" "Y" "start date"
  add_options "e:" "end-date:" "END_DATE" "Y" "end date"

  ## common options
  # 1. options like m: or emails: can be changed
  # 2. variable names like RECIPIENTS are NOT expected to be changed as they are used in libraries
  add_options "m:" "email:" "RECIPIENTS" "N" "emails(separated by space) to receive notifications"
  add_options "S" "success-notification" "SUCCESS_NOTIFICATION_IND" "N" "indication whether to send success notifications"
  add_options "C" "check" "CHECK_MODE" "N" "don't make any changes"
  add_options "G" "generate-config" "GEN_CONFIG" "N" "generate config file if not exists"
  add_options "w" "write-values" "WRITE_VALUES" "N" "used together with -G, write values provided by command options to config file"
  add_options "H" "help" "HELP" "N" "show this help"
  add_options "V" "version" "VERSION" "N" "output version information"
  add_options "v" "verbose" "VERBOSE" "N" "verbose mode"
}

# custom function
echo_dates() {
  local lv_start_date="$1"
  local lv_end_date="$2"
  while [[ "${lv_start_date}" -le "${lv_end_date}" ]]; do
    echo_blue_bold "Processing for date ${lv_start_date}"
    log_trace "Work for date ${lv_start_date} done."
    lv_start_date=$(date -d "${lv_start_date} + 1 days" +'%Y%m%d')
  done
}

main() {
  add_main_options
  parse_args "$@"

  # add your logic here
  if [[ "$CHECK_MODE" != "Y" ]]; then
    echo_dates "${START_DATE}" "${END_DATE}"
  elif [[ "$CHECK_MODE" == "Y" ]]; then
    log_warning "CHECK MODE. Skip echoing dates."
  fi
}

main "$@"

Run your script

Show help message

Show help message to get information about options.

bash echo_dates.sh -H

在这里插入图片描述

Run with parameters

bash echo_dates.sh -s 20220101 -e 20220107

Result:
在这里插入图片描述

Run in verbose mode

Run in verbose mode to debug or get more detailed information, e.g. LOG_FILE.

bash echo_dates.sh -s 20220101 -e 20220107 -v

在这里插入图片描述

Run in check mode

Run in check mode so that you can double check before you run it actually.

bash echo_dates.sh -s 20220101 -e 20220107 -v -C

在这里插入图片描述

Run without all required parameters

Better run in check mode so that it makes no changes.

Run with no parameters, it will:

  1. give warning hints about all required options
  2. give a fatal message
  3. exit after the fatal message
bash echo_dates.sh -v -C

在这里插入图片描述

Run with one parameter.

bash echo_dates.sh -s 20220101 -v -C

在这里插入图片描述

Generate config file

You can also store your parameters in a config file.

Write the config file yourself ?

Just use -G option to generate a config file for you.

bash echo_dates.sh -s 20220101 -e 20220107 -v -G

在这里插入图片描述

Check the contents of the config file.

cat config.sh
# start date
START_DATE=""

# end date
END_DATE=""

...

During development, you might add more options.

It will save the old config file once you generate again.
在这里插入图片描述

Generate config file with values

Generating a config file with empty values is not enough, you need to set the values automatically.

Combine options -G and -w together, it will generate config file with parameters assigned.

bash echo_dates.sh -s 20220101 -e 20220107 -v -G -w

在这里插入图片描述

cat config.sh
# start date
START_DATE="20220101"

# end date
END_DATE="20220107"

...

Run with config file

After having generated config file with variables assigned, you can run the script without parameters.

bash echo_dates.sh -v

在这里插入图片描述

Overwrite config file variables

Variables in config file will be overwritten by options.

bash echo_dates.sh -v -e 20220103

在这里插入图片描述

Send messages

Add send_msg as below to send notification so that you are informed timely.

# custom function
echo_dates() {
  local lv_start_date="$1"
  local lv_end_date="$2"
  local lv_msg="Work between ${lv_start_date} and ${lv_end_date} is done."
  ...
  send_msg "${lv_msg}"
}

Email can be specified by -m .

bash echo_dates.sh -s 20220101 -e 20220107 -m "alvin@dbadaily.com"

原文链接:
https://blog.csdn.net/DBADaily/article/details/124600775
您浏览的网址与此链接不一致的话,则为未授权的转载,为了更好的阅读体验,建议阅读原文。

举报

相关推荐

0 条评论