我如何定义一个bash别名作为多个命令序列? [英] How can I define a bash alias as a sequence of multiple commands?

查看:124
本文介绍了我如何定义一个bash别名作为多个命令序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何配置在bash别名,但有配置一个别名命令序列的方式?

I know how to configure aliases in bash, but is there a way to configure an alias for a sequence of commands?

即说我想要一个命令切换到一个特定的目录,然后运行另一个命令。

I.e say I want one command to change to a particular directory, then run another command.

另外,有没有办法设置运行命令mycommand,然后输入密码的命令?在MS-DOS几天我会找一个.bat文件,但我不能确定的linux(或在这种情况下,Mac OSX版)等价的。

In addition, is there a way to setup a command that runs "sudo mycommand", then enters the password? In the MS-DOS days I'd be looking for a .bat file but I'm unsure of the linux (or in this case Mac OSX) equivalent.

推荐答案

您别说BAT文件所以也许你想要的是写一个shell脚本。如果是这样那么只需输入命令你想行由行成像这样的文件:

You mention BAT files so perhaps what you want is to write a shell script. If so then just enter the commands you want line-by-line into a file like so:

command1
command2

和要求的bash执行的文件:

and ask bash to execute the file:

bash myscript.sh

如果您希望能够直接调用脚本而无需键入庆典,然后添加以下行的的第一行的文件:

If you want to be able to invoke the script directly without typing "bash" then add the following line as the first line of the file:

#! /bin/bash
command1
command2

然后将文件标记为可执行文件:

Then mark the file as executable:

chmod 755 myscript.sh

现在你可以像任何其他可执行文件运行它:

Now you can run it just like any other executable:

./myscript.sh

需要注意的是UNIX并不真正关心的文件扩展名。如果你喜欢,你可以简单地将文件命名为的MyScript无.SH扩展名。那就是,重要的是特别第一行。例如,如果要编写脚本的Perl编程语言,而不是bash的第一行是:

Note that unix doesn't really care about file extensions. You can simply name the file "myscript" without the ".sh" extension if you like. It's that special first line that is important. For example, if you want to write your script in the Perl programming language instead of bash the first line would be:

#! /usr/bin/perl

这是第一行告诉你的外壳间有什么preTER调用来执行你的脚本。

That first line tells your shell what interpreter to invoke to execute your script.

另外,如果你现在你的脚本复制到在$ PATH环境变量,然后列出的目录中,你可以从任何地方只需输入其文件名调用它:

Also, if you now copy your script into one of the directories listed in the $PATH environment variable then you can call it from anywhere by simply typing its file name:

myscript.sh

即使制表完成的作品。这就是为什么我通常包括在我的$ PATH一个〜/ bin目录,这样我可以轻松地安装自己编写的脚本。最重要的是,一旦你有一堆自己编写的脚本,你是习惯了,你可以轻松地将它们移植到任何新的UNIX计算机复制您的个人〜/ bin目录。

Even tab-completion works. Which is why I usually include a ~/bin directory in my $PATH so that I can easily install personal scripts. And best of all, once you have a bunch of personal scripts that you are used to having you can easily port them to any new unix machine by copying your personal ~/bin directory.

这篇关于我如何定义一个bash别名作为多个命令序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆