Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Add users, passwords, email and groups ***Class Assignment***
This is my add user from csv script. I am new to linux so do not laugh to hard at my hack job. My problem is that my "students group" will not work. All other groups add fine. I am also not sure if email will work...
My instructer told me to post it on the forums to see if i can get help, so your not helping me cheat.
Thanks in advance.
Kyle
#!/bin/bash
if [ "$(whoami)" != "root" ]
then
echo "Error: You ARE NOT ROOT!!!!!"
exit 1
else
echo "You are Lucky! I am watching you!!"
echo "Please enter the CSV"
read CSVFILE
if [ -f $CSVFILE ]
then
exec < $CSVFILE
while
read line
do
firstName=$(echo $line | awk -F, '{print $1'})
lastName=$(echo $line | awk -F, '{print $2'})
department=$(echo $line | awk -F, '{print $3'})
department=$(echo $department | tr 'A-Z' 'a-z')
group1=$(echo $line | awk -F, '{print $5'})
group1=$(echo $group1 | tr 'A-Z' 'a-z')
group2=$(echo $line | awk -F, '{print $6'})
group2=$(echo $group2 | tr 'A-Z' 'a-z')
group3=$(echo $line | awk -F, '{print $7'})
group3=$(echo $group3 | tr 'A-Z' 'a-z')
group4=$(echo $line | awk -F, '{print $8'})
group4=$(echo $group4 | tr 'A-Z' 'a-z')
group5=$(echo $line | awk -F, '{print $9'})
group5=$(echo $group5 | tr 'A-Z' 'a-z')
firstInit=$(echo $line | cut -c1)
userName=$lastName$firstInit
userName=$(echo $userName | tr 'A-Z' 'a-z')
firstInit=$(echo $firstInit | tr 'a-z' 'A-Z')
fNameCut=$(echo $firstName | cut -c2-40)
lastInit=$(echo $lastName | cut -c1)
lastInit=$(echo $lastInit | tr 'a-z' 'A-Z')
lNameCut=$(echo $lastName | cut -c2-40)
day=$(date +%d)
month=$(date +%m)
year=$(date +%Y)
nextYear=$((year + 1))
useradd $userName -c "$firstInit$fNameCut $lastInit$lNameCut" -e "$nextYear-$month-$day"
echo "useradd $userName -c $firstInit$fNameCut $lastInit$lNameCut -e $nextYear-$month-$day $department"
if [ "$department" = "year one" ]
then
usermod -G students $userName
fi
if [ "$group1" = "groupa" ]
then
usermod -G GROUPA $userName
fi
if [ "$group2" = "groupb" ]
then
usermod -G GROUPB $userName
fi
if [ "$group3" = "groupc" ]
then
usermod -G GROUPC $userName
fi
if [ "$group4" = "groupd" ]
then
usermod -G GROUPD $userName
fi
if [ "$group5" = "tech" ]
then
usermod -G TECH $userName
fi
ranlist="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
passChar1=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar2=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar3=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar4=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar5=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar6=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar7=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
passChar8=$(echo ${ranlist:$(($RANDOM%${#ranlist})):1})
pass=$passChar1$passChar2$passChar3$passChar4$passChar5$passChar6$passChar7$passChar8
echo "$pass" | passwd --stdin "$userName"
echo "Password for $userName is $pass"
echo
SUBJECT="New User Infromation"
TO="coreyk@itas.ca"
MESSAGE="/tmp/message.txt"
echo "Welcome to ITAS $firstInit$fNameCut $lastInit$lNameCut" >> $MESSAGE
echo "User Name: $userName" >> $MESSAGE
echo "Password: $pass" >> $MESSAGE
echo "Your account expires on $nextYear-$month-$day" >> $MESSAGE
echo "We recommend changing your password immediately" >>$MESSAGE
echo "From your user account type passwd and new password">> $MESSAGE
echo "Good Luck!!" >> $MESSAGE
/bin/mail -s “$SUBJECT” “$TO” < $MESSAGE
rm $MESSAGE
done
else
echo "Your Dead"
fi
fi
Last edited by Caighil; 03-07-2008 at 03:28 PM.
Reason: got it working
Spelling has not always been my strong suit. Neither has grammer apparently. Now that you have taught me a valuable english lesson, any ideas about the script?
In the future you may wish to utilize [ CODE ] tags to help make your text more readable.
Secondly, the LQ Rules explicitly forbids the posting of homework; not only will you learn more but working on it yourself, if we do your homework for you, you'll feel obligated and/or comfortable with posting your next assignment, then your next one, then your next one, et cetera. We don't want either situation.
If there is a particular part of scripting, or linux architecture, or system commands, you're having problems with, feel free to post that -- the only way to learn is to try, ask question, and then try again, in that order. Saying things like "doesn't work" doesn't help us provide you with a fitting answer. What have you tried? How did action X fail? The more information you can provide about the specific problem, the more aptly we are going to be able to help you.
The fact that your thread title blatantly advertises that your thread breaks the rules is quite humorous however.
Fair enough. I was unaware of the "homework rule" And I was unaware of the [code] aswell. I will try to reword it. I have spent hours trying to figure out why the students group will not work. I have tried -g and -G/ -g will not work in terminal, but -G will. Lecture is starting , I will try to reword it later.
What I'd recommend is running the script using $ bash -x /script/here
It's a "debug" mode that allows you to see how variables are being substituted at runtime, which logical conditions are met, etc. You can learn all kinds of interesting things about your script this way.
A clarification on the "no homework" rule: if a member posts up a list of questions and says "please help", we tend to close the threads. If a member has clearly done the work and is looking for a hand with a part they don't understand/can't make work/etc, as in this case,it's fine.
We have no issue with helping people if they are prepared to do the work, it's the lazy ones we close down.
Since you've handled yourself quite well in this thread (no temper tantrums, no foul language) here's some feedback on the final script.
- where you say "checks to see if file exsits" do *test* before operating on it, like [ -f somefile ] || { echo "No such file, exiting."; exit 1; }
- you use "awk -F," which means you're expecting one type of CSV, maybe test for the ';' variety and store it in a value to use
- instead of awk -F, if you use BaSH, you can also manipulate the IFS
- removing spaces VAR=$(echo $department | sed 's/ //') can also be done in BaSH as VAR=${VAR// /}
- one char from a variable firstInit=$(echo $line | cut -c1) can also be firstInit=${line:0:1}
- I like the ${ranlist:$(($RANDOM%${#ranlist})):1} solution, but how random is it as opposed to using some external app?
Secondly, the LQ Rules explicitly forbids the posting of homework;
That's not what it says.....
Regardless of rules, the common practice is to help those who are making an effort, and to ruthlessly harass the ones that just cut and paste the instructor's assignment sheet into (multiple) fora.
For OP: Welcome to LQ and we're glad you got it fixed.
Thanks for the feed back. I try to be civilized (except when the Canucks blow 3rd period leads). At 34 I do not see the value in getting other people to do my homework for me. I am going to reexamine my script in a few day(weeks) as my work load in JavaScript, PHP/MySQL, Ethics and Networking is killing me time wise. I am sure I will be back many many times to seek advice, and eventually give some out too. Thanks again for all your help, UnSpawn I am going to try to implement those suggestions to see how they work.
Kyle Corey
PS feel free to check out the webpage, it was my first attempt at using a css. It will also give you a chance(not that you need one) to see what we do in the ITAS program at Malaspina Univeristy. www.kylecorey.ca
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.