1

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

asked Sep 3, 2018 at 5:27

2 Answers 2

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
answered Sep 3, 2018 at 5:36
Sign up to request clarification or add additional context in comments.

3 Comments

All I have to do know is now go to pass variables to the npm run comgen -d 5 -tc 10
I got to the point where I can run this 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ドル " }
i figured it out. My problem is that i need to pass the parameters with out the "-" and change the name in the bash script to be just d,tc,etc
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
answered Jul 2, 2021 at 1:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.