加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在交互式bash shell下运行系统命令

发布时间:2020-12-15 21:54:58 所属栏目:安全 来源:网络整理
导读:我正在尝试使用system命令运行一个在Perl的?/ .bashrc中别名的命令.它只运行一次命令,但是当我运行它两次时,第二次调用作为后台作业运行然后暂停(与按下 CTRL-Z相同)我必须输入fg才能完成命令.例如 use strict;use warnings;system ('bash -ic "my_cmd"');sy
我正在尝试使用system命令运行一个在Perl的?/ .bashrc中别名的命令.它只运行一次命令,但是当我运行它两次时,第二次调用作为后台作业运行然后暂停(与按下< CTRL-Z>相同)我必须输入fg才能完成命令.例如

use strict;
use warnings;

system ('bash -ic "my_cmd"');
system ('bash -ic "my_cmd"');

第二个呼叫永远不会完成.输出为[1]已停止a.pl.

注意:

>使用任何其他命令替换my_cmd时获得相同的结果,例如ls.
>似乎不依赖于?/ .bashrc文件的内容.我试图从中删除所有内容,问题仍然存在.

我使用的是Ubuntu 14.04和Perl版本5.18.2.

更新

为了调试我将?/ .bashrc缩减为

echo "Entering ~/.bashrc .."
alias my_cmd="ls"
alias

和我的?/ .bash_profile来

if [ -f ~/.bashrc ]; then
    echo "Entering ~/.bash_profile .."
    . ~/.bashrc
fi

现在运行:

system ('bash -lc "my_cmd"');
system ('bash -lc "my_cmd"');

Entering ~/.bash_profile ..
Entering ~/.bashrc ..
alias my_cmd='ls'
bash: my_cmd: command not found
Entering ~/.bash_profile ..
Entering ~/.bashrc ..
alias my_cmd='ls'
bash: my_cmd: command not found

并运行

system ('bash -ic "my_cmd"');
system ('bash -ic "my_cmd"');

Entering ~/.bashrc ..
alias my_cmd='ls'
a.pl  p.sh

[1]+  Stopped                 a.pl

解决方法

我认为您应该使用-l(或–login)开关,而不是将-i开关用于交互式shell,这会导致bash的行为就好像它已被作为登录shell调用一样.

默认情况下,使用-l开关不会加载?/ .bashrc.根据man bash,在登录shell中,加载/ etc / profile /,然后加载从?/ .bash_profile /,?/ .bash_login或?/ .profile /找到的第一个文件.在我的系统上,我在?/ .bash_profile中有以下内容,所以?/ .bashrc被加载:

# Source .bashrc
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

现在您正在加载?/ .bashrc,您需要启用别名的扩展,这在非交互式shell中是关闭的.为此,您可以在设置别名之前添加以下行:

shopt -s expand_aliases

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读