通过npm run将参数传递给bash脚本

问题描述 投票:0回答:1

我是新手写的bash脚本,我有一个npm包,我创建了qazxsw poi

巴什

https://www.npmjs.com/package/comgen

你像这样运行npm run comgen我希望它像这样运行:

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

的package.json

npm run comgen -days "x number of days" -totalNumberOfCommits "x number of commits"

如果天数和传递的总数将替换bash脚本中变量的数量。

我在发布自己的问题之前阅读了这些问题:"scripts": { "comgen": "commit-generator.sh" },

bash shell npm npm-scripts
1个回答
1
投票

我不确定https://unix.stackexchange.com/questions/32290/pass-command-line-arguments-to-bash-script究竟是如何工作的,但根据我的经验,我猜测命令行参数会直接传递给被调用的可执行文件(二进制文件或脚本)。这意味着您的脚本实际上被称为

npm

现在它是解析cmdline参数的纯Bash东西。您评估一个选项并确定下一个值是什么:

/path/to/comgen -days "x number of days" -totalNumberOfCommits "x number of commits"
© www.soinside.com 2019 - 2024. All rights reserved.