Change default shell of a specific user with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change default shell of a specific user with awk
# 1  
Old 07-18-2012
Change default shell of a specific user with awk

I would like to replicate the functionality of chsh (or passwd -e) by awk.
This is what I got so far, but I think there should be an easier way to search and replace field $7 only for lines beginning with user_name:

Code:
awk -v user_name="$user_name" -v new_shell="$new_shell" -F: '$1 == user_name { print $1":"$2":"$3":"$4":"$5":"$6":"new_shell};' passwd

Any help is appreciated!
# 2  
Old 07-18-2012
There is no sanity check on the input. You could get a shell that doesn't exist into your passwd. Or is that checked before? Why not use the system commands?

Anyway, a shorter way you can try out:
Code:
awk -F":" -v user_name="$user_name" -v new_shell="$new_shell" -F: '$1 == user_name { $7=new_shell; print };' OFS=":" passwd


Last edited by zaxxon; 07-18-2012 at 05:33 AM..
# 3  
Old 07-18-2012
Bug

Quote:
Originally Posted by zaxxon
There is no sanity check on the input. You could get a shell that doesn't exist into your passwd. Or is that checked before?
Thank you very much for your help! I ended up using your solution and it works perfeclty. The input variables are validated before running the awk.
Quote:
Originally Posted by zaxxon
Why not use the system commands?
I'm working on a platform independent script and OS commands are different.

Final version:
Code:
awk -F":" -v user_name="$user_name" -v new_shell="$new_shell" -F: '$1 == user_name { $7=new_shell }; 1' OFS=":" passwd

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

Find specific pattern and change some of block values using awk

Hi, Could you please help me finding a way to replace a specific value in a text block when matching a key pattern ? I got the keys and the values from a command similar to: echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1... (2 Replies)
Discussion started by: alex2005
2 Replies

3. UNIX for Advanced & Expert Users

Change sFTP home directory for particular user and from specific server

Hello Folks, Of course i came here for your favour :) How to set a defalult home directory for sFTP login ( at present users land in to their home directrory) when they connect from specific server. When server(A) sFTP's to Linux server(B) they land to thier home directory. I want... (5 Replies)
Discussion started by: Thala
5 Replies

4. Shell Programming and Scripting

awk to change specific string to new value if found in text file

I am trying to use awk to change a specific string in a field, if it is found, to another value. In the tab-delimited file the text in bold in $3 contains the string 23, which is always right before a ., if it is present. I am trying to change that string to X, keeping the formatting and the... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Add character to specific columns using sed or awk and make it a permanent change

Hi, I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches. file has lot of functions defined a.sh #!/bin/bash fn a { beautiful evening sunny day } fn b { } fn c { hello world .its a beautiful day ... (12 Replies)
Discussion started by: ashima jain
12 Replies

6. Shell Programming and Scripting

How to change a number on a specific line with cshell or shell?

Hello all, I need to change a number in a file by adding some residuals respectively To make it clear, I need to add 0.11 to the number between 24-28 (which is below the SECON) for all the lines starting with FRR1 or I need to add 0.13 to the number between 24-28 (which is below the... (9 Replies)
Discussion started by: miriammiriam
9 Replies

7. Shell Programming and Scripting

How to change a number on a specific lines in a file with shell?

Hello My problem is that I want to change some specific numbers in a file. It is like, 2009 10 3 2349 21.3 L 40.719 27.388 10.8 FRO 7 0.8 1.1LFRO 2.6CFRO 1.1LMAM1 GAP=157 1.69 5.7 5.9 5.8 0.5405E+01 0.4455E+00 0.1653E+02E STAT SP IPHASW D HRMM SECON CODA AMPLIT... (11 Replies)
Discussion started by: miriammiriam
11 Replies

8. UNIX for Dummies Questions & Answers

How to change Default Shell for any user?

Hi, I am new for solaris... how can we change default shell for any user and how to check that which shall currently we are in...... (1 Reply)
Discussion started by: lalit21984
1 Replies

9. Shell Programming and Scripting

How do i change to super user then revert back to ordinary user ,using shell script?

Hi all, I am trying to eject the cdrom from a livecd after certain stage... Now assuming that it is possible to eject,please consider my issue!!! The OS boots into a regular user by default...so i am unable to use the eject command to push out the drive... However if i try pfexec eject it... (3 Replies)
Discussion started by: wrapster
3 Replies

10. UNIX for Dummies Questions & Answers

change user in a shell

Hi, is it possible to chnage the user in a shell script? i would like to create a script with should make some actions on the host which needs root permissions. i want do give the user which should start the script the root permissions. so is it possible to make a command in the script... (2 Replies)
Discussion started by: scottl
2 Replies
Login or Register to Ask a Question