我如何在perl中获得我的bash别名列表?

dfty9e19  于 8个月前  发布在  Perl
关注(0)|答案(2)|浏览(84)

我知道我能做

alias

在巴什找到我的化名名单
我想在Perl中这样做:

qx(alias)

并获取上述命令的输出进行处理。
然而,这是行不通的(它的工作正常-说ls)。
我做错了什么?

wfypjpf4

wfypjpf41#

每个bash进程都有自己的别名列表。
如果你想要一个新启动的交互式bash进程的别名,[1]你可以执行

bash -i -c alias

要执行该命令,可以使用

my $output = qx{bash -i -c alias};
die( "Couldn't execute child: $!\n"               ) if $? == -1;
die( "Child killed by signal ".( $? & 0x7F )."\n" ) if $? & 0x7F;
die( "Child exited with error ".( $? >> 8 )."\n"  ) if $? >> 8;

use IPC::System::Simple qw( capturex );

my $output = capturex( "bash", "-i", "-c", "alias" );

后者为您处理错误检查,并且避免运行第二个shell(避免运行sh来运行bash)。
1.别名通常不会在非交互式shell中创建。

r7xajy2e

r7xajy2e2#

因为alias是一个内置在交互式shell中的shell,所以可以尝试:

qx(bash -ci alias)

相关问题