使用变量创建 Mac 终端脚本
Create Mac terminal script with variable
我想创建一个脚本来执行多行代码,但也想问用户一个问题以用作变量。
例如,这是我在终端中执行的:
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.1
和 NAME
作为变量,我向用户询问启动脚本的问题:
What is the name of this pod?
What version?
然后我想将这些变量合并到上面的脚本中。我对使用什么 "dialect"(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
给这个脚本一个合适的名字(script 或 script.sh 等等),然后分配适当的权限:
chmod +x path/to/the/script
然后 运行 从终端:
path/to/the/script
您也可以让脚本将名称和版本作为参数。结合上述方法的方法是:
#!/bin/bash
name="";ver=""
[[ $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
并且它不会要求 name
和 ver
,而是将 podname
作为名称,并根据传递的参数将 ver
作为版本。
如果第二个参数不传,会要求ver.
如果传递了 none 个参数,它将要求两个参数,就像第一个代码示例一样。
我想创建一个脚本来执行多行代码,但也想问用户一个问题以用作变量。
例如,这是我在终端中执行的:
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.1
和 NAME
作为变量,我向用户询问启动脚本的问题:
What is the name of this pod?
What version?
然后我想将这些变量合并到上面的脚本中。我对使用什么 "dialect"(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
给这个脚本一个合适的名字(script 或 script.sh 等等),然后分配适当的权限:
chmod +x path/to/the/script
然后 运行 从终端:
path/to/the/script
您也可以让脚本将名称和版本作为参数。结合上述方法的方法是:
#!/bin/bash
name="";ver=""
[[ $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
并且它不会要求 name
和 ver
,而是将 podname
作为名称,并根据传递的参数将 ver
作为版本。
如果第二个参数不传,会要求ver.
如果传递了 none 个参数,它将要求两个参数,就像第一个代码示例一样。