Exercise 3

  1. Unix Shell Scripting
  2. Shell Basics
  3. Testing in bash
  4. Capturing User Input
  5. Scripting : Exercise 1
  6. Debugging
  7. Bourne/Bash/Korn Commands
  8. Shell Variables
  9. IO Redirection
  10. Pipes
  11. Operators, Wildcards and Expressions
  12. Flow Control
  13. Scripting : Exercise 2
  14. Shell Differences
  15. String Functions
  16. awk
  17. xargs
  18. Power Tools
  19. Exercise 3

Now it’s time to assemble the tinker-toys you’ve been learning to use.

1. Create a new script named ~/bin/userlogin.

2. Create two log files: ~/success.log and ~/failure.log

3. Create a password file, ~/userpasswords, and populate it with at least one tab-separated user name and password: user1 and pword.

4. Create variables in userlogin to hold the log file names.

5. When it’s run, userlogin must prompt the user for a user name, password and department. Use echo and read.

6. Use the USER environment variable to capture the user’s actual SYSTEM name.

7. Use grep to check that the user name is in the userpasswords file.

8. Use grep to check that the password is in the userpasswords file.

How can you be sure the user name and password are on the same line?

9. Use redirection to append successful logins to success.log. Log the user name, password and department, as well as the date and time.

10. Append unsuccessful logins to failure.log. Capture the same information.

11. Send a message to the terminal indicating whether the login operation was successful or failed.

12. Now, deliberately provoke an error with a faulty command (try “cat foo”). Capture the error output and send it to failure.log.

13. Call the script and perform successful and unsuccessful logins. Check your log files. What kind of feedback should you be getting? Modify your script as necessary.

14. Place the script and the log files in a directory from which it can be called by anyone. What are the pros and cons of different locations?

15. Check the log files. Can you read them?