n0p*_*0pe 2 perl getopt-long command-line-arguments
我正在制作一个使用Getopt :: Long来解析命令行参数的perl脚本.但是,我有一个可以接受字符串(带空格)的参数.如何将整个字符串转换为变量.例如:
./script.pl --string=blah blah blah blah yup --another-opt
Run Code Online (Sandbox Code Playgroud)
我需要变量$ string中的"blah blah blah blah yup".我知道Getopt :: Long支持一个参数的多个输入,当你知道你将拥有多少时(我没有).这可能吗?
你需要在参数周围加上引号:
./script.pl --string="blah blah blah blah yup" --another-opt
Run Code Online (Sandbox Code Playgroud)
或逃离空间:
./script.pl --string=blah\ blah\ blah\ blah\ yup --another-opt
Run Code Online (Sandbox Code Playgroud)
这真的是对MaxMackie关于欧内斯特答案的问题的回复,但它太长了,所以这里是:
您的脚本永远不会看到引号,只看到传递参数的shell.当您调用脚本(或程序)时,较低级别上发生的事情是在数组中使用多个参数调用该命令.shell通常根据空格分割参数.你的程序现在看到的是:
[0]./script.pl
[1]--string=blah
[2]blah
[3]blah
[4]blah
[5]yup
[6]--another-opt
Run Code Online (Sandbox Code Playgroud)
在字符串周围放置引号或转义它会导致:
[0]./script.pl
[1]--string=blah blah blah blah yup
[2]--another-opt
Run Code Online (Sandbox Code Playgroud)
贝壳(特别是bash,其他我很确定)允许在没有空格的情况下点缀引号.ls -"l""h"是一样的ls -lh,所以你做"--string=blah blah"或做--string="blah blah".使用getopt解析各个参数,这些参数允许您将它们置于无序状态并使用--long-name或-l在将所有内容转换为数组项并传递给程序后发生.shell与此无关.
| 归档时间: |
|
| 查看次数: |
4800 次 |
| 最近记录: |