Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a file (users_uid) with users and uid are two parameters as given below :

    assds  611
    dsdsd  612
    xyzx   613
    rerer  614

These users exist in the machine. I want to change the uid of each user to that value given right for the username. I know that I can modify the uid using usermod -u <uid> <user>. But I don't know how to read the two values in the file and put them as the 2 parameters in a for loop with usermod command.

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

awk '{print "usermod -u "$2" "$1}' users_uid | bash

share|improve this answer
    
Thanks It worked ! But the sed is not required i think. # cat users_uid |awk '{print "usermod -u "$2" "$1}' | bash –  nitins Dec 8 '11 at 13:03
1  
Sed it required in case the first line is like in your example users uid. Otherwise it isn't required. –  rush Dec 8 '11 at 13:06
    
@nitins: if the users_uid file doesn't contain column name (the first line), no need to use cat: awk '{ print "usermod -u "$2" "$1 }' users_uid | bash. –  quanta Dec 8 '11 at 13:14
    
Yes you are correct. I actually don't have that line. Just added here to make it clear –  nitins Dec 8 '11 at 13:15
    
I've removed that line to make it clear that the "header" is not actually in the file. –  Monty Harder Jul 15 '13 at 14:51
add comment

The accepted answer runs the risk of executing malicious or accidental content in the data file. Here is the correct way to do what you want without spawning awk and an additional copy of bash plus it's safe from the risk I mentioned.

while read -r user uid
do
    usermod -u "$user" "$uid"
done < users_uid

As a one-liner:

while read -r user uid; do usermod -u "$user" "$uid"; done < users_uid
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.