通过 npm 运行 将参数传递给 bash 脚本
pass parameters to bash script through npm run
我是编写 bash 脚本的新手,我有一个我创建的 npm 包
https://www.npmjs.com/package/comgen
Bash
days=3
hours=24
minutes=60
totalNumberOfCommits=3
lenghtOfTime=$((days*hours*minutes))
arrayOfCommits=$(shuf -i 1-$lenghtOfTime -n $totalNumberOfCommits | sort -r -n)
for index in $arrayOfCommits
do
randomMessage=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
git commit --allow-empty --date "$(date -d "-$index minutes")" -m "$randomMessage"
done
git push origin master
你 运行 它像这样 npm 运行 comgen 我想要它 运行 像这样:
npm run comgen -days "x number of days" -totalNumberOfCommits "x number of commits"
Package.json
"scripts": {
"comgen": "commit-generator.sh"
},
天数和通过的提交总数将替换我在 bash 脚本中的变量中的数字。
我在发帖之前阅读了这个 SO 问题:https://unix.stackexchange.com/questions/32290/pass-command-line-arguments-to-bash-script
我不确定 npm
究竟是如何工作的,但根据我的经验,我猜命令行参数会直接传递给调用的可执行文件(二进制或脚本)。这意味着您的脚本实际上被称为
/path/to/comgen -days "x number of days" -totalNumberOfCommits "x number of commits"
现在纯粹是 Bash 来解析 cmdline 参数。您评估一个选项并决定下一个值是什么:
days=3
hours=24
minutes=60
totalNumberOfCommits=3
while [ $# -ne 0 ]; do
case "" in
"-d"|"-days") days=; shift 2;;
"-tc"|"-totalNumberOfCommits") totalNumberOfCommits=; shift 2;;
# "-x"|"-xx"|"-xxx") : Process another option; shift 2;;
*) break;;
esac
done
lenghtOfTime=$((days*hours*minutes))
... rest of the code
试试这个
package.json
"scripts": {
"comgen": "commit-generator.sh"
},
提交-generator.sh
#!/bin/bash
echo $npm_config_days
echo $npm_config_hours
终于在航站楼
npm run comgen --days=foo --hours=bar
输出:
> commit-generator.sh
foo
bar
我是编写 bash 脚本的新手,我有一个我创建的 npm 包 https://www.npmjs.com/package/comgen
Bash
days=3
hours=24
minutes=60
totalNumberOfCommits=3
lenghtOfTime=$((days*hours*minutes))
arrayOfCommits=$(shuf -i 1-$lenghtOfTime -n $totalNumberOfCommits | sort -r -n)
for index in $arrayOfCommits
do
randomMessage=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
git commit --allow-empty --date "$(date -d "-$index minutes")" -m "$randomMessage"
done
git push origin master
你 运行 它像这样 npm 运行 comgen 我想要它 运行 像这样:
npm run comgen -days "x number of days" -totalNumberOfCommits "x number of commits"
Package.json
"scripts": {
"comgen": "commit-generator.sh"
},
天数和通过的提交总数将替换我在 bash 脚本中的变量中的数字。
我在发帖之前阅读了这个 SO 问题:https://unix.stackexchange.com/questions/32290/pass-command-line-arguments-to-bash-script
我不确定 npm
究竟是如何工作的,但根据我的经验,我猜命令行参数会直接传递给调用的可执行文件(二进制或脚本)。这意味着您的脚本实际上被称为
/path/to/comgen -days "x number of days" -totalNumberOfCommits "x number of commits"
现在纯粹是 Bash 来解析 cmdline 参数。您评估一个选项并决定下一个值是什么:
days=3
hours=24
minutes=60
totalNumberOfCommits=3
while [ $# -ne 0 ]; do
case "" in
"-d"|"-days") days=; shift 2;;
"-tc"|"-totalNumberOfCommits") totalNumberOfCommits=; shift 2;;
# "-x"|"-xx"|"-xxx") : Process another option; shift 2;;
*) break;;
esac
done
lenghtOfTime=$((days*hours*minutes))
... rest of the code
试试这个
package.json
"scripts": {
"comgen": "commit-generator.sh"
},
提交-generator.sh
#!/bin/bash
echo $npm_config_days
echo $npm_config_hours
终于在航站楼
npm run comgen --days=foo --hours=bar
输出:
> commit-generator.sh
foo
bar