Shell-Script

將文本行添加到規則文件的部分

  • January 31, 2019

我正在嘗試編寫一個 bash 腳本來使用 pagespeed 模組自動安裝 nginx。

其中一部分需要我添加:--add-module=$(MODULESDIR)/ngx_pagespeed \/usr/src/nginx/nginx-X.X.6/debian/rules文件的一部分。

每個部分都類似於:

light_configure_flags := \
                   $(common_configure_flags) \
                   --with-http_gzip_static_module \
                   --without-http_browser_module \
                   --without-http_geo_module \
                   --without-http_limit_req_module \
                   --without-http_limit_conn_module \
                   --without-http_memcached_module \
                   --without-http_referer_module \
                   --without-http_scgi_module \
                   --without-http_split_clients_module \
                   --without-http_ssi_module \
                   --without-http_userid_module \
                   --without-http_uwsgi_module \
                   --add-module=$(MODULESDIR)/nginx-echo

完整文件在這裡: https ://jsfiddle.net/72hL5pya/1/ (對不起,不知道該放在哪裡)

每個部分都是 *_configure_flags

每個“版本”的 nginx 都有許多這些部分要編譯(輕量級、完整版、附加版等)

所以最後--add-module=一行在每個部分都是不同的。

我怎樣才能附加--add-module=$(MODULESDIR)/ngx_pagespeed \到每個?

注意:看起來從任何日期開始,此rules文件的結構都已更改。

#!/usr/bin/make -f

#export DH_VERBOSE=1
CFLAGS ?= $(shell dpkg-buildflags --get CFLAGS)
LDFLAGS ?= $(shell dpkg-buildflags --get LDFLAGS)
WITH_HTTP2 := $(shell printf \
   "Source: nginx\nBuild-Depends: libssl-dev (>= 1.0.1)\n" | \
   dpkg-checkbuilddeps - >/dev/null 2>&1 && \
   echo "--with-http_v2_module")
PKGS = nginx nginx-dbg \
   nginx-module-xslt nginx-module-geoip nginx-module-image-filter \
   nginx-module-perl nginx-module-njs
COMMON_CONFIGURE_ARGS := \
   --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-http_ssl_module \
   --with-http_realip_module \
   --with-http_addition_module \
   --with-http_sub_module \
   --with-http_dav_module \
   --with-http_flv_module \
   --with-http_mp4_module \
   --with-http_gunzip_module \
   --with-http_gzip_static_module \
   --with-http_random_index_module \
   --with-http_secure_link_module \
   --with-http_stub_status_module \
   --with-http_auth_request_module \
   --with-http_xslt_module=dynamic \
   --with-http_image_filter_module=dynamic \
   --with-http_geoip_module=dynamic \
   --with-http_perl_module=dynamic \
   --add-dynamic-module=debian/extra/njs-1c50334fbea6/nginx \
   --with-threads \
   --with-stream \
   --with-stream_ssl_module \
   --with-http_slice_module \
   --with-mail \
   --with-mail_ssl_module \
   --with-file-aio \
   --with-ipv6 \
   $(WITH_HTTP2) \
   --with-cc-opt="$(CFLAGS)" \
   --with-ld-opt="$(LDFLAGS)"

%:
   dh $@ 

override_dh_auto_configure: configure_debug

override_dh_strip:
   dh_strip --dbg-package=nginx-dbg

override_dh_auto_build:
   dh_auto_build
   mv objs/nginx objs/nginx-debug
   mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so
   mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so
   mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so
   mv objs/ngx_http_perl_module.so objs/ngx_http_perl_module-debug.so
   mv objs/src/http/modules/perl/blib/arch/auto/nginx/nginx.so objs/src/http/modules/perl/blib/arch/auto/nginx/nginx-debug.so
   mv objs/ngx_http_js_module.so objs/ngx_http_js_module-debug.so
   CFLAGS="" ./configure $(COMMON_CONFIGURE_ARGS)
   dh_auto_build

configure_debug:
   CFLAGS="" ./configure $(COMMON_CONFIGURE_ARGS) \
       --with-debug

override_dh_auto_install:
   sed -e 's/%%PROVIDES%%/nginx/g' \
       -e 's/%%DEFAULTSTART%%/2 3 4 5/g' \
       -e 's/%%DEFAULTSTOP%%/0 1 6/g' \
       < debian/init.d.in > debian/init.d
   dh_auto_install
   mkdir -p debian/nginx/etc/init.d debian/nginx/etc/default \
       debian/nginx/usr/lib/nginx/modules
   sed -e 's/%%PROVIDES%%/nginx-debug/g' \
       -e 's/%%DEFAULTSTART%%//g' \
       -e 's/%%DEFAULTSTOP%%/0 1 2 3 4 5 6/g' \
       < debian/init.d.in > debian/debug.init.d
   /usr/bin/install -m 755 debian/debug.init.d \
       debian/nginx/etc/init.d/nginx-debug
   /usr/bin/install -m 644 debian/nginx-debug.default \
       debian/nginx/etc/default/nginx-debug
   /usr/bin/install -m 644 debian/nginx.conf debian/nginx/etc/nginx/
   /usr/bin/install -m 644 conf/win-utf debian/nginx/etc/nginx/
   /usr/bin/install -m 644 conf/koi-utf debian/nginx/etc/nginx/
   /usr/bin/install -m 644 conf/koi-win debian/nginx/etc/nginx/
   /usr/bin/install -m 644 conf/mime.types debian/nginx/etc/nginx/
   /usr/bin/install -m 644 conf/scgi_params debian/nginx/etc/nginx/
   /usr/bin/install -m 644 conf/fastcgi_params debian/nginx/etc/nginx/
   /usr/bin/install -m 644 conf/uwsgi_params debian/nginx/etc/nginx/
   /usr/bin/install -m 644 html/index.html \
       debian/nginx/usr/share/nginx/html/
   /usr/bin/install -m 644 html/50x.html \
       debian/nginx/usr/share/nginx/html/
   /usr/bin/install -m 644 debian/nginx.vh.default.conf \
       debian/nginx/etc/nginx/conf.d/default.conf
   /usr/bin/install -m 755 objs/nginx  debian/nginx/usr/sbin/
   /usr/bin/install -m 755 objs/nginx-debug  debian/nginx/usr/sbin/
   cd debian/nginx/etc/nginx && /bin/ln -s \
       ../../usr/lib/nginx/modules modules && cd -

override_dh_gencontrol:
   for p in $(PKGS); do \
       if [ -e debian/$$p.version ]; then \
           dpkg-gencontrol -p$$p -ldebian/changelog -Tdebian/$$p.substvars -Pdebian/$$p -v`cat debian/$$p.version`~`lsb_release -cs`; \
       else \
           dpkg-gencontrol -p$$p -ldebian/changelog -Tdebian/$$p.substvars -Pdebian/$$p ; \
       fi ; \
   done

override_dh_clean:
   dh_clean
   rm -f debian/*init.d

如果新選項在選項列表中的位置無關緊要,您可以

sed '/_configure_flags *:=/ a\
                   --add-module=$(MODULESDIR)/ngx_pagespeed \\
' file
light_configure_flags := \
                   --add-module=$(MODULESDIR)/ngx_pagespeed \
                   $(common_configure_flags) \
                   --with-http_gzip_static_module \
...

要在“common_configure_flags”行之後添加,您可以:

sed -r '
   # when the line ends with a backslash
   # add the new line with a backslash
   /\$\(common_configure_flags\)[[:blank:]]*\\$/ a\
                   --add-module=$(MODULESDIR)/ngx_pagespeed \\

   # when the line does not end with a backslash,
   # add a backslash, then
   # add the new line without a backslash
   /\$\(common_configure_flags\)[[:blank:]]*$/ {
           s/$/ \\/
           a\
                   --add-module=$(MODULESDIR)/ngx_pagespeed
   }
' file

引用自:https://unix.stackexchange.com/questions/250036