Fedora Linux Support Community & Resources Center
  #1  
Old 8th October 2009, 10:36 PM
cardinalsfan030 Offline
Registered User
 
Join Date: Aug 2009
Posts: 21
linuxfedorafirefox
Scripting Question

Hello Everyone, I am some what new to Fedora, but I do know my way around pretty good. I am currently taking a Linux (Fedora) class in college. This week was all about programming and scripting. I personally hate programming but its one of those things that is good to know.... anyways so here is my problem. I have to write a script to include the following:

You have to write a script that reads a list of user names. The script should also check if the user account exists. For this check to see if the user' home directory exists. If the user account does exist, then display a message stating so, if it does not exist, then create it with the following command
Code:
useradd -m account_name
In this command, account_name is the name of the user account and will be represented by a variable in your script. To properly test the script make sure that your list includes names that are on your system as well as names that do not exist.

Note: Since you are creating new accounts with this script, it will have to be run as root.

I already have the list made up and some accounts are on my computer and some are not. The biggest problem I am having with this is the code to see if the users home directory exists and what variable to use with adduser -m account_name.

If someone could please point me in the right direction or give me an example of how the script should be written. I had to do some basic If Then....Else If... Case....For Loop..... and While Loop scripts which were not a problem, this is the only one I am not sure about. Thanks in advance for any help
Reply With Quote
  #2  
Old 9th October 2009, 02:11 AM
ghostdog74 Offline
Registered User
 
Join Date: Sep 2006
Posts: 52
linuxfedorafirefox
so what have you been doing during class??
that aside, read here for bash scripting guide
Reply With Quote
  #3  
Old 9th October 2009, 02:39 AM
cardinalsfan030 Offline
Registered User
 
Join Date: Aug 2009
Posts: 21
linuxfedorafirefox
Quote:
Originally Posted by ghostdog74 View Post
so what have you been doing during class??
that aside, read here for bash scripting guide
Thanks for the guide and FYI I do most of my work in class, this has been one of the exceptions. all i really need is the best way to search for the users home folder. I pretty much know how to write everything else.
Reply With Quote
  #4  
Old 9th October 2009, 02:48 AM
forkbomb Offline
Registered User
 
Join Date: May 2007
Location: U.S.
Posts: 4,851
linuxgentoofirefox
Assuming you're checking if USER's home dir exists:

if [ -d /home/"$USER" ]; then
echo "Directory exists..."
else
echo "Directory doesn't exist..."
fi

Last edited by forkbomb; 9th October 2009 at 02:50 AM.
Reply With Quote
  #5  
Old 9th October 2009, 03:26 AM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,814
linuxfedorafirefox
It is more reliable to extract the home directory from the passwd entry.

If you can use perl, just use the getpwnam function, then verify that the home directory exists...

a bit overkill, but partially created logins are known to happen.

The advantage of this is that users home directories may be located anywhere, not just "/home".
I worked at a large site (1200+ users), and we grouped them into /u/a/<user> /u/b/<user> /u/c/<user>
so that we could use one filesystem per group (not unix group - just an administrative grouping -
all users from division "a" were in /u/a, those from division "b" ...
Reply With Quote
  #6  
Old 12th October 2009, 02:07 PM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
DIvide & conquer:

1. Read input text file (bash)
2. Check user existence in /etc/passwd (grep beginning of line)
3. Check grep return code
3.1 If it does not exist, create new user (useradd)
3.2 Else, extract home dir (awk) from /etc/passwd line and check its existence (tjvanwyk covered this already)

P.S. awk maybe omitted in favour of pure bash syntax, e.g. converting the /etc/passwd line to an array (separating at colons)

Example:

Code:
saveIFS=$IFS
IFS=":"
....
name=($line)
echo ${name[5]}
IFS=$saveIFS

Last edited by giulix; 12th October 2009 at 02:32 PM. Reason: Added array option
Reply With Quote
  #7  
Old 12th October 2009, 02:35 PM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
I thought that was straight forward I read the comment from jpollard (I'm but a learner myself).

One of the problems I had in learning this stuff is that one is not really learning "programming" as such when you learn, say, bash or python, etc. Really what your learning is logical thinking with a sort of coloured or flavoured tool set. The other side of the coin is understanding how the system your working on works - which in many ways is not really the same thing a shell scripting at all. For example, take IP tables, you can read the man pages and get the idea of what this or that acts like in the script, but if you don't take the time to learn something about the mechanical side of the iptables you could find yourself on the high road to a gaping security hole!

I mention this because it can be a point of real frustration in "programming", even though its not really in the programming side of it so far as language learning goes. It seems to me that if you can pretty well be exacting like in the example of giulix gave (divide and conqueror), and then simply type that up in bash words, you got it. The problem is, there is no logic that teaches you that /etc/paaswd might just be relevant to the matter, instead, experience and time seems to teach that.
__________________
The longer I run numerous Linux box's, the more I like it.

UUID: 5a0128f0-55b8-11e0-b31d-eb482d4d1d3a
Reply With Quote
Reply

Tags
question, scripting

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
scripting question i think Zero-Override Using Fedora 2 19th April 2007 11:45 AM
Bash scripting question MaldiGola Using Fedora 5 7th September 2006 05:13 PM
Bash scripting question leaded Programming & Packaging 4 17th March 2006 04:15 PM


Current GMT-time: 10:09 (Thursday, 21-08-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat