LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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



Reply
 
Search this Thread
Old 03-03-2008, 12:00 PM   #1
Caighil
LQ Newbie
 
Registered: Feb 2008
Posts: 22

Rep: Reputation: 15
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 04:28 PM. Reason: got it working
 
Old 03-03-2008, 12:31 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by Caighil
My instructer told me to post it on the forums to see if i can get help, so your not helping me cheat.
Clever.

Two tips from me: 1) it's spelled "instructor"; 2) the contraction of "you" and "are" is "you're".
 
Old 03-03-2008, 12:39 PM   #3
Caighil
LQ Newbie
 
Registered: Feb 2008
Posts: 22

Original Poster
Rep: Reputation: 15
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?

Kyle
 
Old 03-03-2008, 12:43 PM   #4
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
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.
 
Old 03-03-2008, 12:47 PM   #5
Caighil
LQ Newbie
 
Registered: Feb 2008
Posts: 22

Original Poster
Rep: Reputation: 15
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.

Kyle
 
Old 03-03-2008, 12:51 PM   #6
Caighil
LQ Newbie
 
Registered: Feb 2008
Posts: 22

Original Poster
Rep: Reputation: 15
Quote:
The fact that your thread title blatantly advertises that your thread breaks the rules is quite humorous however.
Again it is my newness to this forum.
 
Old 03-03-2008, 01:19 PM   #7
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by Caighil
... any ideas about the script?
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.
 
Old 03-03-2008, 01:53 PM   #8
Caighil
LQ Newbie
 
Registered: Feb 2008
Posts: 22

Original Poster
Rep: Reputation: 15
Code:
  if [ "$department" = "yearone"  ]
          then
           usermod -G students $userName           
        fi
Any suggestions why this one if/fi statement will not add users to the group.
The next 4 work fine.

THnaks Kyle
 
Old 03-03-2008, 03:05 PM   #9
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
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.

Please continue
 
Old 03-07-2008, 04:25 PM   #10
Caighil
LQ Newbie
 
Registered: Feb 2008
Posts: 22

Original Poster
Rep: Reputation: 15
Thanks to all :)

Thanks to everyone for their help, both in linux, and in grammer.

Got the issue solved. My groups were in conflict with each other and it would not write to 2 groups. If anyone is interested

please feel free to visit www.kylecorey.ca under portfolio and linux is my finished code. It may be a good laugh to some

expert progammers and may help some novices like me.

Cheers Kyle Corey.
 
Old 03-08-2008, 05:08 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,699
Blog Entries: 54

Rep: Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962Reputation: 2962
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?
 
Old 03-08-2008, 08:06 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Quote:
Originally Posted by Poetics View Post
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.
 
Old 03-09-2008, 05:12 PM   #13
Caighil
LQ Newbie
 
Registered: Feb 2008
Posts: 22

Original Poster
Rep: Reputation: 15
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

Last edited by Caighil; 03-09-2008 at 05:15 PM.
 
  


Reply

Tags
bash, email, group, groups, password, script, user


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 09:05 AM
LXer: Class Assignment: Implement a Open Source Solution for Non-Profits LXer Syndicated Linux News 0 08-14-2006 03:21 AM
Which C++ editor in Linux has the class view/class browser feature imaginationworks Programming 7 05-22-2006 12:09 AM
overloading an assignment operator to change one class to one that derives it byteframe Programming 1 12-19-2005 05:52 PM
need help with class assignment tenraek Linux - General 4 04-03-2003 01:31 AM


All times are GMT -5. The time now is 10:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration