perl 脚本参数包含双配额“

发布于 2024-11-01 10:39:07 字数 575 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中楼上月下 2024-11-08 10:39:08
  • 当然,你会失去双引号。这是标准的 *NIX 命令行行为,其中双引号表示您希望将其中的内容视为一个参数。
  • 当您打印项目列表时,它们将简单地连接起来。

实现您想要的效果的最简单方法是 print "@ARGV"; 因为默认列表分隔符(对于内插列表)是一个空格。

  • Of course you lose the double quotes. That's standard *NIX command line behavior, where double quotes indicates that you want the stuff inside them treated as one argument.
  • And when you 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.

泅渡 2024-11-08 10:39:07

你的 Perl 程序不接收 3 个参数,它接收 6 个参数。只需打印出来亲自查看:

perl -le 'print for @ARGV' 2MT5 4XAW KEAR TTRR YYMM "TEMP 2012 FEB 01":Single:123.x

您的第一个参数 (2MT5) 不是“很长”,它只有 4 个字符。

你没有遇到 Perl 问题,你有一个 shell 问题。 shell 在空格上分割 args 并处理双引号。如果您不希望它这样做,则必须引用任何包含空格或引号的参数:

./my_script.pl '2MT5 4XAW KEAR TTRR YYMM "TEMP 2012 FEB 01":Single:123.x'

Your Perl program does NOT receive 3 arguments, it receives 6 arguments. Simply print them out to see for yourself:

perl -le 'print for @ARGV' 2MT5 4XAW KEAR TTRR YYMM "TEMP 2012 FEB 01":Single:123.x

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:

./my_script.pl '2MT5 4XAW KEAR TTRR YYMM "TEMP 2012 FEB 01":Single:123.x'
何处潇湘 2024-11-08 10:39:07

这是 shell 丢弃了这些字符。正确地传递参数(包括引用任何包含空格的参数,并转义其中的任何引号),这不会成为问题:

./my_script.pl "2MT5 4XAW KEAR TTRR YYMM \"TEMP 2012 FEB 01\"" "Single" "123.x"

那么您就不需要浪费时间进行拆分和连接。首先,您将正确组织论据。

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:

./my_script.pl "2MT5 4XAW KEAR TTRR YYMM \"TEMP 2012 FEB 01\"" "Single" "123.x"

Then you don't need to mess around with splitting and joining. You'll have the arguments properly organised in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文