xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Linux shell script auto generate batch files All In One

Linux shell script auto generate batch files All In One

Linux shell script 自动批量生成文件

原理分析

  1. 定义成全局 cli command
  2. 接收参数
  3. 使用循环,动态批量生成文件
  4. 读取 template
  5. npm 发布

demos

error ❌

#!/usr/bin/env bash

rm -rf ./test
mkdir test
cd ./test

# TS_TEMPLATE=<<EOF"
TS_TEMPLATE='
"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2023-03-23
 * @modified
 *
 * @description
 * @description
 * @difficulty Easy
 * @ime_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://www.freecodecamp.org/chinese/learn/javascript-algorithms-and-data-structures/regular-expressions/
 * @link https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/
 * @solutions
 *
 * @best_solutions
 *
 */

export { };

const log = console.log;

'
# EOF

echo $TS_TEMPLATE

i=1
n=3

# $0
# while(( $i <= $0 ))
while(( $i <= $n ))
do
  echo $i
  name=""
  if (( $i < 10 ))
  then
    name="0$i.ts"
  elif (( $i >= 10 ))
  then
    name="$i.ts"
  else
    echo "❌"
  fi
  # touch $name
  echo $TS_TEMPLATE > $name
  let "i++"
done

# arr=1..10
# arr=(1 2 3 4 5 6 7 8 9 10)

# for index in $arr
# do
#   echo "index = $index"
#   echo index = $index
# done

for loop in 1 2 3 4 5
do
  echo "The value is: $loop"
done

# arr=(1 2 3)

# for index in $arr
# do
#   echo "index = $index"
#   echo index = ${arr[$index]}
# done

# arr=(1 2 3)

# for index in ${arr}
# do
#   echo "index = $index"
#   echo index = ${arr[$index]}
# done

echo -e "\n"
# i = 1
# n = 3
# ./for-loop.sh: line 34: i: command not found
# ./for-loop.sh: line 35: n: command not found

i=1
n=3

while(( $i <= $n ))
# while(( $i<=$n ))
# while(($i<=$n))
do
  echo $i
  let "i++"
done


solution ✅

#!/usr/bin/env bash

# output errors ???
# 显示所有的已经执行的命令
# set -eux

echo "🚀 auto create templates in current folder..."

# clear
rm -rf ./test
mkdir test
cd ./test

fallback="es2023"

filename=""

if (($2))
then
  filename=$2
else
  filename=$fallback
fi

# ✅ single quotes string
TS_TEMPLATE='
"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2023-03-23
 * @modified
 *
 * @description
 * @description
 * @difficulty Easy
 * @ime_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://www.freecodecamp.org/chinese/learn/javascript-algorithms-and-data-structures/'$2'/
 * @link https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/'$2'/
 * @link https://www.freecodecamp.org/chinese/learn/javascript-algorithms-and-data-structures/'$filename'/
 * @link https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/'$filename'/
 * @solutions
 *
 * @best_solutions
 *
 */

export { };

const log = console.log;

'

# EOF output empty bug ❌
# TS_TEMPLATE=<<EOF
# "use strict";

# /**
#  *
#  * @author xgqfrms
#  * @license MIT
#  * @copyright xgqfrms
#  * @created 2023-03-23
#  * @modified
#  *
#  * @description
#  * @description
#  * @difficulty Easy
#  * @ime_complexity O(n)
#  * @space_complexity O(n)
#  * @augments
#  * @example
#  * @link https://www.freecodecamp.org/chinese/learn/javascript-algorithms-and-data-structures/regular-expressions/
#  * @link https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/
#  * @solutions
#  *
#  * @best_solutions
#  *
#  */

# export { };

# const log = console.log;
# EOF

# $1
# echo $1

# if (($1))
# then
#   echo $1
# else
#   echo "❌"
# fi


# tsGenerator: command not found ❌ function 必须先定义,然后再使用
# function func() {} vs func() {}
# tsGenerator() {
function tsGenerator() {
  # function scope $1, 必须在调用函数的时候传递进来
  index=1
  while(( $index <= $1 ))
  do
    echo 👻 index = $index
    name=""
    if (( $index < 10 ))
    then
      name="0$index.ts"
    elif (( $index >= 10 ))
    then
      name="$index.ts"
    else
      echo "❌"
    fi
    echo -e filename = $name
    # printf "$TS_TEMPLATE"
    printf "$TS_TEMPLATE" > $name
    let "index++"
  done
  return 0
}

# scipt scope $1 ✅
if (($1))
then
  echo "🎮 arg = $1"
  tsGenerator $1
  # tsGenerator 6
else
  echo "❌"
fi


# arg=$1

# if (($arg))
# then
#   echo "🎮 arg = $1"
#   # tsGenerator $arg
#   # tsGenerator $1
#   tsGenerator $1
# else
#   echo "❌"
# fi


# $1: unbound variable
# if (($1))
# then
#   echo $1
#   # arg=$1
#   # tsGenerator $arg
#   # tsGenerator $1
#   # tsGenerator
# else
#   echo "❌"
# fi

# 1. 接收参数 ✅
# 2. 读取模版
# 3. NPM publish

# 一个参数
# $ ./auto-ts-files-generator.sh 3
# $ ./auto-ts-files-generator.sh 7

# 两个参数
# ./auto-ts-files-generator.sh 3 es6
# ./auto-ts-files-generator.sh 7 typescript


image

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

EOF

markdown

#!/usr/bin/env bash

SH_DATE=$(TZ=':Asia/Shanghai' date '+%Y-%m-%d %T');

MARKDOWN='
{
    "msg_type": "post",
    "content": {
        "post": {
            "zh_cn": {
                "title": "周报通知",
                "content": [
                    [
                        {
                            "tag": "a",
                            "text": "2022 Q1 周报地址\n",
                            "href": "https://xgqfrms.feishu.com/sheets/xgqfrms?sheet=O8Nxwm"
                        },
                        {
                            "tag": "text",
                            "text": "日期: '$SH_DATE'\n"
                        },
                        {
                            "tag": "at",
                            "user_id": "all",
                            "user_name": "所有人"
                        }
                    ]
                ]
            }
        }
    }
}'

https://github.com/xgqfrms/fs-robot-notice/blob/main/src/weekly-notice.sh#L34

https://github.com/xgqfrms/linux/blob/master/daily-notes/2021/auto-read-write-template.sh

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-03-24 14:45  xgqfrms  阅读(17)  评论(3编辑  收藏  举报