【问题标题】:Trouble With Spaces In Bash Script VariableBash 脚本变量中的空格问题
【发布时间】:2019-05-02 06:41:42
【问题描述】:

我正在尝试在 bash 脚本中创建一个变量,但第一个空格会导致问题。

变量看起来像这样:

VAR="
   --option1=foo \
   --option2=bar \
   --option3='something here with spaces'"

我正在尝试运行“make install”,但它总是抱怨第一个空格(即在上例中的“here”一词之前)。

如何创建带空格的变量?我尝试使用双引号,各种尝试转义单引号,但我无法工作。

我做错了什么?

下面的实际代码:

            NGINX_OPTIONS="
            --prefix=/etc/nginx \
            --sbin-path=/usr/sbin/nginx \
            --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-ld-opt='-ljemalloc -Wl,-z,relro -Wl,-rpath,/usr/local/lib -Wl,--as-needed -pie' \
            --with-cc-opt='-m64 -march=native -DTCP_FASTOPEN=23 -g -O3 -Wno-error=strict-aliasing -fstack-protector-strong -flto -fuse-ld=gold --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wno-deprecated-declarations -gsplit-dwarf'"

            NGINX_MODULES="
            --with-compat \
            --with-threads \
            --with-file-aio \
            --with-http_addition_module \
            --with-http_auth_request_module \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_mp4_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --with-http_realip_module \
            --with-http_secure_link_module \
            --with-http_slice_module \
            --with-http_sub_module \
            --with-mail \
            --with-mail_ssl_module \
            --with-stream \
            --with-stream_realip_module \
            --with-stream_ssl_module \
            --with-stream_ssl_preread_module"

            ./configure $NGINX_OPTIONS $NGINX_MODULES
            make
            make install

【问题讨论】:

  • 用反斜杠转义空格。像这样的东西:--option3='something\ here\ with\ spaces'
  • 不起作用。说 ./configure: error: invalid option "here\" (我将这里的单词替换为第一个空格后的第二个参数)
  • 用反斜杠转义引号,然后:--option3=\'something here with spaces\'
  • 显示使用这个变量的代码?
  • 可以是Makfile吗?尝试将其复制到 shellscript 中,然后将 /configure $NGINX_OPTIONS $NGINX_MODULES 替换为 echo $NGINX_OPTIONS $NGINX_MODULES

标签: bash nginx


【解决方案1】:

您尚未展示如何使用此变量,但如果我理解正确,您的命令包含引号外的 $VAR

问题是这样的:

echo $VAR

等价于:

echo "--option1=foo" "--option2=bar" "--option3='something" "here" "with" "spaces'"

因为变量内的空格很重要,但变量内的引号不重要。

相反,您需要使用一个数组。您可以像这样声明变量:

VAR=(
   --option1=foo
   --option2=bar
   --option3='something here with spaces'
)

并像这样使用它:

echo "${VAR[@]}"       # note the quotation-marks

相当于这个:

echo "--option1=foo" "--option2=bar" "--option3=something here with spaces"

【讨论】:

  • 我已经添加了我的实际代码。不知道如何使用我的代码申请。
  • 我需要 var 等于 --option1=foo --option2=bar --option3='something here'
  • @DarioZadro:我很确定你错了;这可能不是你真正需要的。您想要这样的唯一原因是,如果您将变量插入到将得到 re 解析的命令中(例如,通过eval,或通过单独调用 Bash) .从您发布的内容来看,不是似乎是这种情况。
【解决方案2】:

我尝试了所有可能的转义引号组合,最后得到了一个“配置”语句,没有像这样的双引号或单引号变量:

    ./configure --prefix=/etc/nginx \
                --sbin-path=/usr/sbin/nginx \
                --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-cc-opt="-m64 -march=native -DTCP_FASTOPEN=23 -g -O3 -Wno-error=strict-aliasing -fstack-protector-strong -flto -fuse-ld=gold --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wno-deprecated-declarations -gsplit-dwarf" \
        --with-ld-opt="-ljemalloc -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie" \
        $NGINX_MODULES

我也遇到了一些错误,例如必须使用以下方法安装“jemalloc”:

apt-get install libjemalloc-dev

这是从源代码构建 nginx 的一个更大的练习,而不是 bash 脚本中的空格或引号。我希望这可以帮助其他人解决我遇到的许多挑战。

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2013-04-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2020-11-02
    相关资源
    最近更新 更多