Results 1 to 10 of 12
Greetings to everyone at the forums. I'm new here as I am still fairly new to linux as well. Recently I've started a unix administration class, where we use red ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-19-2005 #1Just Joined!
- Join Date
- Sep 2005
- Posts
- 6
automated account creation shell script (red hat Linux 9, bourne/bash)
Greetings to everyone at the forums. I'm new here as I am still fairly new to linux as well. Recently I've started a unix administration class, where we use red hat linux 9 as the OS of choice (I know Linux and Unix are two different things, but bear with me here.) for the class.
As a major project that's due out soon, we have to create a script that, based on data supplied from a text file, will create user accounts, directories, passwords, and user ids automatically, without any sort of intervention. In other words, the script must be able to pull the data off of the file without any sort of input from the keyboard.
I've been working dilligently on figuring this out and was thinking of a for/do loop, involving a read command. While there was data from the file to be read, data from the line would be placed into variables, and those variables placed into a useradd command wherever necessary. when the read command reached the end of the file, the loop would terminate, meeting the requirements. In any case, there's one small issue: the read command itself. First off, what do I have to do in order to specify a file as the read source? do I just put in the absolute file path such as:
read /home/dan/datafilekthxbye.txt
will this statement work? or do I have to do something different?
the second part of it involves placing data from the read command into the variables necessary for the useradd command.. how exactly do I do it? say for instance the first field on the line is the account name I want to use (dandoh) or something as such. How do I pull this data from the read statement and apply it to a variable such as name= so I can apply the variable to my useradd command?
I'm going to apologize in advance if this is a noobish question, but I've already stated i'm a noob at linux. If you can offer any sort of help or even point me to where I can find help, I would appreciate it. thanks in advance, as well.
- 09-19-2005 #2
Re: automated account creation shell script (red hat Linux 9, bourne/bash)
You'd want:
Originally Posted by godmachine667
And then reference foo asCode:read foo < /home/dan/datafilekthxbye.txt
Code:$foo
The above solution solves this.the second part of it involves placing data from the read command into the variables necessary for the useradd command.. how exactly do I do it? say for instance the first field on the line is the account name I want to use (dandoh) or something as such. How do I pull this data from the read statement and apply it to a variable such as name= so I can apply the variable to my useradd command?
Good Luck, and I must see it's refreshing to find a Homework post where you've actually done...well your homework ( excuse the expression ).
-lakerdonald
- 09-19-2005 #3Just Joined!
- Join Date
- Sep 2005
- Posts
- 6
what you've posted solves part of the problem, but what if more than one thing is specified per line on the read statement? say it's something like this inside of datafilekthxbye.txt:
(the parenthesized text isn't actually in the file, it's just there to let you know what field is what.)
.. all this in one line..Code:line 1: dan roth (goes in -c argument) droth (username) 243b5234 (password)
so, to read the data off of the line should I do something like this:
and if I were to echo these variables out would get:Code:read foo fee fer < /home/dan/datafilekthxbye.txt
and would I have to use delimiters?Code:dan roth (would this be $foo) droth (would this be $fee) 243b5234 (and would this be $fer?)
thanks in advance.
- 09-19-2005 #4
Yes they would get stored in the respective variables.
- 09-19-2005 #5Just Joined!
- Join Date
- Sep 2005
- Posts
- 6
thanks, this helps in massive amounts.. i'm gonna get to work on coding. will give an update if I run into anymore issues. thank you again.
- 09-20-2005 #6Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
While loops are good for this sort of task (unless you want your variables to have scope outside the while subshell, in which case use the (real) korn shell).
BTW, in the above example, if your lines have more than 4 "words" all the remaining values get stored in the last named var (var4 in this case).Code:cat file | while read var1 var 2 var3 var4 do # Do your stuff for each line in here done
- 09-23-2005 #7Just Joined!
- Join Date
- Sep 2005
- Posts
- 6
the project I have to do, has five fields: user userid useridnumber groupassignments and the password.. Is there any possible way I can squeeze out five variables?
the way I'm trying to do it is a while/do loop, while it reads data off the file it'll do a useradd command with the specified parameters... however, there's a problem: first i tried doing the program itself that way, but then I just wanted to test the variables and see what's getting printed.. it keeps looping the variables off the first read statement infinitely without doing another read statement to read the variables off the next line, etc. anyhow, how's my coding, if anyone can help me out here I'd appreciate it.
when I do this loop, it'll read the first line and all the variables for the read statement I want, but it'll just echo out the first line infintely.Code:#!/bin/bash echo "test output for inputfile.txt " while read name usr uid groups pass < /inputfile.txt do echo " $name $usr $uid $groups $pass " done exit 0
edit: nevermind, got it to work! thanks to all of you! if you want, I can post the code for a working account automation script when I'm done coding it, but for now, I got the test script to work.
- 09-24-2005 #8Linux Newbie
- Join Date
- May 2005
- Location
- Chennai,TamilNadu, India
- Posts
- 141
hi
Since anyway i was working on this after i saw ur post, i thought of posting it. I have added the feature to skip comments or blank lines also if u need itCode:#!/bin/sh # # Script for getting input from file # SCRIPTFILE=inputfile.txt #This function will tell us if the line has a comment or #blank line and need to be skipped - $1 is the line in the file #Returns 1 indicates skip the line else dont skip CanLineBeSkipped() { echo $cmd_line | grep "^#" > /dev/null if [ $? == 0 ] then return 1 fi if [ "$cmd_line" == "" ] then return 1 fi return 0 } # reading line by line from the file while read cmd_line do # check whether the line can be skipped CanLineBeSkipped $cmd_line if [ $? == 1 ] then continue fi read NAME USR USERID GROUPS PASS << EOF $(echo $cmd_line) EOF echo "$NAME $USR $UID $GROUPS $PASS" done < $SCRIPTFILE
hope it is useful for anyone
- 09-24-2005 #9Linux Newbie
- Join Date
- May 2005
- Location
- Chennai,TamilNadu, India
- Posts
- 141
Please make the following changes int my code in the corresponding lines
Code:read NAME USR USERID GROUPNAME PASS << EOF
Since i had tried with UID and GROUPS first , instead of USERID and GROUPNAMECode:echo "$NAME $USR $USERID $GROUPNAME $PASS
For using UID it gave me an error, and for using GROUPS it was printing zero as output whe echoing it.
when i changed it to GROUPNAME it worked
- 09-24-2005 #10Linux Newbie
- Join Date
- May 2005
- Location
- Chennai,TamilNadu, India
- Posts
- 141
This was the error i got whe i used UID. It printed the other variables
./test1.sh: line 41: UID: readonly variable
sharon hello 0 how pass
./test1.sh: line 41: UID: readonly variable
jomy hello 0 how pass


Reply With Quote
