• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1436
  • Last Modified:

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")
{
}
0
Mohammad Aamir Qureshi
Asked:
Mohammad Aamir Qureshi
  • 8
  • 6
  • 4
  • +1
1 Solution
 
Peter KwanCommented:
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
 
savoneCommented:
for i in `cat text | awk -F: '{print $2}'`; do
echo $i
if [ "$i" = "EOF" ]; then
       exit
fi
       useradd $i
done
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
savoneCommented:
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
 
Mohammad Aamir QureshiAuthor Commented:
Thanks for savone
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 8
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now