macos - 使用变量创建 Mac 终端脚本

标签 macos bash shell terminal sh

我想创建一个执行多行代码的脚本,同时也向用户询问一个问题以用作变量。

例如,这是我在终端中执行的:

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

我想将 0.0.1NAME 作为变量,我向用户询问问题以启动脚本:

What is the name of this pod?
What version?

然后我想将这些变量合并到上面的脚本中。我对使用什么“方言”(sh、bash、csh、JavaScript?等)以及应该将其保存为该扩展名感到困惑,所以我只需双击它即可。

我该怎么做?

最佳答案

这应该做:

#!/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

给这个脚本一个合适的名称(scriptscript.sh 等),然后分配适当的权限:

chmod +x path/to/the/script

然后从终端运行它:

path/to/the/script


您也可以使脚本将名称和版本作为参数。结合上述方法的方法是:

#!/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
...

这具有在像第一个那样工作的同时接受参数的优点。您现在可以使用参数调用脚本:

path/to/the/script podname ver

并且它不会询问namever,而是将podname作为名称和ver 作为传递参数的版本。

如果第二个参数未传递,则会询问 ver。

如果没有传递任何参数,它将要求提供这两个参数,就像第一个代码示例一样。

关于macos - 使用变量创建 Mac 终端脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31385822/

相关文章:

linux - 如何使用 Ubuntu 命令从文本文件中提取域?

linux - 使用 tcsh 的彩色联机帮助页?

html - 如何从 linux 中的多个文件中删除特定行?

ios - 如何对核心数据进行按天分组?

swift - 在枚举期间获取完整文件路径

bash - 如何检查 PATH 是否已包含特定目录

c - 保持 ssh 上的管道链打开

linux - 在所有服务器上自动化 mkdir、chmod 和 scp

macos - NSS slider 动画

macos - 从 rc1-final 开始,ASP.NET 5 是否支持 Azure SQL?