加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 带空格的Shell变量,引用单个命令行选项

发布时间:2020-12-15 18:49:31 所属栏目:安全 来源:网络整理
导读:Autoconf脚本在使用空格的文件名或路径名时遇到问题.例如, ./configure CPPFLAGS="-I"/path with space"" 结果(config.log): configure:3012: gcc -I"/path with space" conftest.c 5gcc: with: No such file or directorygcc: space": No such file or d
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示例是创建一个带有空格的文件,并尝试列出一个带有一个shell变量的ls作为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环境变量.

从男子bash(1):

IFS – The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read builtin
command. The default value is ”space tab newline”.

例如

IFS=<C-v C-m>  # newline
file="a b"
touch $file
ls $file

不要忘记设置$IFS回来或奇怪的事情会发生.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读