Solved

useradd from file using while loop : Linux Script

Posted on 2013-11-05
19
1,110 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
squid3 ntlm and itunes authentication 5 78
Lame BIND 9.3 10 58
Strongswan setup to Cisco ASA Site to site 5 322
php54-php-imap for redhat enterprise linux 7.2 1 73
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…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

10 Experts available now in Live!

Get 1:1 Help Now