Find the answer to your Linux question:
Results 1 to 3 of 3
okey here is my problem. i have users i need to add from a text file. when i run the bash script it just hangs. like it is stuck in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    10

    adding users from file


    okey here is my problem. i have users i need to add from a text file. when i run the bash script it just hangs. like it is stuck in a loop. and does nothing. any help would be greatly appriciated.

    Code:
    #!/bin/bash
    	ROOT_UID=0      #The root user has a UID of 0
    	if      [ "$UID" -ne "$ROOT_UID" ]; then
            	echo "****You must be the root user to run this script!****"
            	exit
    	fi
    
    	echo
    	echo Identity Verified
    	echo
    
    	echo
    	echo "Present working directory is: `pwd`"
    	echo
    	echo -n "Please enter the correct path to the file (e.g. /root/Aaron/w2n1.txt): "
    	read Path
    	if [ -e $Path ]; then                                   #If file user specified exists
    		Year=`grep "Year" $path | cut -f2 -d:`          #Extract year from text file
    
    		Group=`grep "Group" $path | cut -f2 -d:`        #Extract group from text file
    
    
            	if [ -e ./$Group-mysql.sql ]; then      #Check if mysql file already created
            	echo >> ./$Group-mysql.sql
    	else
            	Write the beginning of the MySQL setup script
            	echo "######################################################" > ./$Group-mysql.sql
            	echo "#         script to create users database XXX_test   #" >> ./$Group-mysql.sql
            	echo "#         grant each user a password                 #" >> ./$Group-mysql.sql
            	echo "#                                                    #" >> ./$Group-mysql.sql
            	echo "######################################################" >> ./$Group-mysql.sql
    
    	fi
    	tail -n +9 $Path > tempnames.txt                #Send lines containing user details to temp file
           	head -n 9 $Path > $Group-dup.txt                #Send start of file to duplicates file
            while read Nextline
            do
                            Firstname=`echo $Nextline | cut -f1`        #Cut relevant information from list
                            Surname=`echo $Nextline | cut -f2`          #and assign it to appropriate variable
                            Username=`echo $Nextline | cut -f3`
                            Password=`echo $Nextline | cut -f4`
                            SambaPW=`echo $Nextline | cut -f5`
                            MySQLPW=`echo $Nextline | cut -f6`
           
    	Adduser()               #Adds user to the system and gives them a password
            {
                    #Adds user to the system
                    useradd -c "$Firstname $Surname" -g $Group -d /home/$Year/$Group/$Username -m $Username
                    #Add users password
                    echo $Password | /usr/bin/passwd --stdin $Username
    
                    #Creates Samba account
                    smbpasswd -a $Username $SambaPW
            }
            
    	Webpage()               #Creates a web page for user in the ~/public_html directory
            {
                    #Create directory
                    mkdir -p /home/$Year/$Group/$Username/public_html
                    touch /home/$Year/$Group/$Username/public_html/index.html
                    #Create web page index.html
                    echo "<html>" >> /home/$Year/$Group/$Username/public_html/index.html
                    echo "<head>" >> /home/$Year/$Group/$Username/public_html/index.html
                    echo "<title> $username's home page </title>" >> /home/$Year/$Group/$Username/public_html/index.html
                    echo "</head>" >> /home/$Year/$Group/$Username/public_html/index.html
                    echo "<body>" >> /home/$Year/$Group/$Username/public_html/index.html
                    echo "Welcome to the wep page of $firstname $surname. Enjoy the ride!" >> /home/$Year/$Group/$Username/public_html/index.html
                    echo "</body>" >> /home/$Year/$Group/$Username/public_html/index.html
                    echo "</html>" >> /home/$Year/$Group/$Username/public_html/index.html
    
                    #Change permissions and ownerships on neccessary folders
                    chmod 701 /home/$Year/$Yroup/$Username                         #Users folder
                    chmod 701 /home/$Year/$Yroup/$Username/public_html             #public_html folder
                    chmod 705 /home/$Year/$Group/$Username/public_html/index.html  #web page file
                    chown $Username /home/$Year/$Group/$Username/public_html
                    chown $Username /home/$Year/$Group/$Username/public_html/index.html
            }

  2. #2
    Just Joined!
    Join Date
    Aug 2005
    Posts
    10
    wouldn't let me post more then 10,000 characters so i will have to continue it on here

    Code:
    	
    	Mysql()                 #creates MySQL user setup script w2n1-mysql.sql
            {
            echo >> ./$Group-mysql.sql
            echo "#************** user $Firstname $Surname" >> ./$Group-mysql.sql
            echo "#************** username $Username" >> ./$Group-mysql.sql
            echo "# quick check" >> ./$Group-mysql.sql
            echo "FLUSH PRIVILEGES;" >> ./$Group-mysql.sql
            echo -n "DROP DATABASE IF EXISTS $Username" >> ./$Group-mysql.sql
            echo "_test;" >> ./$Group-mysql.sql
            echo -n "CREATE DATABASE $Username" >> ./$Group-mysql.sql
            echo "_test;" >> ./$Group-mysql.sql
            echo -n "GRANT ALL ON $Username" >> ./$Group-mysql.sql
            echo "_test.* TO $Username@localhost IDENTIFIED BY '$MySQLPW';" >> ./$Group-mysql.sql
            echo -n "GRANT ALL ON $Username" >> ./$Group-mysql.sql
            echo "_test.* TO $Username@'%' IDENTIFIED BY '$MySQLPW';" >> ./$Group-mysql.sql
            }
    
            if [ "$Usercheck" == "$Username" ]; then                #If user already exists
    
                    echo "$Firstname,$Surname,$Username,$Password,$SambaPW,$MySQLPW" >> $Group-dup.txt
    
            else                                                            #If username Ok
                    if [ "$Yearcheck" != "$Year" ]; then                    #if year does not exist
                            mkdir -p /home/$Year                            #Make year directory
                            if [ "$Groupcheck" != "$Group" ]; then  #if group does not exist automatically
                                    groupadd $Group                 #create the group
                                    mkdir -p /home/$Year/$Group     #and the group directory
                                    adduser                         #adduser function
                                    webpage                         #webpage function
                                    mysql                           #mysql function
                                    echo
                                    echo "User $Username added successfully"
                                    echo
                            else                                    #if group does exist
                                    Groupcheck2=`ls -l /home/$Year | grep -m 1 $Group | cut -f24 -d" "` #See if group dir exist
                                    if [ "$Groupcheck2" != "$Group" ]; then     #if group directory does not exist
                                            mkdir -p /home/$Year/$Group        #create the group directory
                                            adduser                         #adduser function
                                            webpage                         #webpage function
                                            mysql                           #mysql function
                                            echo
                                            echo "User $Username added successfully"                                        echo
                                    else                                           #if group directrory does exist
                                            #adduser function
                                            adduser
                                            #webpage function
                                            webpage
                                            #mysql function
                                            mysql
                                            echo
                                            echo "User $Username added successfully"                                        echo
                                    fi
                            fi
                    else                                                           #if year does exist
                            if [ "$Groupcheck" != "$Group" ]; then                 #if group does not exist
                                    groupadd $Group                                #create group
                                    mkdir -p /home/$Year/$Group                    #create group directory
                                    adduser                                 #adduser function
                                    webpage                                 #webpage function
                                    mysql                                   #mysql function
                                    echo
                                    echo "User $Username added successfully"
                                    echo
    
                            else                                                   #if group does exist
                                    Groupcheck2=`ls -l /home/$Year | grep -m 1 $Group | cut -f24 -d" "` #See if group dir exist
                                    if [ "$Groupcheck2" != "$Group" ]; then     #if group directory does not exist
                                            mkdir -p /home/$Year/$Group            #create the group directory
                                            adduser                         #adduser function
                                            webpage                         #webpage function
                                            mysql                           #mysql function
                                            echo
                                            echo "User $Username added successfully"                                        echo
    
                                    else                                           #if group directrory does exist
                                            #adduser function
                                            adduser
                                            #webpage function
                                            webpage
                                            #mysql function
                                            mysql
                                            echo
                                            echo "User $Username added successfully"                                        echo
                                    fi
                            fi
                    fi
    
            fi
    
    
    
                    done < tempnames.txt                    #get information from tempnames.txt file
    
                    rm -f ./tempnames.txt                   #deletes the tempnames file
                    echo
                    echo "Press <ENTER> to continue"
                    read -s enter
                    clear
                    ./addusers.sh
    
    else                                    #If file user entered does not exist
            echo "The filename you have entered does not exist!"
            echo "Press <ENTER> to continue"
            read -s Enter
            clear
            ./addusers.sh
    fi
    and here is the text file
    Code:
    	Swinburne Tafe Student Account
    	==============================
    Filename:w2n1.txt
    Year:2006
    Campus:Wantirna
    Group:w2n1
    
    Firstname	Secondname	Username	Password	SambaPW		MySQLPW
    =========	===========	========	========	========	========	
    John		Lennon		johnl		123456		123456 		123456
    Paul		McCartney	paulm		987654		987654		987654
    George		Harrison	georgeh		135791		135791		135791
    Ringo		Star		ringos		246810		246810		246810
    Mick		Jagger		mick j		675843		675843		675843
    Keith		Richards	keithr		112233		112233		112233
    Charlie		Watts		charliew	667788		667788		667788
    Ron		Wood		ronw		879867		879867		879867
    Bill		Wyman		billw		987655		987655		987655

  3. #3
    Linux User muha's Avatar
    Join Date
    Jan 2006
    Posts
    290
    try saving the script as small bits and troubleshooting per bit.
    It also doesn't echo 'Identity Verified' ?
    Now what? You have Linux installed and running. The GUI is working fine, but you are getting tired of changing your desktop themes. You keep seeing this "terminal" thing. Don't worry, they'll show you what to do @
    <~ http://www.linuxcommand.org/ ~>

  4. $spacer_open
    $spacer_close

Posting Permissions

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