简体   繁体   English

使用变量创建Mac终端脚本

[英]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: 我想将0.0.1NAME为变量,然后问用户问题以启动脚本:

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. 我对要使用的“方言”(sh,bash,csh,JavaScript?等)感到困惑,并且应该将该扩展名另存为,因此只需双击它。

How can I do this? 我怎样才能做到这一点?

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: 给该脚本起一个合适的名称( scriptscript.sh等。),然后分配适当的权限:

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. 并且它不会询问namever ,而是从传递的参数中以podname作为name和ver作为版本。

If the second argument is not passed, it will ask for ver. 如果未传递第二个参数,它将要求输入ver。

If none of the arguments is passed, it will ask for both of them, just like the first code sample. 如果没有传递任何参数,则将像第一个代码示例一样要求两个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM