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
2 Answers 2
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
3 Comments
commit-generator.sh -d 4 -tc 10 in the terminal. Now, when I want to do npm run comgen -d 5 -tc 10 i get commit-generator.sh -d 1ドル -tc 2ドル "1" "10" in my package.json I have "scripts": { "comgen": "commit-generator.sh -d 1ドル -tc 2ドル " }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