LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-28-2014, 03:23 PM   #1
steeladept
LQ Newbie
 
Registered: Oct 2014
Posts: 24

Rep: Reputation: Disabled
sed issue editing /etc/init.d/mongod file


I am trying to create the correct syntax for sed to fix a common problem via scripting. The problem is reproducible by installing MongoDB on CentOS 6.5 and results in a warning:
Quote:
/usr/bin/dirname: extra operand '2>&1.pid'
I found, here, that there is a solution. Just edit the /etc/init.d/mongod file to remove that from line 66.

Code:
<line 66>

 daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
It works consistently, and now I am trying to script it using sed. I am using this line to try and do so:

Code:
sed `s/S \>\/dev\/null 2\>\&1/S/` /etc/init.d/mongod
I think I have this all escaped properly, but no matter what I do, it keeps giving me one of 2 errors which do not make sense to me. If I don't escape it as above, I get something to the effect of:

Quote:
1/S/ ambiguous redirect
And with it as above (which seems right to me), I get:

Quote:
-bash s/S: No such file or directory.
Now I do know this is a hardlink and that *may* be the issue - at least I read that it does cause issues in Solaris due to an inode mismatch. But being able to edit it directly makes me think this isn't the issue here. The only other option I have is that I don't have everything escaped correctly, but I don't know what or where I am missing it.

Last edited by steeladept; 11-28-2014 at 03:53 PM.
 
Old 11-28-2014, 03:34 PM   #2
Ratamahatta
Member
 
Registered: Feb 2012
Location: Germany
Distribution: siduction
Posts: 134

Rep: Reputation: 17
I may be mistaken, but I assume not everyone on this site has mongodb installed or just a different version of the script. So most people here would not be able to easily reproduce that error. It would help a lot if you posted line 66, so sed specialists here can have a go at it.
 
1 members found this post helpful.
Old 11-28-2014, 03:38 PM   #3
steeladept
LQ Newbie
 
Registered: Oct 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
Sorry about that. Tried to be thorough and still missed one

Here is line 66 of init.d/mongod:

Code:
 daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
So if I can just put in the " after $OPTIONS and delete the rest, it works fine.

And adding to original post for completeness for any reading after...

Last edited by steeladept; 11-28-2014 at 03:51 PM.
 
Old 11-28-2014, 04:16 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Without being too creative something like this should work
Code:
sed 's/ >.*$/"/'
 
1 members found this post helpful.
Old 11-28-2014, 04:26 PM   #5
steeladept
LQ Newbie
 
Registered: Oct 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
SOLVED - sed issue editing /etc/init.d/mongod file

That definitely works, but it got a little too greedy. Unfortunately, there is another line checking for NUMACTL_ARGS which gets edited as well. However, that helped a lot. Looks like I was making it too difficult on myself.

I can base it off this and just change the one line. I will just add the line number before it to make it work. New command (and it appears to work on the command line - will try in the script later):

sed '66 s/ >.*$/"/' /etc/init.d/mongod

Thanks!

Last edited by steeladept; 11-28-2014 at 04:28 PM. Reason: Solution provided
 
Old 11-28-2014, 04:31 PM   #6
Ratamahatta
Member
 
Registered: Feb 2012
Location: Germany
Distribution: siduction
Posts: 134

Rep: Reputation: 17
syg00 was faster...

But his answer will remove all redirections. I just succeeded with
Code:
sed -i 's-\(.*\) >/dev/null 2>&1-\1-g'
That is more specific but I'm thinking way too complicated again...

Last edited by Ratamahatta; 11-28-2014 at 04:33 PM. Reason: fixed some uncertainty
 
1 members found this post helpful.
Old 11-28-2014, 04:40 PM   #7
steeladept
LQ Newbie
 
Registered: Oct 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
Wink

Quote:
Originally Posted by Ratamahatta View Post
syg00 was faster...

But his answer will remove all redirections. I just succeeded with
Code:
sed -i 's-\(.*\) >/dev/null 2>&1-\1-g'
That is more specific but I'm thinking way too complicated again...
LOL - yes, but yours is closer to what I was attempting to do. I do see why syg00's solution is better, though.
 
Old 11-28-2014, 06:41 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Personaly I wouldn't rely on line number being always the same - a package update could change that silently by inserting lines. Better to use some unique text - "MUNGO_USER" maybe.
 
Old 12-01-2014, 09:30 AM   #9
steeladept
LQ Newbie
 
Registered: Oct 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
Well I definitely agree to that, but I was having the toughest time getting it to match consistently. I am admittedly new to this, but my regex expressions never seem to turn up what they should. That said, I think I finally have it figured out. Still have to test it to make sure there isn't other parts the Regex changes, but this *seems* to work (daemon may be another good one since I didn't find another entry for it) -

Code:
sed 's/\(.*MONGO_USER.*\) >.*$/\1"/' /etc/init.d/mongod
*NOTE: This is more for future readers who may want/need a better solution than my original.

Last edited by steeladept; 12-01-2014 at 09:32 AM.
 
Old 12-01-2014, 04:00 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Sorry, I meant to use the text as an "address" - where you had "66" previously. This should select lines containing that text, and only then attempt the update.
Code:
sed '/MONGO_USER/ s/ >.*$/"/' /etc/init.d/mongod
 
Old 12-01-2014, 04:03 PM   #11
steeladept
LQ Newbie
 
Registered: Oct 2014
Posts: 24

Original Poster
Rep: Reputation: Disabled
Thumbs up

Now THAT is cool. I didn't even know you could do that. Always learning. Thanks again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Editing a file using sed within a script t_tunewiki Linux - Newbie 3 02-28-2012 12:28 PM
Setting numlock on at boot, editing the Init/Default file, don't want to mess it up sandaili Fedora 2 07-08-2008 04:35 PM
Editing a File with SED SBN Linux - General 16 05-18-2008 08:05 AM
Command interpretation issue in Sed script file angel115 Programming 9 04-21-2006 07:26 PM
multiple file editing using... sed? perl? bpk Linux - Software 2 10-07-2005 04:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration