TruMan1
TruMan1

Reputation: 36158

Create Mac terminal script with variable

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?

Upvotes: 2

Views: 1934

Answers (1)

Jahid
Jahid

Reputation: 22438

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.

Upvotes: 2

Related Questions