如何在命令行中使用单个命令运行一系列命令?
我通常运行以下命令来部署特定应用程序:
compass compile -e production --force
git add .
git commit -m "Some message"
git push
git push production master
如何将其包装到单个命令中?
我需要能够自定义提交消息。因此该命令可能类似于:
deploy -m "Some message"
I typically run the following commands to deploy a particular app:
compass compile -e production --force
git add .
git commit -m "Some message"
git push
git push production master
How can I wrap that up into a single command?
I'd need to be able to customize the commit message. So the command might look something like:
deploy -m "Some message"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有两种可能性:
脚本,正如其他人回答的那样
函数,在您的 .bash_profile 中定义:
如果没有参数,您将有第三个选项,即别名:
There are two possibilities:
a script, as others answered
a function, defined in your .bash_profile:
Without arguments, you'd have a third option, namely an alias:
您可以创建一个函数来执行您想要的操作,并将提交消息作为参数传递:
将其放入您的
.bashrc
中,然后就可以开始了。You could create a function that does what you want, and pass the commit message as argument:
Put that in your
.bashrc
and you're good to go.你可以制作一个shell脚本。看起来像这样的东西(注意没有输入验证等):
将其保存到 myscript.sh,
chmod +x
它,然后执行类似./myscript 的操作。 sh“一些消息”
。You can make a shell script. Something that looks like this (note no input validation etc):
Save that to
myscript.sh
,chmod +x
it, then do something like./myscript.sh "Some message"
.您可以为此编写一个 shell 脚本,
将其保存到“部署”并对其执行 chmod 7xx。现在您可以将其用作 ./deploy“Some message”
You can write a shell script for this
Save this to 'deploy' and do a chmod 7xx on it. Now you can use it as ./deploy "Some message"
您可以将这些命令写入名为deploy.sh的文件中。
然后使其可执行并作为 sh deploy.sh 运行
您甚至可以通过导出保存脚本的路径将其添加到您的路径中。
you could write these commands into a file named deploy.sh .
Then make it executable and run as sh deploy.sh
You could even add it to your path by exporting the path where you save the script.
每个人都提到编写脚本,这可能是最好的方法。
然而,有一天您可能想使用另一种方式 - 将命令与 && 合并,例如:
将在父目录中创建一个文件“abc”:)
这只是为了让您了解这样的事情,对于这个特定的场景(和 99% 的其他人)请看看其他答案:)
everyone mentions about writing a script and this is probably the best way of doing it.
However you might someday want to use another way - merge commands with &&, for example:
will create a file "abc" in a parent directory :)
It is just to let you know about such thing, for this particular scenario (and 99% of the others) please take a look at other answers :)
我会努力使该命令不仅仅适用于当前目录。最通用的方法之一是在 BASH 脚本中使用 getopt。确保您已安装 getopt,创建
deploy.sh
然后chmod 755 deploy.sh
然后执行如下操作:I would go through the effort of making the command work for more than just the current directory. One of the most versitle ways of doing this is to use getopt in a BASH script. Make sure you have getopt installed, create
deploy.sh
thenchmod 755 deploy.sh
and then do something like this: