Linux 如何将 bash 别名定义为多个命令的序列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2135644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I define a bash alias as a sequence of multiple commands?
提问by stormist
I know how to configure aliases in bash, but is there a way to configure an alias for a sequence of commands?
我知道如何在 bash 中配置别名,但是有没有办法为一系列命令配置别名?
I.e say I want one command to change to a particular directory, then run another command.
即说我想要一个命令更改到特定目录,然后运行另一个命令。
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.
另外,有没有办法设置一个运行“sudo mycommand”的命令,然后输入密码?在 MS-DOS 时代,我会寻找一个 .bat 文件,但我不确定 linux(或在这种情况下是 Mac OSX)的等价物。
采纳答案by slebetman
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:
你提到了 BAT 文件,所以也许你想要的是编写一个 shell 脚本。如果是这样,那么只需将您想要的命令逐行输入到文件中,如下所示:
command1
command2
and ask bash to execute the file:
并要求 bash 执行文件:
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 lineof the file:
如果您希望能够在不键入“bash”的情况下直接调用脚本,请添加以下行作为文件的第一行:
#! /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
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:
请注意,unix 并不真正关心文件扩展名。如果您愿意,您可以简单地将文件命名为“myscript”而不带“.sh”扩展名。重要的是特殊的第一行。例如,如果您想用 Perl 编程语言而不是 bash 编写脚本,第一行将是:
#! /usr/bin/perl
That first line tells your shell what interpreter to invoke to execute your script.
第一行告诉你的 shell 调用什么解释器来执行你的脚本。
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:
此外,如果您现在将脚本复制到 $PATH 环境变量中列出的目录之一,那么您只需键入其文件名即可从任何地方调用它:
myscript.sh
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.
甚至制表符完成也有效。这就是为什么我通常在 $PATH 中包含一个 ~/bin 目录,以便我可以轻松安装个人脚本。最重要的是,一旦您拥有一堆习惯使用的个人脚本,您就可以通过复制您的个人 ~/bin 目录轻松地将它们移植到任何新的 unix 机器上。
回答by Larry Shatzer
For chaining a sequence of commands, try this:
要链接一系列命令,请尝试以下操作:
alias x='command1;command2;command3;'
Or you can do this:
或者你可以这样做:
alias x='command1 && command2 && command3'
The && makes it only execute subsequent commands if the previous returns successful.
&& 使它仅在前一个返回成功时才执行后续命令。
Also for entering passwords interactively, or interfacing with other programs like that, check out expect. (http://expect.nist.gov/)
此外,对于交互式输入密码,或与其他类似程序交互,请查看 expect。( http://expect.nist.gov/)
回答by gregseth
For the alias you can use this:
对于别名,您可以使用:
alias sequence='command1 -args; command2 -args;'
or if the second command must be executed only if the first one succeeds use:
或者如果只有在第一个命令成功时才必须执行第二个命令,请使用:
alias sequence='command1 -args && command2 -args'
回答by Noufal Ibrahim
Apropos multiple commands in a single alias, you can use one of the logical operators to combine them. Here's one to switch to a directory and do an ls on it
Apropos 多个命令在一个别名中,您可以使用其中一个逻辑运算符来组合它们。这是一个切换到目录并对其执行 ls 的方法
alias x="cd /tmp && ls -al"
Another option is to use a shell function. These are sh/zsh/bash commands. I don't know enough of other shells to be sure if they work.
另一种选择是使用 shell 函数。这些是 sh/zsh/bash 命令。我对其他外壳的了解不够,无法确定它们是否有效。
As for the sudo thing, if you want that (although I don't think it's a good idea), the right way to go is to alter the /etc/sudoers
file to get what you want.
至于 sudo 的事情,如果你想要(虽然我认为这不是一个好主意),正确的方法是改变/etc/sudoers
文件以获得你想要的。
回答by wich
it's probably easier to define functions for these types of things than aliases, keeps things more readable if you want to do more than a command or two:
为这些类型的事物定义函数可能比别名更容易,如果您想做的不仅仅是一两个命令,则可以使事情更具可读性:
In your .bashrc
在您的 .bashrc 中
perform_my_command() {
pushd /some_dir
my_command "$@"
popd
}
Then on the command line you can simply do:
然后在命令行上,您可以简单地执行以下操作:
perform_my_command my_parameter my_other_parameter "my quoted parameter"
You could do anything you like in a function, call other functions, etc.
你可以在一个函数中做任何你喜欢的事情,调用其他函数等等。
You may want to have a look at the Advanced Bash Scripting Guidefor in depth knowledge.
您可能需要查看高级 Bash 脚本指南以获得更深入的知识。
回答by Diego Torres Milano
Your best bet is probably a shell function instead of an alias if the logic becomes more complex or if you need to add parameters (though bash supports aliases parameters).
如果逻辑变得更复杂或者您需要添加参数(尽管 bash 支持别名参数),您最好的选择可能是 shell 函数而不是别名。
This function can be defined in your .profile or .bashrc. The subshell is to avoid changing your working directory.
这个函数可以在你的 .profile 或 .bashrc 中定义。子shell 是为了避免更改您的工作目录。
function myfunc {
( cd /tmp; command )
}
then from your command prompt
然后从您的命令提示符
$ myfunc
For your second question you can just add your command to /etc/sudoers (if you are completely sure of what you are doing)
对于您的第二个问题,您可以将命令添加到 /etc/sudoers(如果您完全确定自己在做什么)
myuser ALL = NOPASSWD: \
/bin/mycommand
回答by B.Kocis
回答by eold
You can embed the function declaration followed by the function in the alias itself, like so:
您可以将函数声明后跟函数嵌入别名本身,如下所示:
alias my_alias='f() { do_stuff_with "$@" (arguments)" ...; }; f'
The benefit of this approach over just declaring the function by itself is that you can have a peace of mind that your function is not going to be overriden by some other script you're sourcing (or using .
), which might use its own helper under the same name.
这种方法比只声明函数本身的好处是,您可以放心,您的函数不会被您正在采购(或使用.
)的其他脚本覆盖,这些脚本可能会在下面使用自己的帮助程序同名。
E.g., Suppose you have a script init-my-workspace.sh
that you're calling like . init-my-workspace.sh
or source init-my-workspace.sh
whose purpose is to set or export a bunch of environment variables (e.g., JAVA_HOME, PYTHON_PATH etc.). If you happen to have a function my_alias
inside there, as well, then you're out of luck as the latestfunction declaration withing the same shell instance wins.
例如,假设您有一个init-my-workspace.sh
您正在调用的脚本,. init-my-workspace.sh
或者source init-my-workspace.sh
其目的是设置或导出一堆环境变量(例如,JAVA_HOME、PYTHON_PATH 等)。如果你碰巧my_alias
里面也有一个函数,那么你就不走运了,因为具有相同 shell 实例的最新函数声明获胜。
Conversely, aliases have separate namespace and even in case of name clash, they are looked up first. Therefore, for customization relevant to interactive usage, you should only ever use aliases.
相反,别名具有单独的命名空间,即使在名称冲突的情况下,也会首先查找它们。因此,对于与交互使用相关的自定义,您应该只使用别名。
Finally, note that the practice of putting all the aliases in the same place (e.g., ~/.bash_aliases
) enables you to easily spot any name clashes.
最后,请注意将所有别名放在同一位置(例如,~/.bash_aliases
)的做法使您可以轻松发现任何名称冲突。