1833723 Members
2704 Online
110063 Solutions
New Discussion

Re: sed usage

 
Achilles_2
Regular Advisor

sed usage

Hi all,

I am writing a setup srcipt which want to replace one line of the file /etc/passwd

root::0:3::/:/sbin/sh
daemon:*:1:5::/:/sbin/sh
bin:*:2:2::/usr/bin:/sbin/sh
sys:*:3:3::/:
adm:*:4:4::/var/adm:/sbin/sh
uucp:*:5:3::/var/spool/uucppublic:/usr/lbin/uucp/uucico
lp:*:9:7::/var/spool/lp:/sbin/sh
nuucp:*:11:11::/var/spool/uucppublic:/usr/lbin/uucp/uucico
hpdb:*:27:1:ALLBASE:/:/sbin/sh
nobody:*:-2:-2::/:
www:*:30:1::/:
smbnull:*:101:101:DO NOT USE OR DELETE - needed by Samba:/home/smbnull:/sbin/sh

to root::0:3::/.root:/sbin/ksh, my idea is to use sed

grep root /etc/passwd | sed -e 's/"/"/"/.root"/' -e 's/"/sbin/sh"/"/sbin/ksh"/'
but I find that the character / is the special.

Do anyone has an idea? Thanks
9 REPLIES 9
Arunvijai_4
Honored Contributor

Re: sed usage

You can use \/ instead.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Pete Randall
Outstanding Contributor

Re: sed usage

You're trying to change root's shell to the korn shell? This is NOT a good idea. When you need to boot into single user mode, the korn shell is not going to be available, because /usr is not mounted. And, unless you managed to put it there, which is unlikely because it's dynamically rather than staticaly linked, ksh is not in /sbin, anyway.

I would rethink this whole proposal if I were you.


Pete

Pete
Muthukumar_5
Honored Contributor

Re: sed usage

If you want to change /etc/passwd file then use passwd command first. Leave root account's shell as /sbin/sh. It is the only one account going to be used in single user mode.

If you want to play manually then,

# cp -p /etc/passwd /etc/passwd.bak
# passwd -e /sbin/ksh root

It will change /sbin/sh to /sbin/ksh

hth.



Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: sed usage

there is no /sbin/ksh available on HP-UX. Double check before doing anything,

# ls /sbin/ksh (11.11 and 11.23)
/sbin/ksh not found

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Stephen Keane
Honored Contributor

Re: sed usage

As Pete says you probably want to rethink what you are trying to do, but for future reference you can use another delimiter with sed if you want.

e.g.

# sed -e 's|before|after|g' file

uses the pipe symbol '|' as the delimiter.

so in your case ...

grep "^root:" /etc/passwd | sed -e 's|/sbin/sh|/sbin/ksh|'
James R. Ferguson
Acclaimed Contributor

Re: sed usage

Hi:

Pete is correct. Unless you only want to boot your system one last time, do *NOT* change root's shell! It must remain '/sbin/sh' as he noted. On HP-UX, 'sh' is the POSIX shell which is a superset of the Korn88 shell -- which is all you get with '/usr/bin/ksh'.

Regards!

...JRF...
Muthukumar_5
Honored Contributor

Re: sed usage

For sed practice you can try as,

# sed -e '/root/s/sh/ksh/g' /etc/passwd

It won't update in /etc/passwd

You can negate / with \ or else s## instead of s// or s%%.

# echo "root::0:3::/.root:/sbin/sh" | sed -e '/root/s%/sbin/sh%/sbin/ksh%'
root::0:3::/.root:/sbin/ksh
# echo "root::0:3::/.root:/sbin/sh" | sed -e '/root/s#/sbin/sh#/sbin/ksh#'
root::0:3::/.root:/sbin/ksh
# echo "root::0:3::/.root:/sbin/sh" | sed -e '/root/s/\/sbin\/sh/\/sbin\/ksh/'
root::0:3::/.root:/sbin/ksh
# echo "root::0:3::/.root:/sbin/sh" | sed -e '/root/s|/sbin/sh|/sbin/ksh|'
root::0:3::/.root:/sbin/ksh

hth.
Easy to suggest when don't know about the problem!
John M.
Advisor

Re: sed usage

How about using /usr/sbin/logins command to generate list of users then use that list as input to /usr/sbin/usermod command for each users in for loop?
It fails for users that are logged on, but can be scheduled to run offshift.
"Things fall apart, it's scientific" David Byrne
Stuart Abramson
Trusted Contributor

Re: sed usage

There is a lot of good advice about changing the root password shell above. Take it.

There is no difference between ksh and sh on hpux anymore, so NO reason to make this change anyway.