Solved

useradd from file using while loop : Linux Script

Posted on 2013-11-05
19
1,137 Views
Last Modified: 2013-11-06
Hello
My text file look like
1:Abc
2:Abd
3:EOF

How can I add the user from text file using while loop and how to read just username

while ($username != "EOF")
{
}
0
Comment
  • 8
  • 6
  • 4
  • +1
19 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 39626498
Does your 1:, 2:, 3: are actually inside the content of the file?

If no, you may try the following:

#!/bin/bash

while read LINE 
do
        if [ $LINE = "EOF" ]; then
                break
        fi
        useradd $LINE
done < $1

Open in new window


$1 is your text file name.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39626513
try this

awk -F":" filename '{ print $2 } | while read username
do
if $username == 'EOF'
then
exit
fi
useradd $username
done
0
 
LVL 23

Expert Comment

by:savone
ID: 39627136
for i in `cat text | awk -F: '{print $2}'`; do
echo $i
if [ "$i" = "EOF" ]; then
       exit
fi
       useradd $i
done
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 2

Author Comment

by:Mohammad Aamir Qureshi
ID: 39627167
Please read my question carefully. I AM ASKING FOR WHILE loop
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39627385
Did you try the solution given by me (ID: 39626513) above?
0
 
LVL 23

Expert Comment

by:savone
ID: 39628817
Sorry, try this:

awk -F":" '{ print $2 }' textfile.txt | while read username
do
if [ "$username" = "EOF" ]; then
        exit
fi
useradd $username
done
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39629191
Thank you savone for correcting my script :)

I put == rather =
0
 
LVL 23

Expert Comment

by:savone
ID: 39629203
You also left out a ` and a couple of [ ]...
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39629235
Thanks
0
 
LVL 2

Author Comment

by:Mohammad Aamir Qureshi
ID: 39629276
Sorry friends
I am new in Linux scripting

I have problem with reading files and receive that error

awk: cmd. line:1: fatal: cannot open file `userfiles.txt' for reading (No such file or directory)
0
 
LVL 23

Expert Comment

by:savone
ID: 39629291
That means it can not find your file.  Did you create a file called userfiles.txt?  If so where is it?  Try using the full path in the script like this:

awk -F":" '{ print $2 }' /path/to/userfiles.txt | while read username
do
if [ "$username" = "EOF" ]; then
        exit
fi
useradd $username
done
0
 
LVL 2

Author Comment

by:Mohammad Aamir Qureshi
ID: 39629297
File of path is Desktop/Assignment/userfiles.txt and I am login as root
I have created the file
0
 
LVL 2

Author Comment

by:Mohammad Aamir Qureshi
ID: 39629301
This is my code

awk -F":" '{ print $2 }' /Desktop/Assignment/userfiles.txt | while read username
do
if [ "$username" = "EOF" ]; then
        exit
fi
useradd $username
passwd $username
done


awk: cmd. line:1: fatal: cannot open file `/Desktop/Assignment/userfiles.txt' for reading (No such file or directory)
0
 
LVL 2

Author Comment

by:Mohammad Aamir Qureshi
ID: 39629304
POINTS INCREASE 235 to 500
0
 
LVL 23

Expert Comment

by:savone
ID: 39629321
ok, try this path:

/root/Desktop/Assignment/userfiles.txt

Also, try this command and post the output:

cat /root/Desktop/Assignment/userfiles.txt

If it does not print out the file then that means the file is not there.
0
 
LVL 2

Author Comment

by:Mohammad Aamir Qureshi
ID: 39629337
All done
how can I add the password (should be same as username)
see my code above
0
 
LVL 2

Author Comment

by:Mohammad Aamir Qureshi
ID: 39629339
and If user already exist then it should not add the user
Need script for this
Sorry to disturb you again and again savone
0
 
LVL 23

Accepted Solution

by:
savone earned 500 total points
ID: 39629347
useradd will not add a user if it already exists, it will just error out.

Use this code to add the password.  Although this is frowned upon (using the same password as username) and some systems might reject the password telling you it is using the username.

#!/bin/bash
awk -F":" '{ print $2 }' /Desktop/Assignment/userfiles.txt | while read username
do
echo "adding $username"
if [ "$username" = "EOF" ]; then
        exit
fi
useradd $username
echo -e "$username\n$username" | passwd $username
done

Open in new window

0
 
LVL 2

Author Closing Comment

by:Mohammad Aamir Qureshi
ID: 39629366
Thanks for savone
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert Routes to Linux (CENTOS 7.0) 3 99
Guacamole and browser performance 1 87
dns master and slave in linux 2 69
Linux Login using LDAP or Active Directory 4 127
I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question