Autoconf 脚本在处理带有空格的文件名或路径名时遇到问题。例如,
./configure CPPFLAGS="-I\"/path with space\""
结果(config.log):
configure:3012: gcc -I"/path with space" conftest.c >&5
gcc: with: No such file or directory
gcc: space": No such file or directory
./configure 中的编译命令是ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
我无法修改它(我也许可以,但以这种方式解决 autoconf 并不是一个通用的解决方案)。
我认为这归结为获取一个包含空格的 shell 变量,将其解析为单个命令行变量,而不是在空格处拆分。我能想到的最简单的 shell 示例是创建一个带有空格的文件并尝试列出ls
以 shell 变量作为参数ls
:
$ touch "a b"
$ file="a b"
$ ls $file
ls: a: No such file or directory
ls: b: No such file or directory
这可行,但非法,因为在 autoconf 中我无法修改 shell 代码:
$ ls "$file"
a b
以下引用事物的尝试均无效:
$ file="\"a \"b"; ls $file
ls: "a: No such file or directory
ls: b": No such file or directory
$ file="a\ b"
$ file="a\\ b"
$ file="`echo \\"a b\\"`"
等等。
这不可能在 shell 脚本中完成吗?是否有一个神奇的引用可以将带有空格的 shell 变量扩展为单个命令行参数?
您应该尝试设置$IFS
环境变量。
来自 man bash(1):
IFS - 用于分词的内部字段分隔符
扩展后并使用 read 内置函数将行分割成单词
命令。默认值为“空格制表符换行符”。
例如
IFS=<C-v C-m> # newline
file="a b"
touch $file
ls $file
别忘了设置$IFS
回来或者会发生奇怪的事情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)