Skip to content
Advertisement

Reading username and password from file

I’m looking at a away of reading the username and password of a file and inputing those to either add user or delete user.

EG: I have a file named ‘userlist’ with the following content with this format:

user1 pass1
user2 pass2
user3 pass3

What I don’t understand completely is how to use BASH script to add these accounts.

What I have so far is this:

if [[ whoami -ne "root" ]]
then
exit
else
echo "wish to add or delete? a/d"
read uArg
echo "enter file name"
read uFile
if [ $uArg = "a" -o $uArg = "A" ]
then
    IDK WHAT TO DO HERE.
elif [ $uArg = "d" -o $uArg = "D" ]
then
   IDK WHAT TO DO HERE.
fi
fi

Alright, what I don’t understand is how to read each word line by line and input the username and password to add a new user or delete an existing user.

The program is ment to read the whole file and add each user with there corrosponding password. If delete is chosen then it deletes each user within the file.

I’m new to BASH so any help would be greatyl appreciated.

Advertisement

Answer

awk perfectly feets your needs.

See this example:

$ awk '{print "Hi! my name is " $1 ", and my pass is " $2}' ./userpass.txt 
Hi! my name is user1, and my pass is pass1
Hi! my name is user2, and my pass is pass2
Hi! my name is user3, and my pass is pass3

Awk stores usernames in $1 and passwords in $2 (first and second column).

You can use pipelines to execute the strings you get from awk as commands:

$ awk '{print "echo " $1}' ./userpass.txt | /bin/bash
user1
user2
user3
Advertisement