useradd from file using while loop : Linux Script

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")
{
}
LVL 2
Mohammad Aamir QureshiAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
savoneConnect With a Mentor Commented:
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
 
Peter KwanAnalyst ProgrammerCommented:
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
 
omarfaridCommented:
try this

awk -F":" filename '{ print $2 } | while read username
do
if $username == 'EOF'
then
exit
fi
useradd $username
done
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
savoneCommented:
for i in `cat text | awk -F: '{print $2}'`; do
echo $i
if [ "$i" = "EOF" ]; then
       exit
fi
       useradd $i
done
0
 
Mohammad Aamir QureshiAuthor Commented:
Please read my question carefully. I AM ASKING FOR WHILE loop
0
 
omarfaridCommented:
Did you try the solution given by me (ID: 39626513) above?
0
 
savoneCommented:
Sorry, try this:

awk -F":" '{ print $2 }' textfile.txt | while read username
do
if [ "$username" = "EOF" ]; then
        exit
fi
useradd $username
done
0
 
omarfaridCommented:
Thank you savone for correcting my script :)

I put == rather =
0
 
savoneCommented:
You also left out a ` and a couple of [ ]...
0
 
omarfaridCommented:
Thanks
0
 
Mohammad Aamir QureshiAuthor Commented:
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
 
savoneCommented:
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
 
Mohammad Aamir QureshiAuthor Commented:
File of path is Desktop/Assignment/userfiles.txt and I am login as root
I have created the file
0
 
Mohammad Aamir QureshiAuthor Commented:
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
 
Mohammad Aamir QureshiAuthor Commented:
POINTS INCREASE 235 to 500
0
 
savoneCommented:
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
 
Mohammad Aamir QureshiAuthor Commented:
All done
how can I add the password (should be same as username)
see my code above
0
 
Mohammad Aamir QureshiAuthor Commented:
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
 
Mohammad Aamir QureshiAuthor Commented:
Thanks for savone
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.