Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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.
  1. #1
    Just 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.

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035

    Re: automated account creation shell script (red hat Linux 9, bourne/bash)

    Quote Originally Posted by godmachine667
    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?
    You'd want:
    Code:
    read foo < /home/dan/datafilekthxbye.txt
    And then reference foo as
    Code:
    $foo
    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?
    The above solution solves this.

    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

  3. #3
    Just 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.)

    Code:
    line 1&#58; dan roth &#40;goes in -c argument&#41; droth &#40;username&#41; 243b5234 &#40;password&#41;
    .. all this in one line..

    so, to read the data off of the line should I do something like this:

    Code:
    read foo fee fer  < /home/dan/datafilekthxbye.txt
    and if I were to echo these variables out would get:

    Code:
     dan roth &#40;would this be $foo&#41; droth &#40;would this be $fee&#41; 243b5234 &#40;and would this be $fer?&#41;
    and would I have to use delimiters?

    thanks in advance.

  4. #4
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Yes they would get stored in the respective variables.

  5. #5
    Just 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.

  6. #6
    scm
    scm is offline
    Linux 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).
    Code:
    cat file | while read var1 var 2 var3 var4
    do
         # Do your stuff for each line in here
    done
    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).

  7. #7
    Just 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.

    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
    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.

    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.

  8. #8
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    hi

    Code:
    #!/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&#40;&#41;
    &#123;
    	echo $cmd_line | grep "^#" > /dev/null
    	    if &#91; $? == 0 &#93;
    	    then
    	        return 1
    	    fi
    	
    	    if &#91; "$cmd_line" == "" &#93;
    	    then
    	        return 1
    	    fi
    	
        return 0
    &#125;
    
    # reading line by line from the file
    
    while read cmd_line
    do
    # check whether the line can be skipped
    
    	CanLineBeSkipped $cmd_line
    	if &#91; $? == 1 &#93;
    	then 
    		continue
    	fi
    	
    	read NAME USR USERID GROUPS PASS << EOF
    	$&#40;echo $cmd_line&#41;
    EOF
    	echo "$NAME $USR $UID $GROUPS $PASS"
    done < $SCRIPTFILE
    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 it

    hope it is useful for anyone

  9. #9
    Linux 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
    Code:
     echo "$NAME $USR $USERID $GROUPNAME $PASS
    Since i had tried with UID and GROUPS first , instead of USERID and GROUPNAME

    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

  10. #10
    Linux 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

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •