How can i uncomment a line with echo or printf
A noob question, but how can i uncomment a line via the command line (w/o a text editor), i'm just kinda curious how to do it that way. Think of echo/printf but don't know that well how to use them.
Edit: If you didn't understood, I meant to uncomment a line like # %wheel ALL=(ALL:ALL) ALL and any shell command, not only echo/printf
Edit 2: Think I have the answer now :D
Edited 19 Aug 2022. We only hold this version.
About this post
- Posted
- Archived
- Edited
- Score
- 4 ยท 56% up
- Kind
- Text
- ID
- wsba20
26 comments
You mean something like this
echo [magic string] > fileand it modifies the file to uncomment a line? That won't work, you can only append data or overwite the whole file using echo and > or >> operators.To do more advanced editing like uncommenting you can use
sed.I don't mean those commands only btw.. any command like
cut,sedor such will do I'm just talking about the lines like# %wheel ALL=(ALL:ALL) ALLsed -i '/^# %wheel/s/# //' fileuncomments all lines in a file starting with# %wheel/Remove
#For the general case, use
sed:For the
sudoersfile, please usesudo visudobefore you lock yourself out like I did.Thx, that will work!
Copy that ๐
I know this is not on topic or answering your question either, but that looks like your sudoers file and I'd recommend only editing it with
visudoand nothing else, let alone a script.Yeah, have given the wrong example, I'm sorry
echo and printf are not what you should use. you could use sed to do a search and replace for every instance of # with an empty string.
Are you saying that you have a file, and you want to modify the file so that every line that has an echo or printf is uncommented?
maybe you're the noob
Perhaps, but I'd still like to understand the question. The only other interpretation I can think of is to do some fancy shell scripting to uncomment a line using echo or printf, but I can't think of an obvious way to do that since I'd prefer using sed or something like that.
๐๐๐ yo relax
So the easiest way would be to just add a new line without the comment. So
echo %wheel ALL=(ALL:ALL) ALL >> file.confyh i know can also do that but was just curious for another way
You can uncomment the respective line with
sed. The issue with that approach is that the commented line needs to exist for the command to have an effect and thatsedwill "fail" silently. The sed expressions/pattern/replacement/will replace 'pattern' with 'expression' if and only if it encounters it. If it doesn't encounter the pattern it will still exit successfully, having found nothing to do.Therefore simply appending the line you want to have is the safer and quicker solution.
"echo" and "printf" are shell commands. they don't comment out stuff. You probably added those in front of the commands you wanted removed because you didn't want them executed. And that works, but it is not what those commands are for. They print strings out to the terminal the script is executed in. If you write "echo test", the script will print out "test".
To actually comment out a line from a shell script, prefix the line with a hash character ("#").
Deleted before we saved it.
Use sed and/or awk
I think you would want to use 'sed' to accomplish this
In my opinion instead editing the file
/etc/sudoersthe best way to do it is something like that:P.S. but you have to check if the next line exists into the file
/etc/sudoers