简体   繁体   English

Bash变量的后期绑定,同时保留空格

[英]Late binding of Bash variables while preserving spaces

I'm trying to do late binding of the directories where my packages are in a make command. 我正在尝试对我的包在make命令中的目录进行后期绑定。 Basically, WITH_OPTIONS command line contains a space-separated list of link options similar to a configure script. 基本上,WITH_OPTIONS命令行包含一个以空格分隔的链接选项列表,类似于configure脚本。 The problem is the directories are bound late, after the command line to this script is parsed, so I need to eval the template string to get the right variable bindings. 问题是在解析了这个脚本的命令行之后,目录被绑定得很晚,所以我需要评估模板字符串以获得正确的变量绑定。 The closest I can come with so much googling and reading of the Bash FAQ, especially article 50 is something like: 最接近我可以用这么多谷歌搜索和阅读Bash常见问题,特别是第50条是这样的:

MAKE_INSTALL="make install"
TEMPLATE=('--first=$a' '--second=$b' '--third=$a')

# Parse Parameters
a=/first/path
b=/second/path
TEMPLATE_REPL=$( eval echo ${TEMPLATE[@]} )
CMD="$MAKE_INSTALL WITH_OPTIONS=\"$TEMPLATE_REPL\""
$CMD

What I'd expect $CMD to contain would be a string with one command and 2 parameters: 我期望$ CMD包含的是一个带有一个命令和两个参数的字符串:

make install WITH_OPTIONS="--first=/first/path --second=/second/path --third=/first/path"

However, I can't get bash to keep the part in quotes together, so it keeps parsing it as: 但是,我无法通过bash将部分保持在引号中,因此它会将其解析为:

make install 'WITH_OPTIONS="--first=/first/path' '--second=/second/path' '--third=/first/path"'

Which is obviously totally wrong and will cause a bad command line when executed. 这显然是完全错误的,并且在执行时会导致错误的命令行。

What am I missing? 我错过了什么? How do I preserve the bit between the quotes as one parameter, not 3? 如何将引号之间的位保留为一个参数,而不是3?

Why not simply make TEMPLATE a string: 为什么不简单地将TEMPLATE设为字符串:

TEMPLATE='--first=$a --second=$b --third=$a'
$ a=/first/path
$ b=/second/path
$ eval echo $TEMPLATE
--first=/first/path --second=/second/path --third=/first/path
MAKE_INSTALL="make install"
TEMPLATE='--first=$a --second=$b --third=$a'

# Parse Parameters
a=/first/path
b=/second/path
CMD="$MAKE_INSTALL WITH_OPTIONS=\"$TEMPLATE\""
eval $CMD

As ennuikiller pointed out, Arrays are unnecessary but because Johnathan Leffler points out make, the command I'm attempting to execute, is the thing ignoring the double quotes, the correct approach is to call eval when executing $CMD. 正如ennuikiller指出的那样,Arrays是不必要的,但是因为Johnathan Leffler指出make,我试图执行的命令是忽略双引号的东西,正确的方法是在执行$ CMD时调用eval。 This will require a bit of work to fit into my script but I think that's the basic solution to the problem as I set it up. 这将需要一些工作来适应我的脚本,但我认为这是我设置它时问题的基本解决方案。 Although I can't give ennuikiller full credit for this solution I'm happy to at least given you a +1 in your rating. 虽然我不能给予ennuikiller这个解决方案的全部功劳,但我很高兴至少给你一个+1的评分。

(Ed: Because the second eval is required, the first eval can be removed.) (Ed:因为需要第二个eval,所以可以删除第一个eval。)

Here's a generic example of using late binding in bash scripts, with variables (variable substitution): 这是在bash脚本中使用后期绑定的一般示例,包含变量(变量替换):

#!/usr/bin/env bash

A_tmp="echo -n Late binding: \$B !!!"             ## note the slash before $
B="test string"

A=`eval $A_tmp`

echo $A

This script prints: 这个脚本打印:

Late binding: test string !!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM