简体   繁体   English

如何让Perl运行我在BASH中定义的别名?

[英]How do I get Perl to run an alias I've defined in BASH?

I have a script that opens up different files at the same time. 我有一个脚本,可以同时打开不同的文件。

My problem here is that when I run system() on the alias I've defined in bash which points to /home/user1/Software/nc, perl tells me that it can't execute the alias because there is no file/directory at the current location. 我的问题是,当我在bash中定义的别名上运行system()时指向/ home / user1 / Software / nc,perl告诉我它不能执行别名,因为没有文件/目录在当前位置。 I know that the alias works because when I invoke it directly in a shell, it opens fine. 我知道别名有效,因为当我直接在shell中调用它时,它打开很好。

Funny enough, I can do system("firefox") within my script fine, but not the alias. 有趣的是,我可以在我的脚本中做系统(“firefox”),但不是别名。 How do I use this alias in this script without it breaking? 如何在不破坏此脚本的情况下使用此别名?

Perl won't run bash, it will execute the command directly. Perl不会运行bash,它会直接执行命令。 You can call 你可以打电话

bash -c your_command

instead of calling the command itself in Perl. 而不是在Perl中调用命令本身。

As it is, this doesn't load your aliases. 实际上,这不会加载您的别名。 You need to open an interactive shell, as in @MortezaLSC's answer. 您需要打开一个交互式shell,如@ MortezaLSC的答案。 There supposedly is a way of loading aliases correctly in a non-interactive shell, but I can't figure it out. 据说有一种方法可以在非交互式shell中正确加载别名,但我无法弄明白。

But why don't you just use the command you have aliased to directly in perl? 但是你为什么不直接在perl中使用别名的命令呢? The only reason I could see not to do this, is if your alias is going to change in the future, but you will still want to run whatever command it points to. 我可以看到不这样做的唯一原因是,如果您的别名将来会发生变化,但您仍然希望运行它指向的任何命令。 This seems weird and dangerous to say the least. 至少可以说这看起来很怪异和危险。

Aliases are designed to reduce the typing you do if you invoke commands with the same options etc all the time. 别名旨在减少您在使用相同选项等始终调用命令时所执行的键入操作。 They're not all-purpose macros for bash. 它们不是用于bash的通用宏。 Bash has functions for doing more complicated stuff, but why would you want to call non-trivial bash code from a perl script? Bash具有执行更复杂内容的功能,但是为什么要从perl脚本中调用非平凡的bash代码呢? It doesn't seem like you really need this here. 看起来你真的不需要这个。 Keep the complexity, and the potential for modification and failure, in one place (the perl script). 在一个地方(perl脚本)保持复杂性,以及修改和失败的可能性。

See a couple of answers to similar questions: 查看类似问题的几个答案:

如果你很聪明,你就做了它,所以你的别名只是为交互式shell定义的,所以你必须启动bash并指定你想要一个使用-i的交互式shell。

system('bash', '-i', '-c', 'shell command');

它有效吗?

system 'bash -i -c "your alias parameter"';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM