使用 bash 设置参数
我经常运行一个简单的 bash 命令:
rpm -Uvh --define "_transaction_color 3" myPackage.rpm
它工作正常。
但现在我尝试将其编写到 bash 文件中,并使其更加灵活:
#!/bin/bash
INSTALL_CMD=rpm
INSTALL_OPT="-Uvh --define '_transaction_color 3'"
${INSTALL_CMD} ${INSTALL_OPT} myPackage.rpm
但是,这不断生成错误:
error: Macro % has illegal name (%define)
该错误来自 --define
和引用的 _transaction_color
已处理。
我尝试了各种转义、不同的措辞,甚至使 INSTALL_OPT
成为一个数组,用 ${INSTALL_OPT[@]}
处理。
到目前为止,我的尝试还没有奏效。
显然,我想要的非常简单。我只是不确定如何实现它。
如何让 bash 正确处理我的 --define
参数?
I frequently run a simple bash command:
rpm -Uvh --define "_transaction_color 3" myPackage.rpm
which works properly.
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)
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.
How can I get bash to handle my --define
argument properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是变量替换后不处理引号。所以看起来您正在尝试定义一个名为
'_transaction_color
的宏。尝试使用数组:
然后:
将
${INSTALL_OPT[@]}
放在双引号内以获得重新引用非常重要。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
.Try using an array:
then:
It's important to put
${INSTALL_OPT[@]}
inside double quotes to get the requoting.这可能是空格上分词的 bash 问题:
尝试:
It might be a bash issue with word splitting on space:
Try: