shell script to comment out or uncomment a line - testing

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
2
down vote

favorite












Trying to remove a # sign from a single line in a file. The line is currently



#/usr/bin/tvservice -o


if [[ '$grep tvservice /etc/rc.local' =~ ^# ]] ;
then sed -i '/^#.* tvservice /s/^#//' /etc/rc.local ;
else echo 1 fi


Trying to test if this command will work before I add it to a script. When I run it I get a > symbol and have to stop execution with Ctrl+C



Am I not passing a string to grep correctly or what? I'm fairly new to this.










share|improve this question



























    up vote
    2
    down vote

    favorite












    Trying to remove a # sign from a single line in a file. The line is currently



    #/usr/bin/tvservice -o


    if [[ '$grep tvservice /etc/rc.local' =~ ^# ]] ;
    then sed -i '/^#.* tvservice /s/^#//' /etc/rc.local ;
    else echo 1 fi


    Trying to test if this command will work before I add it to a script. When I run it I get a > symbol and have to stop execution with Ctrl+C



    Am I not passing a string to grep correctly or what? I'm fairly new to this.










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Trying to remove a # sign from a single line in a file. The line is currently



      #/usr/bin/tvservice -o


      if [[ '$grep tvservice /etc/rc.local' =~ ^# ]] ;
      then sed -i '/^#.* tvservice /s/^#//' /etc/rc.local ;
      else echo 1 fi


      Trying to test if this command will work before I add it to a script. When I run it I get a > symbol and have to stop execution with Ctrl+C



      Am I not passing a string to grep correctly or what? I'm fairly new to this.










      share|improve this question















      Trying to remove a # sign from a single line in a file. The line is currently



      #/usr/bin/tvservice -o


      if [[ '$grep tvservice /etc/rc.local' =~ ^# ]] ;
      then sed -i '/^#.* tvservice /s/^#//' /etc/rc.local ;
      else echo 1 fi


      Trying to test if this command will work before I add it to a script. When I run it I get a > symbol and have to stop execution with Ctrl+C



      Am I not passing a string to grep correctly or what? I'm fairly new to this.







      bash scripts text-processing grep sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 24 at 15:11









      Byte Commander

      59.7k26159268




      59.7k26159268










      asked Feb 24 at 15:04









      Kylar Stern

      111




      111




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          I suggest you test without sed's -i flag first - or use a local copy of the rc.local file.



          You don't need to test for the string's presence, but if you do, then the correct way to substitute grep's output would be "$(grep tvservice /etc/rc.local)" - the way you have written it tests the literal string $grep tvservice /etc/rc.local



          However, a better way IMHO would be to grep for the whole pattern and use grep's exit status directly, avoiding the shell regex test altogether:



          if grep -q '^#.*tvservice' /etc/rc.local; then 
          sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local
          else
          echo 1
          fi


          (I removed the spaces around the pattern, since your sample text doesn't have them).






          share|improve this answer






















          • Meh, just a minute faster than me...
            – Byte Commander
            Feb 24 at 15:25










          • I ran the command in this form and am getting 2 as output and then nothing. It doesn't really exit the script properly, any clues ? if grep -q '^#/*tvservice' /etc/rc.local; then echo 1 sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local else echo 2 sed -e '/tvservice/ s/^#*/#/' fi
            – Kylar Stern
            Feb 24 at 19:29











          • This is the state of the file currently code #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #Disable HDMI #/usr/bin/tvservice -o exit 0 code
            – Kylar Stern
            Feb 24 at 19:33











          • I saw for second sed I didn't give it a file to edit. Fixed it the command runs and exits out but I get the "2" as output still which means that it skips the "then" where it's supposed to remove the # from that line.
            – Kylar Stern
            Feb 24 at 19:46










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "89"
          ;
          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',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1009340%2fshell-script-to-comment-out-or-uncomment-a-line-testing%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          I suggest you test without sed's -i flag first - or use a local copy of the rc.local file.



          You don't need to test for the string's presence, but if you do, then the correct way to substitute grep's output would be "$(grep tvservice /etc/rc.local)" - the way you have written it tests the literal string $grep tvservice /etc/rc.local



          However, a better way IMHO would be to grep for the whole pattern and use grep's exit status directly, avoiding the shell regex test altogether:



          if grep -q '^#.*tvservice' /etc/rc.local; then 
          sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local
          else
          echo 1
          fi


          (I removed the spaces around the pattern, since your sample text doesn't have them).






          share|improve this answer






















          • Meh, just a minute faster than me...
            – Byte Commander
            Feb 24 at 15:25










          • I ran the command in this form and am getting 2 as output and then nothing. It doesn't really exit the script properly, any clues ? if grep -q '^#/*tvservice' /etc/rc.local; then echo 1 sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local else echo 2 sed -e '/tvservice/ s/^#*/#/' fi
            – Kylar Stern
            Feb 24 at 19:29











          • This is the state of the file currently code #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #Disable HDMI #/usr/bin/tvservice -o exit 0 code
            – Kylar Stern
            Feb 24 at 19:33











          • I saw for second sed I didn't give it a file to edit. Fixed it the command runs and exits out but I get the "2" as output still which means that it skips the "then" where it's supposed to remove the # from that line.
            – Kylar Stern
            Feb 24 at 19:46














          up vote
          3
          down vote













          I suggest you test without sed's -i flag first - or use a local copy of the rc.local file.



          You don't need to test for the string's presence, but if you do, then the correct way to substitute grep's output would be "$(grep tvservice /etc/rc.local)" - the way you have written it tests the literal string $grep tvservice /etc/rc.local



          However, a better way IMHO would be to grep for the whole pattern and use grep's exit status directly, avoiding the shell regex test altogether:



          if grep -q '^#.*tvservice' /etc/rc.local; then 
          sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local
          else
          echo 1
          fi


          (I removed the spaces around the pattern, since your sample text doesn't have them).






          share|improve this answer






















          • Meh, just a minute faster than me...
            – Byte Commander
            Feb 24 at 15:25










          • I ran the command in this form and am getting 2 as output and then nothing. It doesn't really exit the script properly, any clues ? if grep -q '^#/*tvservice' /etc/rc.local; then echo 1 sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local else echo 2 sed -e '/tvservice/ s/^#*/#/' fi
            – Kylar Stern
            Feb 24 at 19:29











          • This is the state of the file currently code #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #Disable HDMI #/usr/bin/tvservice -o exit 0 code
            – Kylar Stern
            Feb 24 at 19:33











          • I saw for second sed I didn't give it a file to edit. Fixed it the command runs and exits out but I get the "2" as output still which means that it skips the "then" where it's supposed to remove the # from that line.
            – Kylar Stern
            Feb 24 at 19:46












          up vote
          3
          down vote










          up vote
          3
          down vote









          I suggest you test without sed's -i flag first - or use a local copy of the rc.local file.



          You don't need to test for the string's presence, but if you do, then the correct way to substitute grep's output would be "$(grep tvservice /etc/rc.local)" - the way you have written it tests the literal string $grep tvservice /etc/rc.local



          However, a better way IMHO would be to grep for the whole pattern and use grep's exit status directly, avoiding the shell regex test altogether:



          if grep -q '^#.*tvservice' /etc/rc.local; then 
          sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local
          else
          echo 1
          fi


          (I removed the spaces around the pattern, since your sample text doesn't have them).






          share|improve this answer














          I suggest you test without sed's -i flag first - or use a local copy of the rc.local file.



          You don't need to test for the string's presence, but if you do, then the correct way to substitute grep's output would be "$(grep tvservice /etc/rc.local)" - the way you have written it tests the literal string $grep tvservice /etc/rc.local



          However, a better way IMHO would be to grep for the whole pattern and use grep's exit status directly, avoiding the shell regex test altogether:



          if grep -q '^#.*tvservice' /etc/rc.local; then 
          sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local
          else
          echo 1
          fi


          (I removed the spaces around the pattern, since your sample text doesn't have them).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 24 at 15:32

























          answered Feb 24 at 15:21









          steeldriver

          63.3k1198167




          63.3k1198167











          • Meh, just a minute faster than me...
            – Byte Commander
            Feb 24 at 15:25










          • I ran the command in this form and am getting 2 as output and then nothing. It doesn't really exit the script properly, any clues ? if grep -q '^#/*tvservice' /etc/rc.local; then echo 1 sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local else echo 2 sed -e '/tvservice/ s/^#*/#/' fi
            – Kylar Stern
            Feb 24 at 19:29











          • This is the state of the file currently code #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #Disable HDMI #/usr/bin/tvservice -o exit 0 code
            – Kylar Stern
            Feb 24 at 19:33











          • I saw for second sed I didn't give it a file to edit. Fixed it the command runs and exits out but I get the "2" as output still which means that it skips the "then" where it's supposed to remove the # from that line.
            – Kylar Stern
            Feb 24 at 19:46
















          • Meh, just a minute faster than me...
            – Byte Commander
            Feb 24 at 15:25










          • I ran the command in this form and am getting 2 as output and then nothing. It doesn't really exit the script properly, any clues ? if grep -q '^#/*tvservice' /etc/rc.local; then echo 1 sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local else echo 2 sed -e '/tvservice/ s/^#*/#/' fi
            – Kylar Stern
            Feb 24 at 19:29











          • This is the state of the file currently code #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #Disable HDMI #/usr/bin/tvservice -o exit 0 code
            – Kylar Stern
            Feb 24 at 19:33











          • I saw for second sed I didn't give it a file to edit. Fixed it the command runs and exits out but I get the "2" as output still which means that it skips the "then" where it's supposed to remove the # from that line.
            – Kylar Stern
            Feb 24 at 19:46















          Meh, just a minute faster than me...
          – Byte Commander
          Feb 24 at 15:25




          Meh, just a minute faster than me...
          – Byte Commander
          Feb 24 at 15:25












          I ran the command in this form and am getting 2 as output and then nothing. It doesn't really exit the script properly, any clues ? if grep -q '^#/*tvservice' /etc/rc.local; then echo 1 sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local else echo 2 sed -e '/tvservice/ s/^#*/#/' fi
          – Kylar Stern
          Feb 24 at 19:29





          I ran the command in this form and am getting 2 as output and then nothing. It doesn't really exit the script properly, any clues ? if grep -q '^#/*tvservice' /etc/rc.local; then echo 1 sed -i '/^#.*tvservice/ s/^#//' /etc/rc.local else echo 2 sed -e '/tvservice/ s/^#*/#/' fi
          – Kylar Stern
          Feb 24 at 19:29













          This is the state of the file currently code #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #Disable HDMI #/usr/bin/tvservice -o exit 0 code
          – Kylar Stern
          Feb 24 at 19:33





          This is the state of the file currently code #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #Disable HDMI #/usr/bin/tvservice -o exit 0 code
          – Kylar Stern
          Feb 24 at 19:33













          I saw for second sed I didn't give it a file to edit. Fixed it the command runs and exits out but I get the "2" as output still which means that it skips the "then" where it's supposed to remove the # from that line.
          – Kylar Stern
          Feb 24 at 19:46




          I saw for second sed I didn't give it a file to edit. Fixed it the command runs and exits out but I get the "2" as output still which means that it skips the "then" where it's supposed to remove the # from that line.
          – Kylar Stern
          Feb 24 at 19:46

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1009340%2fshell-script-to-comment-out-or-uncomment-a-line-testing%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Which professions warranted travel in Medieval times?

          How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

          Trouble downloading packages list due to a “Hash sum mismatch” error