views:

40

answers:

3

I wrote this sh script here. What it suppose to be doing is it prompts the user to type in the old password, then checks the password with the password in the "PASSWORD.txt" file if not it would exit, else if it matches then it would ask the user to type in the new password twice. Then it checks if the two new passwords are the same if not it would exit, else i should put the input the user typed and replace the text in the "PASSWORD.txt" file.

Then when i ran the file where it askes me for the old password i got this error:

Please Enter teh old passsword:
test
cat: .txt: No such file or directory
The password doesn't match![root@guzzy ~]#

The thing is the input i typed doesn't match even though i typed the correct old password.

Here is the scirpt below:

#!/bin/sh

clear

echo -e "Please Enter the old password:"

read old

if [ "$old" != "$(cat $PASSWORD.txt)" ]

    then

        echo -n "The password doesn't match!"

        exit

    else

        echo -n "The old password matches!"

        echo -n "Please Enter New password:"

        read new1

        echo -n "Please Enter New password again:"

        read new2

        if [ "$new1" != "$new2" ]

            then

                echo -n "The new passwords don't match!"

                exit

            else

                $new1 >> PASSWORD.txt

                echo -n "The new password has been saved!"

        fi
fi

Please help thanks!

+1  A: 

You haven't set the PASSWORD variable somewhere. If your file is named 'PASSWORD.txt', remove the $ before it.

Lekensteyn
+1  A: 

It think you meant

if [ "$old" != "$(cat PASSWORD.txt)" ]

without the dollar sign.

Bobby
+2  A: 

This line:

$new1 >> PASSWORD.txt

should be like this:

echo "$new1" > PASSWORD.txt

You need to echo the value into the file. I'm assuming that you don't want to keep old values. In order to be able to run your script again on the same file, you should probably overwrite (>) rather than append (>>).

Dennis Williamson
thanks but what if i want to overwrite the text. lets say if the old password is "test" in the PASSWORD.txt and i want to replace the old password with "hello" in the PASSWORD.txt file how do i do that? thanks
GuzzyD
ohh ok thanks! sorry didnt read the last bit...
GuzzyD