More refined script

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
I have a ubuntu server that needs a few hundred accounts created on it. All of these accounts are in this format

student[room number]u[user number] (example student123u1 or student225u30)
50 accounts will be made per room and the passwords will be identical for the user name. These accounts are going to be used for a semester then removed at the end of the semester and replaced.
I have written the following script

roomlist.txt is a text file with the following format
student123u
student563u
student121u
etc
#!/bin/bash
for y in `more roomlist.txt`
do
for x in {1..50}
do
user=$y$x
password=`mkpasswd $user`
useradd $user -m -p $password
echo Account created for User: $user
done
done

Similarly I have a script that replaces the inner do loop with userdel -r $user to delete each user as so:

#!/bin/bash
for y in `more roomlist.txt`
do
for x in {1..50}
do
user=$y$x
userdel -r $user
echo Account deleted for User: $user
done
done

Any suggestions on a better way to write these scripts or stream line them?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Do the scripts work? If so I'd say you did good enough. I'm not sure how you're splitting up the username from the rest of the string though. I'd probably use cat instead of more. Actually I probably would've started with perl and a regexp to split up the lines since I'm more comfortable with them.