bash 使用bash脚本中的空格处理Perl命令行参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5194484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Handling Perl command line arguments with spaces from a bash script?
提问by Symen Timmermans
This has been driving me nuts for hours now.
这已经让我发疯了好几个小时了。
Consider the following test script in perl: (hello.pl)
考虑 perl 中的以下测试脚本:(hello.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";
}
Ok, it simply prints out the command line arguments given to the script.
好的,它只是打印出给脚本的命令行参数。
For instance:
例如:
$ ./hello.pl apple pie
----------------------------------
thanks, you gave me 2 command-line arguments:
apple
pie
I can give the script a single argument with a space by surrounding the words with double quotes:
通过用双引号将单词括起来,我可以给脚本一个带空格的参数:
$ ./hello.pl "apple pie"
----------------------------------
thanks, you gave me 1 command-line arguments:
apple pie
Now I want to use this script in a shell script. I've set up the shell script like this:
现在我想在 shell 脚本中使用这个脚本。我已经像这样设置了shell脚本:
#!/bin/bash
PARAM="apple pie"
COMMAND="./hello.pl \"$PARAM\""
echo "(command is $COMMAND)"
$COMMAND
I am calling the hello.pl with the same params and escaped quotes. This script returns:
我正在使用相同的参数和转义引号调用 hello.pl。该脚本返回:
$ ./test.sh
(command is ./hello.pl "apple pie")
----------------------------------
thanks, you gave me 2 command-line arguments:
"apple
pie"
Even though the $COMMAND variable echoes the command exactly like the way I ran the perl script from the command line the second time, this time it does not want to see the apple pie as a single argument.
尽管 $COMMAND 变量与我第二次从命令行运行 perl 脚本的方式完全一样地回显了命令,但这次它不想将苹果派视为单个参数。
Why not?
为什么不?
回答by Mark Longair
This looks like the problem described in the Bash FAQ as: I'm trying to put a command in a variable, but the complex cases always fail!
这看起来像 Bash FAQ 中描述的问题:我试图将命令放入变量中,但复杂的情况总是失败!
The answer to that FAQ suggests a number of possible solutions - I hope that's of use.
该常见问题的答案提出了许多可能的解决方案 - 我希望这是有用的。
回答by karl
The issue of the 2 command-line arguments
2个命令行参数的问题
"apple
pie"
is due to shell expansion with the IFS shell variable being set to have a space as value.
是由于将 IFS shell 变量设置为具有空格作为值的 shell 扩展。
printf '%q\n' "$IFS" # show value of IFS variable
You may use xargs & sh -c '...code...' to mimic / re-enable ordinary parameter parsing.
您可以使用 xargs & sh -c '...code...' 来模拟/重新启用普通参数解析。
PARAM="'apple pie'"
printf '%s' "$PARAM" | xargs sh -c './hello.pl "$@"' argv0
Another option may be to write a few lines of C (like in shebang.c)!
另一种选择可能是编写几行 C 语言(如在 shebang.c 中)!
回答by mouviciel
You should try eval $COMMANDinstead of simply $COMMAND.
你应该尝试eval $COMMAND而不是简单的$COMMAND。

