简体   繁体   English

perl运行两个系统命令错误

[英]perl run two system commands error

So in my script I need to make to calls to unix, and I do it via the system command like so: 所以在我的脚本中我需要调用unix,我通过系统命令这样做:

system "bash -i -c 'addmothernode'";

...

perl code ...

...

system "bash -i -c 'addnode -ip=$_'";

However, whenever I run both of these commands in the same script, for some reason my process is stopped like this: 但是,每当我在同一个脚本中运行这两个命令时,由于某种原因,我的进程就像这样停止:

[1]+  Stopped                 perl boot.pl

And the script can only be finished when I run fg %1 . 并且只有在运行fg %1时才能完成脚本。 When I only have one of these system calls in, the perl script finishes successfully. 当我只有其中一个系统调用时,perl脚本成功完成。 But I need both commands because they depend on each other. 但我需要两个命令,因为它们相互依赖。 Anyone have any ideas about what's going on? 有什么想法发生了什么? Thanks! 谢谢!

UPDATE: 更新:

A lot of answers below are saying I don't need to use bash -i to run a system command, and I know typically this is true but I need to use aliases that I have created and if I do not use this the aliases won't be recognized. 下面的很多答案都说我不需要使用bash -i来运行系统命令,我知道这通常是正确的但是我需要使用我创建的别名,如果我不使用这个别名就赢了不被承认。 So I do need bash -i . 所以我确实需要bash -i

This problem is unrelated to perl. 这个问题与perl无关。 You can easily reproduce the situation if you start two bashes in the interactive mode ( -i ) one after another: 如果您在交互模式( -i )中一个接一个地启动两个bas,则可以轻松地重现这种情况:

$ cat 1.sh 
bash -i -c 'sleep 1'
bash -i -c 'sleep 1'

$ bash 1.sh

[1]+  Stopped                 bash 1.sh

Of course it would be better to run bash in the non-interactive mode (without -i ) or run the program directly, without bash, but if you need for some reason bash -i you can protect its run with setsid : 当然最好在非交互模式下运行bash(没有-i )或直接运行程序,而不用bash,但是如果你出于某种原因需要bash -i你可以用setsid来保护它的运行:

$ cat 1.sh
setsid bash -i -c 'sleep 1'
setsid bash -i -c 'sleep 1'
echo done

$ bash 1.sh
done

The bash -i means run an interactive shell; bash -i意味着运行一个交互式shell; so you have two shells both reading from the terminal. 所以你有两个炮弹都从终端读取。

Try removing the -i options. 尝试删除-i选项。

system "addmothernode"; 

should work. 应该管用。


To execute a command, bash is not needed. 要执行命令,不需要bash。 The Perl system function is like the system C function, it calls by default sh . Perl system函数就像系统C函数一样,它默认调用sh

man system 人系统

exec EXEC

The standard to which the caller conforms determines which shell is used. 调用者符合的标准决定了使用哪个shell。 See standards(5) . 标准(5)

Standard                                      Shell Used
______________________________________________________________
1989 ANSI  C,  1990  ISO  C,  1999  ISO  C,   /usr/xpg4/bin/sh
POSIX.1  (1990-2001),  SUS,  SUSv2,  SUSv3,
XPG4
POSIX.1 (1988), SVID3,  XPG3,  no  standard   /usr/bin/sh
specified

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

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