Solved

useradd from file using while loop : Linux Script

Posted on 2013-11-05
19
1,085 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now