Reading username and password from file

2019-07-19 22:46发布

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.

3条回答
成全新的幸福
2楼-- · 2019-07-19 23:15

First comments:

  • learn and get used to basic commands like grep, sed, echo, and generally with file manipulation, awk is a good choice as well if you want to know bash basics, a lot you will encounter is about file manipulation
  • the code could use more error testing, it's just a basic skeleton
  • careful about string and variables, quote them whenever possible, spaces in strings can do a lot of bad

Could be something along these lines:

echo "wish to add or delete? a/d"
read uArg
echo "enter username"
read uName
grep "^$uName " password-file
RET=$?

if [ "$uArg" == "a" -o "$uArg" == "A" ]
then
    [ $RET -eq 0 ] && echo "User is already in file"

    if [ $RET -ne 0 ]
    then
        echo "enter password"
        read uPass
        echo "$uName $uPass" >> password-file
    fi

elif [ "$uArg" == "d" -o "$uArg" == "D" ]
then
    [ $RET -ne 0 ] && echo "User is not file"

    if [ $RET -eq 0 ]
    then
        sed -i "/^$uName /d" password-file
        echo "User deleted"

    fi
fi
查看更多
你好瞎i
3楼-- · 2019-07-19 23:28

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
查看更多
聊天终结者
4楼-- · 2019-07-19 23:28

something along the lines of...

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
        while read user passwd rest
        do
            if [ ! -z $rest ]; then
                echo "Bad data"
            else
                useradd -m $user
                passwd $user <<EOP
$passwd
$passwd
EOP
            fi
        done < $uFile
    elif [ $uArg = "d" -o $uArg = "D" ]
    then
        while read user passwd rest
        do
            if [ ! -z $rest ]; then
                echo "Bad data"
            else
                userdel $user
            fi
        done < $uFile
    fi
fi
查看更多
登录 后发表回答