Bash: Dynamic replacing with perl

This question already has an answer here:

  • How do I pass $SHELL variable into a perl search and replace 2 answers
  • How to concatenate string variables in Bash 27 answers

  • Remove " + " (spaces and plus). Bash does automatic concatenation for adjacent strings.

    echo 'hi!'t"here"  # hi!there
    

    Generating Perl code in this way is only safe because the output of id -g -n $USER won't contain , $ , @ or / .


    You were going for

    GROUPNAME="$(id -g -n $USER)"
    perl -i -pe's/(PLACEHOLDER)/'"$GROUPNAME"'/g' filepath/file
    

    But there's no reason to generate Perl code. That is very error prone. Instead, use one of the following:

    export GROUPNAME="$(id -g -n $USER)"
    perl -i -pe's/(PLACEHOLDER)/$ENV{GROUPNAME}/g' filepath/file
    

    or

    GROUPNAME="$(id -g -n $USER)" perl -i -pe's/(PLACEHOLDER)/$ENV{GROUPNAME}/g' filepath/file
    
    链接地址: http://www.djcxy.com/p/29130.html

    上一篇: 从用户输入创建一个文件名

    下一篇: Bash:用perl动态替换