Reputation: 21
So I'm working on an assignment and I'm very close to getting it. Just having issues with the last part. Here is the whole problem I guess, just so you know what I'm trying to do -
Write a shell script called make_uid which creates user login names given a file containing the user's full name. Your script needs to read the newusers file, and for each name in the file create a login name which consists of the first character of the users first name, and up to 7 characters of their last name. If the last name is less than seven characters, use the entire last name. If the user only has one name, use whatever is provided as a name (in the newusers file) to generate an 8 character long login name. Note: login names need to be all lower case!
Once you have created a login name, you need to check the passwd file to make sure that the login name which you just created does not exist. If the name exists, chop off the last character of the name that you created, and add a digit (starting at 1) and check the passwd file again. Repeat this process until you create a unique user login name. Once you have a unique user name, append it to the passwd file, and continue processing the newusers file.
This is my code so far. At this point, it makes a full passwd file with all of the login names. I'm just having trouble with the final step of sorting through the list and editing duplicates accordingly.
#!/bin/bash
#Let's make some login names!
declare -a first
declare -a last
declare -a password
file=newusers
first=( $(cat $file | cut -b1 | tr "[:upper:]" "[:lower:]" | tr '\n' ' ') )
for (( i=0; i<${#first[@]}; i++)); do
echo ${first[i]} >> temp1
done
last=( $(cat $file | awk '{print $NF}' $file | cut -b1-7 | tr "[:upper:]" "[:lower:]"))
for (( i=0; i<${#last[@]}; i++)); do
echo ${last[i]} >> temp2
done
paste -d "" temp1 temp2 >> passwd
sort -o passwd passwd
more passwd
rm temp1 temp2
Upvotes: 1
Views: 377
Reputation: 3666
Well, I probably shouldn't be answering a homework assignment but maybe it will help you learn.
#!/bin/bash
infile=./newusers
outfile=./passwd
echo -n "" > $outfile
cat $infile | while read line; do
read firstName lastName < <(echo $line)
if [ -z "$lastName" ]; then
login=${firstName:0:8}
else
login=${firstName:0:1}${lastName:0:7}
fi
digit=1
while fgrep -q $login $outfile; do
login=${login%?}$digit
let digit++
done
echo $login >> $outfile
done
There may be some way to do the fgrep check in a single command instead of a loop but this is the most readable. Also, your problem statement didn't say what to do if the name was less than 8 characters so this solution doesn't address that and will produce passwords that are short if the names are short.
Edit: The fgrep loop assumes that there will be fewer than 10 duplicates. If not, you need to make it a bit more robust:
lastDigit="?"
nextDigit=1
while fgrep -q $login $outfile; do
login=${login%$lastDigit}$nextDigit
let lastDigit=nextDigit
let nextDigit++
done
Upvotes: 1
Reputation: 121760
Add all user names into another file before adding the digit. Use fgrep -xc theusername thisotherfile
, this returns a digit. Append the digit to the login name if it's not 0.
Upvotes: 0