简体   繁体   中英

linux bash string replace

The actual requirement here is to replace some tags in an nginx server block with values at runtime, fairly standard stuff:

server {
    listen 80;# default_server;

    root <sitepath />/<sitename />/sitefiles/public;

    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;

    index index.php index.html index.htm;

    server_name <sitename />;#<- server_domain_or_$

    error_log <sitepath />/<sitename />/platform_dir/logs/nginxerror.log;
    access_log <sitepath />/<sitename />/platform_dir/logs/nginxaccess.log;


    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 60;
        include fastcgi_params;
    }
}

I have loaded the file into $block and wish to substitute the tag <sitepath /> with the value of the variable $wwwrooot. While testing with

echo "$block" | sed -r "s/<sitepath \/>+/$wwwroot/g"

I get the error

sed: -e expression #1, char 19: unknown option to `s'

I'm gonna go out on a limb and assume $wwwrooot contains slashes.
That's gonna be a problem for your sed command, as you've chosen slashes as regex separators. Using a different character (one that's unlikely to be part of your $wwwrooot , eg # ) should fix that:

echo "$block" | sed -r "s#<sitepath />#$wwwroot#g"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM