[英]how to pass command line argument to a script which should be run in the background?
How to pass command line argument to a script which should be run in the background? 如何将命令行参数传递给应该在后台运行的脚本? I tried the below command and it gives an error:
我尝试了以下命令,它给出了一个错误:
system("perl sample.pl& 1");
Error: 1: command not found
Arguments to the program being run in the background have to be placed before the ampersand: 在后台运行的程序的参数必须放在与号之前:
perl sample.pl 1 &
What you typed is recognized by the shell (I'm assuming you're on a Unix or Linux variant) as 2 separate commands: Shell会将您键入的内容识别为2个单独的命令(假设您使用的是Unix或Linux变体):
perl sample.pl
which is run in the background, and 在后台运行
1
Where the shell is reporting that 1 is not a valid command. 外壳程序报告1不是有效命令的位置。
The reason that does not work is due to the all arguments must be before the ampersand 之所以无效,是因为所有参数都必须在“&”号之前
So system("perl sample.pl 1 &");
因此,
system("perl sample.pl 1 &");
Alternatively you can use exec() or backticks. 或者,您可以使用exec()或反引号。 For example
`perl sample.pl 1 &`
例如
`perl sample.pl 1 &`
If you use exec though, just know it never returns where system does. 但是,如果您使用exec,就知道它永远不会返回系统执行的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.