Modify /etc/sudoers with sed - sed

I'm trying to write a sed program to append Defaults:user !requiretty after the line Defaults requiretty in /etc/sudoers. I tried the following command:
sudo sed -i '/Defaults requiretty/a Defaults:user !requiretty' /etc/sudoers
This is working properly, but only if there are 4 spaces between 'Defaults' and 'requiretty'. I want to modify it in order to work with any number of spaces, so I tried the following:
sudo sed -i '/Defaults\s+requiretty/a Defaults:user !requiretty' /etc/sudoers
I checked the pattern on regexr and it was okay, but still the command does not insert the required line. Why not?

try this;
sed '/Defaults.\s\s.requiretty/a Defaults:user !requiretty' /etc/sudoers

Working from Mustafa's answer, here's a way to do the same thing with some safety checks added
SUDOER_TMP=$(mktemp)
cat /etc/sudoers > $SUDOER_TMP
sed -i -e 's/PATTERN/OUTPUT/' $SUDOER_TMP
visudo -c -f $SUDOER_TMP && \ # this will fail if the syntax is incorrect
cat $SUDOER_TMP > /etc/sudoers
rm $SUDOER_TMP

Related

How do I add the sed command inside a bash script?

I want to add below line to build.sh file to a line number 26
sed -i 's/-DskipTests //' dev/make-distribution.sh
I tried with this command
sed "26 a sed -i 's/-DskipTests //' dev/make-distribution.sh" build.sh
But this is giving error
sed: 1: "26 a sed -i 's/-DskipTe ...": command a expects \ followed by text`
Try putting it in as if it were multiline add as a workaround. Does this work for you?
sed "26 a\\sed -i 's/-DskipTests //' dev/make-distribution.sh" build.sh
You need to quote your quote since you are compounding quote-types.
c.f. the manual, though - it ought to work as it is.

Sed remove matching lines script

I'm requesting help with a very simple script...
#!/usr/bin/sed -f
sed '/11,yahoo/d'
sed '/2506,stackover flow/d'
sed '/2536,reddit/d'
Just need it to remove three matches that account for 18408 in my file, data.csv
% sed -f remove.sed < data.csv
sed: 3: remove.sed: unterminated substitute pattern
Doing these same lines individually is no problem at all, so what am I doing wrong with this?
Using freeBSD 10.1 and its implementation of sed, if that matters.
This, being a sed script, should not have "sed" at each line.
Either change it to:
#!/usr/bin/sed -f
/11,yahoo/d
/2506,stackover flow/d
/2536,reddit/d
Or to
#!/bin/sh
sed -e /11,yahoo/d \
-e /2506,stackover flow/d \
-e /2536,reddit/d

LINUX- What is wrong with my code in sed?

I want to replace
ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"
PIDFile=/home/<USER>/.vnc/%H %i.pid
with
ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"
PIDFile=/root/.vnc/%H %i.pid
I have tried:
sed -i 's$ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i" \n PIDFile=/home/<USER>/.vnc/%H %i.pid$ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i" \n PIDFile=/root/.vnc/%H %i.pid$g' file
I used $ instead of / for special characters. But that doesn't work, nothing is getting edited.
Please tell me what I am missing. Thx.
Got the Soution.Thx to the answerer :) :
sed -i 's$ExecStart=/sbin/runuser -l <USER> -c "/usr/bin/vncserver %i"$ExecStart=/sbin/runuser -l root -c "/usr/bin/vncserver %i"$g' uu && sed -i 's$PIDFile=/home/<USER>/.vnc/%H %i.pid$PIDFile=/root/.vnc/%H %i.pid$g' uu
sed primarily works on one line at a time, so you'll need to define two separate substitute operations, one for each line:
sed -e 's/-l <USER> -c/-l root -c/' \
-e 's%home/<USER>%root%' \
file
Note that the character after s is the delimiter for the regular expression. Rather than messing with backslashes, I changed to % in the second to avoid the collision with slashes.
If you're really worried, you can make your matches match more, but what I showed is unlikely to fail you. It is odd that you want /root to replace /home/<USER>. You might do better with a marker like <HOME> which can be set correctly.

sed: find and replace string but not super-string

I have a file env which looks like
....
LEGACY_DATABASE_SERVER=10.0.0.1
SERVER=10.1.1.1
and here is my sed command:
sed -e "s/SERVER=.*/SERVER=$INSTANCE_IP/g;n" $ENV_FILE > $ENV_FILE.tmp && mv $ENV_FILE.tmp $ENV_FILE
the problem is that sed is also replacing LEGACY_DATABASE_SERVER which is not what I want. I only want SERVER replaced.
(LEGACY_DATABASE_SERVER is a super string of SERVER and I only want to replace SERVER)
What am I missing?
Presumably, you want to make sure that sed knows "SERVER" is at the beginning of the line:
sed -e "s/^SERVER=.*/SERVER=$INSTANCE_IP/g;n" $ENV_FILE > $ENV_FILE.tmp && mv $ENV_FILE.tmp $ENV_FILE

sed: can't read /home/me/weather: No such file or directory

I have the following extract from a script that fetches weather information from accuweather:
wget -O ./weather_raw $address
if [[ -s ./weather_raw ]]; then
egrep 'Currently|Forecast<\/title>|_31x31.gif' ./weather_raw > ./weather
sed -i '/AccuWeather\|Currently in/d' ./weather
sed -i -e 's/^[ \t]*//g' -e 's/<title>\|<\/title>\|<description>\|<\/description>//g' ./weather
sed -i -e 's/<img src="/\n/g' ./weather
sed -i '/^$/d' ./weather
sed -i -e 's/_31x31.*$//g' -e 's/^.*\/icons\///g' ./weather
sed -i -e '1s/.$//' -e '3s/.$//' -e '6s/.$//' ./weather
for (( i=2; i<=8; i+=3 ))
do
im=$(sed -n ${i}p ./weather)
sed -i $i"s/^.*$/$(test_image $im)/" ./weather
done
fi
The command that triggers the code above is in a conkyrc file and its ~/.conkyblue/accu_weather/rss/acc_rss. When I run the conkyrc script from the prompt, I get an error
sed: can't read /home/me/weather: No such file or directory
And indeed when I check, the "weather" file is not created. However if run the command ~/.conkyblue/accu_weather/rss/acc_rss directly from the prompt, it works as expected and create and puts content into the /home/me/weather file.
I don't know anything about the sed command although I'm trying to learn it as a result of this bother.
What could be the problem with the code. I don't think its a permission issue since the folder its writing into is my home folder and I of-course own it.
Thanks
It should have been created by egrep.
When you run your script, the weather directory will be created in the pwd of the script process.
Check and see why egrep does not create the file, or in which directory it created it.