gpt4 book ai didi

perl - 使用 bash 脚本中的空格处理 Perl 命令行参数?

转载 作者:行者123 更新时间:2023-11-29 09:04:01 24 4
gpt4 key购买 nike

这几个小时以来一直让我抓狂。

考虑以下 perl 测试脚本:(你好.pl)

#!/usr/bin/perl
print "----------------------------------\n";
$numArgs = $#ARGV + 1;
print "thanks, you gave me $numArgs command-line arguments:\n";

foreach $argnum (0 .. $#ARGV) {
print "$ARGV[$argnum]\n";
}

好的,它只是打印出提供给脚本的命令行参数。

例如:

$ ./hello.pl apple pie
----------------------------------
thanks, you gave me 2 command-line arguments:
apple
pie

我可以通过用双引号将单词括起来来为脚本提供一个带空格的参数:

$ ./hello.pl "apple pie"
----------------------------------
thanks, you gave me 1 command-line arguments:
apple pie

现在我想在 shell 脚本中使用这个脚本。我已经像这样设置了 shell 脚本:

#!/bin/bash

PARAM="apple pie"
COMMAND="./hello.pl \"$PARAM\""

echo "(command is $COMMAND)"
$COMMAND

我用相同的参数和转义引号调用 hello.pl。该脚本返回:

$ ./test.sh 
(command is ./hello.pl "apple pie")
----------------------------------
thanks, you gave me 2 command-line arguments:
"apple
pie"

尽管 $COMMAND 变量以与我第二次从命令行运行 perl 脚本的方式完全相同的方式回显命令,但这次它不想将苹果派视为单个参数。

为什么不呢?

最佳答案

这看起来像 Bash FAQ 中描述的问题:I'm trying to put a command in a variable, but the complex cases always fail!

该常见问题的答案提出了一些可能的解决方案 - 我希望这些有用。

关于perl - 使用 bash 脚本中的空格处理 Perl 命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5194484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com