简体   繁体   English

如何将命令行参数传递给应该在后台运行的脚本?

[英]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.

相关问题 如何将带有后向引用的替换正则表达式作为命令行参数传递给Perl脚本 - How to pass a replacing regex with a backreference as a command line argument to a Perl script 如何将替换正则表达式作为命令行参数传递给perl脚本 - How to pass a replacing regex as a command line argument to a perl script 如何将空字符串从perl脚本传递到java文件作为命令行参数 - how to pass an empty String from perl Script to java file as a command line argument 如何将输入从脚本传递到命令行 - How to pass inputs from a script to command line 如何将 tcl 数组传递给 perl 命令行参数 - How to pass tcl array to perl command line argument 如何在命令行中将哈希作为可选参数传递给 -M - How to pass a hash as optional argument to -M in command line 如何将强制和可选的命令行参数传递给 perl 脚本? - How to pass both mandatory and optional command line arguments to perl script? 如何在不使用命令行的情况下运行 Perl 脚本? - How can I run a Perl script without using the command line? 如何将参数传递给 perl 脚本 - How to pass an argument to a perl script 如何在Perl中传递一系列数字(例如:100到200)作为命令行参数? - How do I pass a range of numbers (eg: 100 to 200) as command line argument in Perl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM