Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sed Issue ..

SOLVED
Go to solution
Highlighted
rleon
Regular Advisor

Sed Issue ..

I everyone I need some help with a sed statement.

 

This is a scirpt that updates the /etc/shadow on linux and solaris.

 

And I am having probems with one line.

 

ssh -q ${HOST} 2>/dev/null "rm /tmp/shadow.*"
ssh -q ${HOST} 2>/dev/null "cp /etc/shadow /tmp/shadow.bak"

 

ssh -q ${HOST} 2>/dev/null "sed -e '/^root:/s/:[^:]*:/:${PASS}:/' /tmp/shadow.bak > /tmp/shadow.new"

Most of the time it works without any issues but for some reason everyone once in a while /tmp/shadow.new is empty. The file is there .. just nothing in it.

 

-rw-------   1 root     root           0 Sep 14 10:32 /tmp/shadow.new

 

Is there something wrong with my sed staement?

Or am I missing something?


Thanks

Ricardo

 

 

 

 

    

6 REPLIES
rleon
Regular Advisor

Re: Sed Issue ..

Im a little closer ..

 

it seems that the issue happens when the password hash has a \ or / in it.

 

I get the following errors:

 

sed: command garbled:

 

or

 

sed: -e expression #1, char 29: Unknown option to `s'

 

Dennis Handly
Acclaimed Contributor

Re: sed Issue .. (delimiter)

>it seems that the issue happens when the password hash has a \ or / in it

 

You could replace that sed "/" by a comma.

rleon
Regular Advisor

Re: Sed Issue ..

Hi Dennis

 

Can you clarify ..

 

If the password hash is

abcdef123/ABC

 

And I change it to

abcdef123,ABC

 

That makes the password different and doesnt work.

Dennis Handly
Acclaimed Contributor
Solution

Re: sed Issue .. (delimiter)

>Can you clarify?

 

Change your sed command to use "," as the delimiter:

ssh -q ${HOST} 2> /dev/null "sed -e '/^root:/s,:[^:]*:,:${PASS}:,' /tmp/shadow.bak > /tmp/shadow.new"

rleon
Regular Advisor

Re: sed Issue .. (delimiter)

Thanks !!!

 

That worked like a charm !!

 

Woud you mind explaining what the comma does?

 

Dennis Handly
Acclaimed Contributor

Re: sed Issue .. (delimiter)

>Would you mind explaining what the comma does?

 

The same as the "/".  :-)

You can use any char as the delimiter, as long as it matches.  From sed(1):

      (2)s/regular expression/replacement/flags
                  Substitute replacement string for instances of regular
                  expression in the pattern space.  Any character can be
                  used instead of /.  For a fuller description see ed(1).

 

It seems you can even use a ";", though it is normally a command separator.