perl 脚本参数包含双配额“
我有一个 Perl 脚本,它接收 3 个参数。
第一个参数非常长,包含空格和引号,我实际上不知道期望的大小它可以是任何大小。为了分隔我的论点,我使用“:”符号。
参见示例: ./my_script.pl 2MT5 4XAW KEAR TTRR YYMM "TEMP 2012 FEB 01":Single:123.x
问题是我丢失了双引号和空格。查看输出:
LOG The 1st input is:2MT54XAWKEARTTRRYYMMTEMP 2012 FEB 01
LOG Type is:Single
LOG Version is:123.x
我的代码:
open (FD, ">file2.txt");
print FD @ARGV;
close FD;
my $str1=`cat file2.txt`;
my @argv_values = split(':',$str1);
$new_str = $argv_values[0];
$type = $argv_values[1];
$ver = $argv_values[2];
I have a perl script that receives 3 arugments.
First argument is very long and contains spaces and quotes and I actually don't know what size to expect it could be any size . To separate my arguments I use ":" sign.
See example:
./my_script.pl 2MT5 4XAW KEAR TTRR YYMM "TEMP 2012 FEB 01":Single:123.x
The problem is that I lose double quotes and spaces.See output :
LOG The 1st input is:2MT54XAWKEARTTRRYYMMTEMP 2012 FEB 01
LOG Type is:Single
LOG Version is:123.x
My Code :
open (FD, ">file2.txt");
print FD @ARGV;
close FD;
my $str1=`cat file2.txt`;
my @argv_values = split(':',$str1);
$new_str = $argv_values[0];
$type = $argv_values[1];
$ver = $argv_values[2];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
打印
项目列表时,它们将简单地连接起来。实现您想要的效果的最简单方法是
print "@ARGV";
因为默认列表分隔符(对于内插列表)是一个空格。print
a list of items, they are going to be concatenated simply.The simplest way to do what you're intending is to
print "@ARGV";
because the default list separator, for interpolated lists, is a single space.你的 Perl 程序不接收 3 个参数,它接收 6 个参数。只需打印出来亲自查看:
您的第一个参数 (2MT5) 不是“很长”,它只有 4 个字符。
你没有遇到 Perl 问题,你有一个 shell 问题。 shell 在空格上分割 args 并处理双引号。如果您不希望它这样做,则必须引用任何包含空格或引号的参数:
Your Perl program does NOT receive 3 arguments, it receives 6 arguments. Simply print them out to see for yourself:
Your first argument (2MT5) is not "very long" it is only 4 characters.
You do not have a Perl problem, you have a shell problem. The shell splits args on spaces and processes double quotes. If you don't want it to do that, then you must quote any args that contain spaces or quotes:
这是 shell 丢弃了这些字符。正确地传递参数(包括引用任何包含空格的参数,并转义其中的任何引号),这不会成为问题:
那么您就不需要浪费时间进行拆分和连接。首先,您将正确组织论据。
It is the shell discarding those characters. Pass arguments properly (which includes quoting any which contain spaces, and escaping any quotes inside) and that won't be a problem:
Then you don't need to mess around with splitting and joining. You'll have the arguments properly organised in the first place.