Solved

useradd from file using while loop : Linux Script

Posted on 2013-11-05
19
1,243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

627 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