简体   繁体   中英

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. I know that the alias works because when I invoke it directly in a shell, it opens fine.

Funny enough, I can do system("firefox") within my script fine, but not the alias. How do I use this alias in this script without it breaking?

Perl won't run bash, it will execute the command directly. You can call

bash -c your_command

instead of calling the command itself in Perl.

As it is, this doesn't load your aliases. You need to open an interactive shell, as in @MortezaLSC's answer. There supposedly is a way of loading aliases correctly in a non-interactive shell, but I can't figure it out.

But why don't you just use the command you have aliased to directly in 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 has functions for doing more complicated stuff, but why would you want to call non-trivial bash code from a perl script? 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).

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"';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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