linux - 使用错误参数的 Perl 程序

标签 linux perl command-line-interface

我正在尝试在 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/

相关文章:

linux - SQLite:Fsyncing with journal_mode = wal and synchronous = normal

linux - duplicity --exclude 选项不排除提到的目录

python - 使用 Python 机器学习的 Directus 自定义过滤器/搜索

shell - GO 支持使用 native 系统 shell 构建 CLI 工具

angularjs - Angular 2 : Error while creating new component via CLI

PHP JSON 函数在被称为 CLI 脚本时不可用?

linux - 如何创建一个 git 别名以递归地在所有子模块上运行命令?

perl - Perl 5 中 `write` 和 `format` 的替代品是什么?

perl - 我的 perl 脚本不起作用,我感觉是 grep 命令

python - 优化 shell 脚本 (bash) 以提高性能