2

I'd like to create a script that executes several lines of code, but also ask the user a question to use as a variable.

For example, this is what I execute in terminal:

git add -A && git commit -m "Release 0.0.1."
git tag '0.0.1'
git push --tags
pod trunk push NAME.podspec

I'd like to make 0.0.1 and NAME as variables that I ask the user with questions to start the script off:

What is the name of this pod?
What version?

Then I'd like to merge these variables into the script above. I'm confused about what "dialect" to use (sh, bash, csh, JavaScript?, etc) and that extension I should save it as so I just have to double-click it.

How can I do this?

2
  • 2
    Use bash and read. See this article. Commented Jul 13, 2015 at 14:18
  • 1
    Don't use read, it defeats the primary purpose of having a script: calling it programatically. It would be far better to have usage: gitpod podname version and aborting with help if there are invalid or insufficient arguments. Commented Jul 13, 2015 at 14:33

1 Answer 1

2

This should do:

#!/bin/bash
read -e -p "What is the name of this pod?" name
read -e -p "What version?" ver
git add -A && git commit -m "Release $ver."
git tag "$ver"
git push --tags
pod trunk push "$name".podspec

Give this script a suitable name (script or script.sh etc..), then assign proper permission:

chmod +x path/to/the/script

then run it from terminal:

path/to/the/script


You can make your script take the name and version as arguments too. A method to do that combining the above is:

#!/bin/bash
name="$1";ver="$2"
[[ $name == "" ]] && read -e -p "What is the name of this pod?" name
[[ $ver == "" ]] && read -e -p "What version?" ver
...

This has the advantage of taking arguments while working like the first one. You can now call the script with arguments:

path/to/the/script podname ver

and it won't ask for name and ver, instead it will take podname as name and ver as version from the arguments passed.

If the second argument is not passed, it will ask for ver.

If none of the arguments is passed, it will ask for both of them, just like the first code sample.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.