使用 bash 设置参数 [英] Setting an argument with bash
问题描述
我经常运行一个简单的 bash 命令:
I frequently run a simple bash command:
rpm -Uvh --define "_transaction_color 3" myPackage.rpm
哪个工作正常.
但现在我正在尝试将其编写到 bash 文件中,并使其更加灵活:
But now I'm trying to script it into a bash file, and make it more flexible:
#!/bin/bash
INSTALL_CMD=rpm
INSTALL_OPT="-Uvh --define '_transaction_color 3'"
${INSTALL_CMD} ${INSTALL_OPT} myPackage.rpm
然而,这不断产生错误:
However, this keeps generating the error:
error: Macro % has illegal name (%define)
错误来自 --define
和引用的 _transaction_color
的处理方式.
我尝试了各种转义、不同的措辞,甚至将 INSTALL_OPT
变成一个数组,用 ${INSTALL_OPT[@]}
处理.
The error is coming from how --define
and the quoted _transaction_color
is handled.
I've tried a variety of escaping, different phrasing, even making INSTALL_OPT
an array, handled with ${INSTALL_OPT[@]}
.
到目前为止,我的尝试没有奏效.
显然,我想要的非常简单.我只是不确定如何完成它.
So far, my attempts have not worked.
Clearly, what I want is extremely simple. I'm just not sure how to accomplish it.
如何让 bash 正确处理我的 --define
参数?
How can I get bash to handle my --define
argument properly?
推荐答案
问题是变量替换后没有处理引号.所以看起来您正在尝试定义一个名为 '_transaction_color
的宏.
The problem is that quotes are not processed after variable substitution. So it looks like you're trying to define a macro named '_transaction_color
.
尝试使用数组:
INSTALL_OPT=(-Uvh --define '_transaction_color 3')
然后:
"$INSTALL_CMD" "${INSTALL_OPT[@]}" myPackage.rpm
将 ${INSTALL_OPT[@]}
放在双引号内以获得重新引用很重要.
It's important to put ${INSTALL_OPT[@]}
inside double quotes to get the requoting.
这篇关于使用 bash 设置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!