duplicate UID in /etc/passwd - change with shell script
Hello, I need to read /etc/passwd and check for duplicate UIDs for different users. If found, assign them the next free UID above 1000.
can somebody help me ? A sample from my /etc/passwd file: Code:
hugataga:x:1000:1000:hugataga,,,:/home/hugataga:/bin/bash I have the script to find the next free UID: Code:
#! /bin/bash |
Sounds like you want "usermod" to make the change - then if you aren't aiming for top efficiency just have a block of code repeated until there are no more changes to make.
Changing owners of home directories and all that would be a further step. |
Code:
awk -F: -v OFS=: '{ $3=$4=($3+a[$3]++) }1' <( sort -t : -k3n /etc/passwd ) First, the input needs to be sorted, so I used a bash process substitution to dynamically create the sorted "file". awk's -F and -v options set the input and output field separators to ":". $3=$4=() changes fields 3 and 4 (I'm assuming that gid needs to be incremented too) to the new value calculated inside the parentheses. $3+a[$3]++ adds the current value of the array entry "a[$3]" to the value of field three, then post-increments the array value by one ("a[$3]++"). This means that the first time you encounter a given value in field 3, a[$3] will be zero, and one higher for every time encountered after that. The final "1" at the end is a shortcut trick for printing the current line after the modifications have been made. awk only outputs to stdout, so you'll have to save it to a temp file and replace it after that. Edit: Note however that this is just a quick and brain-dead solution. As it stands it only increments the uid number by one and doesn't do any testing against subsequent conflicts. |
All times are GMT -5. The time now is 07:15 AM. |