Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Sed Issue ..

SOLVED
Go to Solution
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

 

 

 

 

    

1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

Re: sed Issue .. (delimiter)

[ Edited ]

>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"

6 REPLIES
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'

 

Acclaimed Contributor Acclaimed Contributor

Re: sed Issue .. (delimiter)

[ Edited ]

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

 

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

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.

Acclaimed Contributor Acclaimed Contributor

Re: sed Issue .. (delimiter)

[ Edited ]

>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"

Regular Advisor

Re: sed Issue .. (delimiter)

Thanks !!!

 

That worked like a charm !!

 

Woud you mind explaining what the comma does?

 

Highlighted
Acclaimed Contributor 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.