从带引号的变量传递 bash 参数

从带引号的变量传递 bash 参数

我正在尝试构建一个 Nginx 模块。我找到的说明中有以下配置行:

./configure --add-dynamic-module=my-module-path $(nginx -V)

运行没有错误,但它创建的模块不起作用。我能够通过提取参数nginx -V并手动构建它来使其工作。

让它工作之后,我尝试自动化该过程并创建了以下脚本:

apt-get source nginx
cd nginx-`nginx -v 2>&1 | sed 's/[^0-9.]*//g'`
CONFIG=`nginx -V 2>&1 | tr '\n' ' ' | sed 's/^.* configure arguments: //g'`
echo "./configure --add-dynamic-module=my-module-path $CONFIG"
./configure --add-dynamic-module=my-module-path $CONFIG
make

运行 bash 脚本时,配置失败,./configure: error: invalid option "-O2" 现在如果我获取回显的行并运行它,它就可以完美运行。

./configure --add-dynamic-module=my-module-path --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.17.6/debian/debuild-base/nginx-1.17.6=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

错误是由这个参数引起的,-02 应该放在引号中,而不是由 configure 直接处理,而是传递给 CC

--with-cc-opt='-g -O2 -fdebug-prefix-map=.....'

我还尝试在 $CONFIG 周围加上双引号,虽然可以运行,但会出现不同的错误:

dirname: unrecognized option '--sbin-path=/usr/sbin/nginx'
Try 'dirname --help' for more information.

我甚至尝试将整个内容放在反引号中,但却出现了line 6: ./configure:: No such file or directory一个多余的冒号,这很奇怪。

如何让 $CONFIG 中的参数在 ./configure 中被正确识别

答案1

这是我采用的解决方案,它很简单,解决了问题,尽管不是很干净。使用该命令创建一个新的 shell 脚本并运行它。

apt-get source nginx
cd nginx-`nginx -v 2>&1 | sed 's/[^0-9.]*//g'`
CONFIG=`nginx -V 2>&1 | tr '\n' ' ' | sed 's/^.* configure arguments: //g'`
echo "./configure --add-dynamic-module=my-module-path $CONFIG" > tempconfigure.sh
chmod 755 tempconfigure.sh
./tempconfigure.sh
rm tempconfigure.sh
make

相关内容