2

Getpt::Long用来从命令行获取参数并将它们分配给它们各自的变量。但是,我在打印时遇到错误。代码和错误如下:

#!usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

GetOptions(
    "mount_path=s" => \my $old_path,
    "var=s"        => \my $var,
    "log_path=s"   => \my $log_path,
) or die "Error in input variables\n";

print <<"END_INPUTS";
 These are your inputs: 
 old_path= $old_path 
 var = $var 
 log_path=$log_path 
 Press enter twice if all looks GOOD 
 *********************************************************
END_INPUTS

命令行参数如下:

 perl getvar.pl --mount_path=/var/sslvpn --var=7.0.0.2_va-SSLVPN-!7.0.0.2.16sv+.jpn-05!j+g_554863- --log_path=log.txt  

运行此程序时出现以下错误

-bash: !7: event not found
4

1 回答 1

8

This is not a Perl problem. The bash shell is processing ! as a special character. You'll have to quote that argument.

 --var='7.0.0.2_va-SSLVPN-!7.0.0.2.16sv+.jpn-05!j+g_554863-' 

You can tell that it's a bash problem and not a Perl problem because the message says it's from bash:

-bash: !7: event not found

Bash never even gets to the part where it runs your program.

于 2014-06-16T23:00:31.387 回答