shell script to comment out or uncomment a line - testing
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
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.
bash scripts text-processing grep sed
add a comment |Â
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.
bash scripts text-processing grep sed
add a comment |Â
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.
bash scripts text-processing grep sed
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
bash scripts text-processing grep sed
edited Feb 24 at 15:11
![](https://i.stack.imgur.com/m8DYH.jpg?s=32&g=1)
![](https://i.stack.imgur.com/m8DYH.jpg?s=32&g=1)
Byte Commander
59.7k26159268
59.7k26159268
asked Feb 24 at 15:04
Kylar Stern
111
111
add a comment |Â
add a comment |Â
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).
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
add a comment |Â
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).
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
add a comment |Â
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).
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
add a comment |Â
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).
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).
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password