Shell Script Password Prompt

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data: I am trying to write a shell script that prompts the user for the password which is "lux" once the correct password has been typed it then needs to list files and folders in the home directory and save it to a text file. If the user types the password wrong 3 times it displays a message and closes the terminal window. If the password was typed correctly after it lists the files and folders in home directory it then needs to ask the name of the user and it inserts it and the date in the first and second lines of the file.

At the end it shows a message about being done with the process.

  1. Relevant commands, code, scripts, algorithms: This is what I have so far but I am stuck I get a error message " unexpected error at ln 9" after I type the password.

  2. The attempts at a solution (include all code and scripts):

PASS="lux"

read -p "Password: " mypassword

echo ""

[ "$mypassword" == "$PASS" ] ; then

echo "Password Accepted"

failed=$(( $failed + 3 )) ; then

echo "Incorrect Password" 
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Baker College, Clinton Twp, MI, Mr. Ansari, LUX211

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Your if-statement lacks a fi. They are supposed to work like this:

if [ ... ]
then
...
fi

To test several times, you will need a loop.

while [ "$failed" -lt 3 ]
do
...
done

I got the first part to work accepting the password and listing all the files in the current directory. The one I am having trouble is with "while" loop.

What command would I use to display that the user has entered the incorrect password and only having 3 tries to do it and then close the terminal.

PASS="lux"

read -p "Password: " mypassword

echo ""

if [ "$mypassword" = "$PASS" ] 

then

echo "Password Accepted" 

ls -l 

fi 

Ro make things easy to read, stick to one standard e.g. if you start with variables in UPPERCASE stick to it! That said that is what was the standard...
We like to say as first line something like:

#!/bin/sh
.
.

We then all know what shell is used for your script, which could be different then the one you use when connecting...

So

#!/yourshell
#
# Document  a little what the script is/for...
#

PASS="lux"
PASSCPT=0                           #  You need a counter for your loop...
#
# A little analyse of what is asked suggests you have a few ways to meet requirement
# You have to decide what you are to do:
# e.g. Load a string variable  with the reason of the prompt ( enter a passwd ...) can 
# be an elegant solution for it allows you to  also display how man chances are left at each loop execution...
#

read -p "Password: " MYPWD
echo ""

if [ "$MYPWD" = "$PASS" ] 
then
   echo "Password Accepted" 
   ls -l 

fi 

And whatabout:

?

IF I understood correctly the end of your requirement: you need it display AND copying output in a file then you will have to look at the command tee...
The best for you to figure out what it to be done is write in pseudocode how the script should work, submitting here we would help and correct to get you doing what you are asked but dont expect we give you the solution ( even if we are tempted.. haha Its hard for us to not and figure out what/what not to post...)
Your script should be divided in 2 paragraphs:

# Initialize your variableis:
MYPWD=""
PASS="lux"
PASSCPT=0
.
# Testing condition:
While ..
# 2 possibilities: while passwd is not good OR $PASSCPT -lt 3
# I let you decide... one of course is more legeant than the other...
# Remember that if codition is met to get out by some means 
# e.g using break in a if condition...
..
done

#Post testing treatment # You should enter here only if required condition are met!

ls- l 
.
#Ask username...
# write at end of generated file
End

Waiting to see your new submission!

Courage!

---
ADDENDUM

Result of my little test:

ant01:/home/vbe/script/test/zz/001 $ ./pwdcheck01 
Please enter the correct passwd, you have 3 tries before exit. you have now: 3  tries left.
Password: 
Please enter the correct passwd, you have 3 tries before exit. you have now: 2  tries left.
Password: 
Please enter the correct passwd, you have 3 tries before exit. you have now: 1  tries left.
Password: 
 3 bad attemps, exiting... 
ant01:/home/vbe/script/test/zz/001 $ ./pwdcheck01
Please enter the correct passwd, you have 3 tries before exit. you have now: 3  tries left.
Password: 
Please enter the correct passwd, you have 3 tries before exit. you have now: 2  tries left.
Password: 
Please enter the correct passwd, you have 3 tries before exit. you have now: 1  tries left.
Password: 
 3 bad attemps, exiting... 
ant01:/home/vbe/script/test/zz/001 $ ./pwdcheck01
Please enter the correct passwd, you have 3 tries before exit. you have now: 3  tries left.
Password: 
Please enter the correct passwd, you have 3 tries before exit. you have now: 2  tries left.
Password: lux
Password Accepted after 2  attempt
total 56
drwxrwxr-x    4 vbe      staff         4096 Jun 04 17:30 .
drwxr-xr-x    3 vbe      bin             256 Jun 03 15:52 ..
drwxrwxr-x    2 vbe      staff          256 Jun 03 15:54 002
-rw-rw-r--    1 vbe      staff            0 Jun 04 17:30 check_test001.out
drwxrwxr-x    2 vbe      staff          256 Jun 04 16:34 dir02
-rw-rw-r--    1 vbe      staff          115 Jun 03 15:53 find_sh001
-rwxr-x---    1 vbe      staff         1436 Jun 04 15:12 pwdcheck
-rwxr-x---    1 vbe      staff         2105 Jun 04 17:00 pwdcheck01
-rw-rw-r--    1 vbe      staff            0 Jun 03 15:55 register.txt
-rw-rw-r--    1 vbe      staff         5049 Jun 03 15:55 regular_expr.howto
-rw-rw-r--    1 vbe      staff          867 Jun 03 15:53 rm_sed001
 As you have succeeded, please give your user name :  ^C
ant01:/home/vbe/script/test/zz/001 $ ./pwdcheck01
Please enter the correct passwd, you have 3 tries before exit. you have now: 3  tries left.
Password: lux
Password Accepted after 1  attempt
total 56
drwxrwxr-x    4 vbe      staff         4096 Jun 04 17:30 .
drwxr-xr-x    3 vbe      bin             256 Jun 03 15:52 ..
drwxrwxr-x    2 vbe      staff          256 Jun 03 15:54 002
-rw-rw-r--    1 vbe      staff            0 Jun 04 17:30 check_test001.out
drwxrwxr-x    2 vbe      staff          256 Jun 04 16:34 dir02
-rw-rw-r--    1 vbe      staff          115 Jun 03 15:53 find_sh001
-rwxr-x---    1 vbe      staff         1436 Jun 04 15:12 pwdcheck
-rwxr-x---    1 vbe      staff         2105 Jun 04 17:00 pwdcheck01
-rw-rw-r--    1 vbe      staff            0 Jun 03 15:55 register.txt
-rw-rw-r--    1 vbe      staff         5049 Jun 03 15:55 regular_expr.howto
-rw-rw-r--    1 vbe      staff          867 Jun 03 15:53 rm_sed001
 As you have succeeded, please give your user name :  ^C
ant01:/home/vbe/script/test/zz/001 $ ./pwdcheck01
Please enter the correct passwd, you have 3 tries before exit. you have now: 3  tries left.
Password: 
Please enter the correct passwd, you have 3 tries before exit. you have now: 2  tries left.
Password: 
Please enter the correct passwd, you have 3 tries before exit. you have now: 1  tries left.
Password: lux
Password Accepted after 3  attempt
total 56
drwxrwxr-x    4 vbe      staff         4096 Jun 04 17:31 .
drwxr-xr-x    3 vbe      bin             256 Jun 03 15:52 ..
drwxrwxr-x    2 vbe      staff          256 Jun 03 15:54 002
-rw-rw-r--    1 vbe      staff            0 Jun 04 17:31 check_test001.out
drwxrwxr-x    2 vbe      staff          256 Jun 04 16:34 dir02
-rw-rw-r--    1 vbe      staff          115 Jun 03 15:53 find_sh001
-rwxr-x---    1 vbe      staff         1436 Jun 04 15:12 pwdcheck
-rwxr-x---    1 vbe      staff         2105 Jun 04 17:00 pwdcheck01
-rw-rw-r--    1 vbe      staff            0 Jun 03 15:55 register.txt
-rw-rw-r--    1 vbe      staff         5049 Jun 03 15:55 regular_expr.howto
-rw-rw-r--    1 vbe      staff          867 Jun 03 15:53 rm_sed001
 As you have succeeded, please give your user name :  Tsadf jkh Osfak
File check_test001.out  User name :   Tsadf jkh Osfak
Tue Jun  4 17:31:18 CEST 2013
total 56
drwxrwxr-x    4 vbe      staff         4096 Jun 04 17:31 .
drwxr-xr-x    3 vbe      bin             256 Jun 03 15:52 ..
drwxrwxr-x    2 vbe      staff          256 Jun 03 15:54 002
-rw-rw-r--    1 vbe      staff            0 Jun 04 17:31 check_test001.out
drwxrwxr-x    2 vbe      staff          256 Jun 04 16:34 dir02
-rw-rw-r--    1 vbe      staff          115 Jun 03 15:53 find_sh001
-rwxr-x---    1 vbe      staff         1436 Jun 04 15:12 pwdcheck
All processing done, file generated is check_test001.out: 
-rw-rw-r--    1 vbe      staff          837 Jun 04 17:31 check_test001.out
ant01:/home/vbe/script/test/zz/001 $