我正在尝试在 perl 中创建简单的启动脚本,它将在系统启动时启动各种程序。如下
my @startupPrograms = qw(google-chrome thunderbird skype pidgin );
my @pagesToBeOpenedInChrome = qw(http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox);
sub runPrograms() {
print("Starting startup Programs... \n");
foreach (@startupPrograms) {
my $command = $_;
print "Starting Program " . $command . "\n";
if($command == "google-chrome") {
foreach (@pagesToBeOpenedInChrome) {
$command = $command . " " . $_;
}
}
`$command &`;
print "Program " . $command . " started \n";
}
}
但是我得到的输出是
[aniket@localhost TestCodes]$ ./startUp.pl
***** Welcome to startup program! *****
Starting startup Programs...
Starting Program google-chrome
Program google-chrome http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox started
Starting Program thunderbird
Program thunderbird http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox started
Starting Program skype
Program skype http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox started
Starting Program pidgin
Program pidgin http://www.google.com/ http://stackoverflow.com/ https://mail.google.com/mail/u/0/#inbox started
为什么要执行的每条命令都取pagesToBeOpenedInChrome数组内容?此外,如果我将 pidgin 放在其他程序之前,它似乎需要永远启动 pidgin(其他程序被锁定)。感谢您提供任何帮助或建议。
最佳答案
改变
if($command == "google-chrome") {
到
if($command eq "google-chrome") {
在 perl 中,您使用 eq 或 ne 进行文本比较,使用 == 进行数字比较!
对文本使用 == 基本上说明 $command 是否为空或 0
关于linux - 使用错误参数的 Perl 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18210417/