Bash script to fill in a template












2















I have looked for this elsewhere but not able to find something that matches my requirements.



The template below is the one I need to use :



dn: uid=$1,ou=home,dc=chan,dc=com

objectClass: organizationalPerson

objectClass: person

objectClass: inetOrgPerson

objectClass: top

cn: $2

sn: $3

userid: $1

userPassword:


I need to script in such a way that populates the following fields $1,$2,$3,$4 with the input that I give it and then outputs to a new file thus allowing to run it using the blank template.










share|improve this question

























  • See also this question and its answers: stackoverflow.com/questions/2914220/…

    – Tomáš Pospíšek
    Feb 18 '17 at 9:51
















2















I have looked for this elsewhere but not able to find something that matches my requirements.



The template below is the one I need to use :



dn: uid=$1,ou=home,dc=chan,dc=com

objectClass: organizationalPerson

objectClass: person

objectClass: inetOrgPerson

objectClass: top

cn: $2

sn: $3

userid: $1

userPassword:


I need to script in such a way that populates the following fields $1,$2,$3,$4 with the input that I give it and then outputs to a new file thus allowing to run it using the blank template.










share|improve this question

























  • See also this question and its answers: stackoverflow.com/questions/2914220/…

    – Tomáš Pospíšek
    Feb 18 '17 at 9:51














2












2








2


1






I have looked for this elsewhere but not able to find something that matches my requirements.



The template below is the one I need to use :



dn: uid=$1,ou=home,dc=chan,dc=com

objectClass: organizationalPerson

objectClass: person

objectClass: inetOrgPerson

objectClass: top

cn: $2

sn: $3

userid: $1

userPassword:


I need to script in such a way that populates the following fields $1,$2,$3,$4 with the input that I give it and then outputs to a new file thus allowing to run it using the blank template.










share|improve this question
















I have looked for this elsewhere but not able to find something that matches my requirements.



The template below is the one I need to use :



dn: uid=$1,ou=home,dc=chan,dc=com

objectClass: organizationalPerson

objectClass: person

objectClass: inetOrgPerson

objectClass: top

cn: $2

sn: $3

userid: $1

userPassword:


I need to script in such a way that populates the following fields $1,$2,$3,$4 with the input that I give it and then outputs to a new file thus allowing to run it using the blank template.







bash shell-script variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 16 '15 at 17:30









mkc

5,83342742




5,83342742










asked Oct 16 '15 at 16:54









user1380599user1380599

8729




8729













  • See also this question and its answers: stackoverflow.com/questions/2914220/…

    – Tomáš Pospíšek
    Feb 18 '17 at 9:51



















  • See also this question and its answers: stackoverflow.com/questions/2914220/…

    – Tomáš Pospíšek
    Feb 18 '17 at 9:51

















See also this question and its answers: stackoverflow.com/questions/2914220/…

– Tomáš Pospíšek
Feb 18 '17 at 9:51





See also this question and its answers: stackoverflow.com/questions/2914220/…

– Tomáš Pospíšek
Feb 18 '17 at 9:51










3 Answers
3






active

oldest

votes


















9














I suspect you are looking for something quite simple like a file my_script that contains:



cat <<<XXX
dn: uid=$1,ou=home,dc=chan,dc=com

objectClass: organizationalPerson

objectClass: person

objectClass: inetOrgPerson

objectClass: top

cn: $2

sn: $3

userid: $1

userPassword:
XXX


To use the above, simply run:



sh my_script fred 100 200


which will output to stdout (and can be redirected to a file).






share|improve this answer































    3














    Does there actually need to be a template file?



    #!/bin/bash

    echo "dn: uid=$1,ou=home,dc=chan,dc=com" > $4
    echo "objectClass: organizationalPerson" >> $4
    echo "objectClass: person" >> $4
    echo "objectClass: inetOrgPerson" >> $4
    echo "objectClass: top" >> $4
    echo "cn: $2" >> $4
    echo "sn: $3" >> $4
    echo "userid: $1" >> $4
    echo "userPassword:" >> $4


    You would run ./SCRIPTNAME Value1 Value2 Value3 OutputFileName



    otherwise



    You script would be:



    #!/bin/bash

    var1=$1
    var2=$2
    var3=$3
    file=$4

    cat TemplateFile > $file

    sed -i "s/1/$var1/g" $file
    sed -i "s/2/$var2/g" $file
    sed -i "s/3/$var3/g" $file


    and your template file would be



    dn: uid=1,ou=home,dc=chan,dc=com 
    objectClass: organizationalPerson
    objectClass: person
    objectClass: inetOrgPerson
    objectClass: top
    cn: 2
    sn: 3
    userid: 1
    userPassword:





    share|improve this answer


























    • Ah I did this when yours was one string, hold on I'll fix it.

      – Kip K
      Oct 16 '15 at 17:22











    • Don't use repeated redirections. Rather than slapping ">> $4" on the end of each line, just do exec > $4 at the start.

      – William Pursell
      Jan 5 at 19:54



















    0














    I solved similar problem with



    elegant and short solution in one line with perl



    Use perl to replace variables with their values:



    export world=World beautiful=wonderful
    echo 'I love you, $world! You are $beautiful.' >my_template.txt
    perl -pe 's|$([A-Za-z_]+)|$ENV{$1}|g' my_template.txt


    The output: I love you, World! You are wonderful.



    my_template.txt can contain variables prefixed with $.






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f236688%2fbash-script-to-fill-in-a-template%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9














      I suspect you are looking for something quite simple like a file my_script that contains:



      cat <<<XXX
      dn: uid=$1,ou=home,dc=chan,dc=com

      objectClass: organizationalPerson

      objectClass: person

      objectClass: inetOrgPerson

      objectClass: top

      cn: $2

      sn: $3

      userid: $1

      userPassword:
      XXX


      To use the above, simply run:



      sh my_script fred 100 200


      which will output to stdout (and can be redirected to a file).






      share|improve this answer




























        9














        I suspect you are looking for something quite simple like a file my_script that contains:



        cat <<<XXX
        dn: uid=$1,ou=home,dc=chan,dc=com

        objectClass: organizationalPerson

        objectClass: person

        objectClass: inetOrgPerson

        objectClass: top

        cn: $2

        sn: $3

        userid: $1

        userPassword:
        XXX


        To use the above, simply run:



        sh my_script fred 100 200


        which will output to stdout (and can be redirected to a file).






        share|improve this answer


























          9












          9








          9







          I suspect you are looking for something quite simple like a file my_script that contains:



          cat <<<XXX
          dn: uid=$1,ou=home,dc=chan,dc=com

          objectClass: organizationalPerson

          objectClass: person

          objectClass: inetOrgPerson

          objectClass: top

          cn: $2

          sn: $3

          userid: $1

          userPassword:
          XXX


          To use the above, simply run:



          sh my_script fred 100 200


          which will output to stdout (and can be redirected to a file).






          share|improve this answer













          I suspect you are looking for something quite simple like a file my_script that contains:



          cat <<<XXX
          dn: uid=$1,ou=home,dc=chan,dc=com

          objectClass: organizationalPerson

          objectClass: person

          objectClass: inetOrgPerson

          objectClass: top

          cn: $2

          sn: $3

          userid: $1

          userPassword:
          XXX


          To use the above, simply run:



          sh my_script fred 100 200


          which will output to stdout (and can be redirected to a file).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 16 '15 at 17:10









          BrianBrian

          1,0321411




          1,0321411

























              3














              Does there actually need to be a template file?



              #!/bin/bash

              echo "dn: uid=$1,ou=home,dc=chan,dc=com" > $4
              echo "objectClass: organizationalPerson" >> $4
              echo "objectClass: person" >> $4
              echo "objectClass: inetOrgPerson" >> $4
              echo "objectClass: top" >> $4
              echo "cn: $2" >> $4
              echo "sn: $3" >> $4
              echo "userid: $1" >> $4
              echo "userPassword:" >> $4


              You would run ./SCRIPTNAME Value1 Value2 Value3 OutputFileName



              otherwise



              You script would be:



              #!/bin/bash

              var1=$1
              var2=$2
              var3=$3
              file=$4

              cat TemplateFile > $file

              sed -i "s/1/$var1/g" $file
              sed -i "s/2/$var2/g" $file
              sed -i "s/3/$var3/g" $file


              and your template file would be



              dn: uid=1,ou=home,dc=chan,dc=com 
              objectClass: organizationalPerson
              objectClass: person
              objectClass: inetOrgPerson
              objectClass: top
              cn: 2
              sn: 3
              userid: 1
              userPassword:





              share|improve this answer


























              • Ah I did this when yours was one string, hold on I'll fix it.

                – Kip K
                Oct 16 '15 at 17:22











              • Don't use repeated redirections. Rather than slapping ">> $4" on the end of each line, just do exec > $4 at the start.

                – William Pursell
                Jan 5 at 19:54
















              3














              Does there actually need to be a template file?



              #!/bin/bash

              echo "dn: uid=$1,ou=home,dc=chan,dc=com" > $4
              echo "objectClass: organizationalPerson" >> $4
              echo "objectClass: person" >> $4
              echo "objectClass: inetOrgPerson" >> $4
              echo "objectClass: top" >> $4
              echo "cn: $2" >> $4
              echo "sn: $3" >> $4
              echo "userid: $1" >> $4
              echo "userPassword:" >> $4


              You would run ./SCRIPTNAME Value1 Value2 Value3 OutputFileName



              otherwise



              You script would be:



              #!/bin/bash

              var1=$1
              var2=$2
              var3=$3
              file=$4

              cat TemplateFile > $file

              sed -i "s/1/$var1/g" $file
              sed -i "s/2/$var2/g" $file
              sed -i "s/3/$var3/g" $file


              and your template file would be



              dn: uid=1,ou=home,dc=chan,dc=com 
              objectClass: organizationalPerson
              objectClass: person
              objectClass: inetOrgPerson
              objectClass: top
              cn: 2
              sn: 3
              userid: 1
              userPassword:





              share|improve this answer


























              • Ah I did this when yours was one string, hold on I'll fix it.

                – Kip K
                Oct 16 '15 at 17:22











              • Don't use repeated redirections. Rather than slapping ">> $4" on the end of each line, just do exec > $4 at the start.

                – William Pursell
                Jan 5 at 19:54














              3












              3








              3







              Does there actually need to be a template file?



              #!/bin/bash

              echo "dn: uid=$1,ou=home,dc=chan,dc=com" > $4
              echo "objectClass: organizationalPerson" >> $4
              echo "objectClass: person" >> $4
              echo "objectClass: inetOrgPerson" >> $4
              echo "objectClass: top" >> $4
              echo "cn: $2" >> $4
              echo "sn: $3" >> $4
              echo "userid: $1" >> $4
              echo "userPassword:" >> $4


              You would run ./SCRIPTNAME Value1 Value2 Value3 OutputFileName



              otherwise



              You script would be:



              #!/bin/bash

              var1=$1
              var2=$2
              var3=$3
              file=$4

              cat TemplateFile > $file

              sed -i "s/1/$var1/g" $file
              sed -i "s/2/$var2/g" $file
              sed -i "s/3/$var3/g" $file


              and your template file would be



              dn: uid=1,ou=home,dc=chan,dc=com 
              objectClass: organizationalPerson
              objectClass: person
              objectClass: inetOrgPerson
              objectClass: top
              cn: 2
              sn: 3
              userid: 1
              userPassword:





              share|improve this answer















              Does there actually need to be a template file?



              #!/bin/bash

              echo "dn: uid=$1,ou=home,dc=chan,dc=com" > $4
              echo "objectClass: organizationalPerson" >> $4
              echo "objectClass: person" >> $4
              echo "objectClass: inetOrgPerson" >> $4
              echo "objectClass: top" >> $4
              echo "cn: $2" >> $4
              echo "sn: $3" >> $4
              echo "userid: $1" >> $4
              echo "userPassword:" >> $4


              You would run ./SCRIPTNAME Value1 Value2 Value3 OutputFileName



              otherwise



              You script would be:



              #!/bin/bash

              var1=$1
              var2=$2
              var3=$3
              file=$4

              cat TemplateFile > $file

              sed -i "s/1/$var1/g" $file
              sed -i "s/2/$var2/g" $file
              sed -i "s/3/$var3/g" $file


              and your template file would be



              dn: uid=1,ou=home,dc=chan,dc=com 
              objectClass: organizationalPerson
              objectClass: person
              objectClass: inetOrgPerson
              objectClass: top
              cn: 2
              sn: 3
              userid: 1
              userPassword:






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 16 '15 at 17:27

























              answered Oct 16 '15 at 17:21









              Kip KKip K

              974




              974













              • Ah I did this when yours was one string, hold on I'll fix it.

                – Kip K
                Oct 16 '15 at 17:22











              • Don't use repeated redirections. Rather than slapping ">> $4" on the end of each line, just do exec > $4 at the start.

                – William Pursell
                Jan 5 at 19:54



















              • Ah I did this when yours was one string, hold on I'll fix it.

                – Kip K
                Oct 16 '15 at 17:22











              • Don't use repeated redirections. Rather than slapping ">> $4" on the end of each line, just do exec > $4 at the start.

                – William Pursell
                Jan 5 at 19:54

















              Ah I did this when yours was one string, hold on I'll fix it.

              – Kip K
              Oct 16 '15 at 17:22





              Ah I did this when yours was one string, hold on I'll fix it.

              – Kip K
              Oct 16 '15 at 17:22













              Don't use repeated redirections. Rather than slapping ">> $4" on the end of each line, just do exec > $4 at the start.

              – William Pursell
              Jan 5 at 19:54





              Don't use repeated redirections. Rather than slapping ">> $4" on the end of each line, just do exec > $4 at the start.

              – William Pursell
              Jan 5 at 19:54











              0














              I solved similar problem with



              elegant and short solution in one line with perl



              Use perl to replace variables with their values:



              export world=World beautiful=wonderful
              echo 'I love you, $world! You are $beautiful.' >my_template.txt
              perl -pe 's|$([A-Za-z_]+)|$ENV{$1}|g' my_template.txt


              The output: I love you, World! You are wonderful.



              my_template.txt can contain variables prefixed with $.






              share|improve this answer




























                0














                I solved similar problem with



                elegant and short solution in one line with perl



                Use perl to replace variables with their values:



                export world=World beautiful=wonderful
                echo 'I love you, $world! You are $beautiful.' >my_template.txt
                perl -pe 's|$([A-Za-z_]+)|$ENV{$1}|g' my_template.txt


                The output: I love you, World! You are wonderful.



                my_template.txt can contain variables prefixed with $.






                share|improve this answer


























                  0












                  0








                  0







                  I solved similar problem with



                  elegant and short solution in one line with perl



                  Use perl to replace variables with their values:



                  export world=World beautiful=wonderful
                  echo 'I love you, $world! You are $beautiful.' >my_template.txt
                  perl -pe 's|$([A-Za-z_]+)|$ENV{$1}|g' my_template.txt


                  The output: I love you, World! You are wonderful.



                  my_template.txt can contain variables prefixed with $.






                  share|improve this answer













                  I solved similar problem with



                  elegant and short solution in one line with perl



                  Use perl to replace variables with their values:



                  export world=World beautiful=wonderful
                  echo 'I love you, $world! You are $beautiful.' >my_template.txt
                  perl -pe 's|$([A-Za-z_]+)|$ENV{$1}|g' my_template.txt


                  The output: I love you, World! You are wonderful.



                  my_template.txt can contain variables prefixed with $.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 5 at 19:17









                  kybkyb

                  144111




                  144111






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f236688%2fbash-script-to-fill-in-a-template%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Morgemoulin

                      Scott Moir

                      Souastre