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?

有帮助吗?

解决方案 2

If you're smart, you made it so your alias is only defined for interactive shells, so you'll have to launch bash and specify that you want an interactive shell using -i.

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

其他提示

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:

Is it working?

system 'bash -i -c "your alias parameter"';
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top