Reputation: 5522
I'm new to writting bash script and I have a npm package that I created 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
You run it like this npm run comgen I want it to run like this:
npm run comgen -days "x number of days" -totalNumberOfCommits "x number of commits"
Package.json
"scripts": {
"comgen": "commit-generator.sh"
},
Where the number of days and the total of commits passed will replace the number in the variables I have in the bash script.
I read this SO questions before postins my own: https://unix.stackexchange.com/questions/32290/pass-command-line-arguments-to-bash-script
Upvotes: 1
Views: 3266
Reputation: 21
Try this
package.json
"scripts": {
"comgen": "commit-generator.sh"
},
commit-generator.sh
#!/bin/bash
echo $npm_config_days
echo $npm_config_hours
and finally in terminal
npm run comgen --days=foo --hours=bar
output:
> commit-generator.sh
foo
bar
Upvotes: 2
Reputation: 37227
I'm not sure how exactly npm
works, but from my experience I guess that command-line arguments are passed directly to the called executable (binary or script). This means your script is actually called like
/path/to/comgen -days "x number of days" -totalNumberOfCommits "x number of commits"
Now it's pure Bash stuff to parse the cmdline arguments. You evaluate an option and decide what the next value is:
days=3
hours=24
minutes=60
totalNumberOfCommits=3
while [ $# -ne 0 ]; do
case "$1" in
"-d"|"-days") days=$2; shift 2;;
"-tc"|"-totalNumberOfCommits") totalNumberOfCommits=$2; shift 2;;
# "-x"|"-xx"|"-xxx") : Process another option; shift 2;;
*) break;;
esac
done
lenghtOfTime=$((days*hours*minutes))
... rest of the code
Upvotes: 2