Find the answer to your Linux question:
Results 1 to 3 of 3
Hello. I am trying to make this shell script bellow work on my server wich should take the names in newacc.cvs and add them to the system. For each user ...
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    2

    shell script problem

    Hello. I am trying to make this shell script bellow work on my server wich should take the names in newacc.cvs and add them to the system. For each user the script should ask me to enter a password for the user im adding and then add them to the system, however my current solution do not work atm as when i run the script it dont stop to let me enter a password at all, it just runs past it :/ do someone know how to make this work?

    Example from newacc.cvs

    maria;jones
    andreas;öring

    Script

    Code:
    #!/bin/sh
    
    if [ "$#" == "0" ]
    then
    
    cat newacc.cvs | while IFS=';'  read firstname surname;
    do
    
        echo "user $firstname $surname har been added with the password $text"
        useradd $firstname -m
        echo  added $firstname to the system
        passwd $firstname
        cd /home/$firstname/
        maildirmake.courier Maildir
        echo Maildir Created for $firstname
    
    
    done
      exit 0

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You should be much more careful when creating scripts like this. For instance what happens if the file contains a user that already exists on the system? In your script

    Code:
    useradd $firstname -m
    Will throw up an error and then the rest of the script will go on to modify the users password/directories
    Linux User #453176

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    53

    Talking your script need more!

    I am figurating you are trying to build a file in oocalc with the users information and then do it with the script.

    Now you should build another one that erase them from the system.

    I would do it something like this

    Code:
    #! /bin/bash
    
    for item in `cat $1`;do
    firstname=`echo $item| awk -F";" '{print $1}'`
    surname=`echo $item| awk -F";" '{print $2}'`
    ifexist=`cat /etc/passwd | grep $firstname | wc -l`
    if let "ifexist==0";then
    # Adding the username to the db system
    useradd -c $firstname" "$surname -m $firstname
    echo "Type a password for "$firstname" User"
    passwd $firstname
    cd /home/$firstname/
    maildirmake.courier Maildir
    echo "Maildir Created for "$firstname
    else
    echo "The user "$firstname" is already in the system"
    fi
    done
    It just do what you are asking there are more things to watch but is a simple idea what you want.

    You must run the code like this:
    Code:
    # bash program.sh newacc.cvs
    Last edited by rojoblandino; 12-03-2009 at 10:20 PM. Reason: fixing post

Posting Permissions

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