如何将 bash 别名定义为多个命令的序列?

发布于 2024-08-19 11:40:48 字数 190 浏览 3 评论 0原文

我知道如何在 bash 中配置别名,但是有没有办法为一系列命令配置别名?

也就是说,我想要一个命令更改到特定目录,然后运行另一个命令。

另外,有没有办法设置一个运行“sudo mycommand”的命令,然后输入密码?在 MS-DOS 时代,我会寻找 .bat 文件,但我不确定 linux(或在本例中是 Mac OSX)的等效文件。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

谜兔 2024-08-26 11:40:48

要链接一系列命令,请尝试以下操作:

alias x='command1;command2;command3;'

或者您可以执行以下操作:

alias x='command1 && command2 && command3'

&&如果前一个命令返回成功,则它只执行后续命令。

另外,对于交互式输入密码或与其他类似程序交互,请查看expect。 (http://expect.nist.gov/)

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/)

浪漫之都 2024-08-26 11:40:48

你提到了 BAT 文件,所以也许你想要的是编写一个 shell 脚本。如果是这样,那么只需将您想要的命令逐行输入到文件中,如下所示:

command1
command2

并要求 bash 执行该文件:

bash myscript.sh

如果您希望能够直接调用脚本而不输入“bash”,则添加以下行:文件的第一行:

#! /bin/bash
command1
command2

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

chmod 755 myscript.sh

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

./myscript.sh

请注意,unix 并不真正关心文件扩展名。如果您愿意,您可以简单地将文件命名为“myscript”,不带“.sh”扩展名。重要的是特殊的第一行。例如,如果您想用 Perl 编程语言而不是 bash 编写脚本,第一行将是:

#! /usr/bin/perl

第一行告诉您的 shell 要调用哪个解释器来执行您的脚本。

另外,如果您现在将脚本复制到 $PATH 环境变量中列出的目录之一,那么您只需键入其文件名即可从任何地方调用它:

myscript.sh

甚至制表符补全也可以。这就是为什么我通常在 $PATH 中包含 ~/bin 目录,以便我可以轻松安装个人脚本。最重要的是,一旦您拥有了一堆您习惯的个人脚本,您就可以通过复制您的个人 ~/bin 目录轻松地将它们移植到任何新的 UNIX 机器上。

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

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

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

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

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

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.

梦罢 2024-08-26 11:40:48

为这些类型的事物定义函数可能比别名更容易,如果您想做的不仅仅是一两个命令,则可以使事物更具可读性:

在 .bashrc 中

perform_my_command() {
    pushd /some_dir
    my_command "$@"
    popd
}

然后在命令行上您可以简单地执行以下操作:

perform_my_command my_parameter my_other_parameter "my quoted parameter"

您可以做任何您喜欢的事情在函数中,调用其他函数等。

您可能需要查看高级 Bash 脚本指南< /a> 以获得深入的知识。

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

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 Guide for in depth knowledge.

就此别过 2024-08-26 11:40:48

对于别名,您可以使用以下命令:

alias sequence='command1 -args; command2 -args;'

或者如果仅当第一个命令成功时才必须执行第二个命令,请使用:

alias sequence='command1 -args && command2 -args'

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'
吻安 2024-08-26 11:40:48

如果逻辑变得更加复杂或者需要添加参数(尽管 bash 支持别名参数),那么最好的选择可能是 shell 函数而不是别名。

该函数可以在您的 .profile 或 .bashrc 中定义。子 shell 是为了避免更改您的工作目录。

function myfunc {
   ( cd /tmp; command )
}

然后从命令提示符

$ myfunc

处 对于第二个问题,您可以将命令添加到 /etc/sudoers (如果您完全确定自己在做什么)

myuser           ALL = NOPASSWD: \
                    /bin/mycommand 

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).

This function can be defined in your .profile or .bashrc. The subshell is to avoid changing your working directory.

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)

myuser           ALL = NOPASSWD: \
                    /bin/mycommand 
Oo萌小芽oO 2024-08-26 11:40:48

对于单个别名中的多个命令,您可以使用逻辑运算符之一将它们组合起来。这是切换到目录并对其执行 ls 的方法。

      alias x="cd /tmp && ls -al"

另一种选择是使用 shell 函数。这些是 sh/zsh/bash 命令。我对其他 shell 的了解还不够,无法确定它们是否有效。

至于 sudo 的事情,如果你想要这样(尽管我认为这不是一个好主意),正确的方法是更改​​ /etc/sudoers 文件以获得你想要的。

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

      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.

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.

不必了 2024-08-26 11:40:48

您可以在别名本身中嵌入函数声明,后跟函数,如下所示:

alias my_alias='f() { do_stuff_with "$@" (arguments)" ...; }; f'

与仅声明函数本身相比,这种方法的好处是,您可以放心,您的函数不会被某些函数覆盖。您正在采购的其他脚本(或使用 .),它可能会使用自己的同名助手。

例如,假设您有一个像 一样调用的脚本 init-my-workspace.sh。 init-my-workspace.shsource init-my-workspace.sh 其目的是设置或导出一堆环境变量(例如,JAVA_HOME、PYTHON_PATH 等)。如果您碰巧也有一个函数 my_alias ,那么您就不走运了,因为具有相同 shell 实例的最新函数声明获胜。

相反,别名具有单独的命名空间,即使在名称冲突的情况下,也会首先查找它们。因此,对于与交互式使用相关的自定义,您应该只使用别名。

最后,请注意,将所有别名放在同一位置(例如,~/.bash_aliases)的做法使您能够轻松发现任何名称冲突。

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 latest function declaration withing the same shell instance wins.

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.

爱的故事 2024-08-26 11:40:48

你也可以写一个shell函数; “ cd ”和“ls ”组合的示例此处

you can also write a shell function; example for " cd " and "ls " combo here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文