Problem

In this hands-on project, you create a shell script that uses decision and loop construc...

In this hands-on project, you create a shell script that uses decision and loop constructs to analyze user input.

1. Switch to a command-line terminal (tty2) by pressing Ctrl+Alt+F2 and log in to the terminal using the user name of root and the password of secret.

2. At the command prompt, type vi myscript2 and press Enter to open a new file for editing called myscript2 in your home directory.

3. Enter the following text into the myscript2 file. When finished, save and quit the vi editor.

#!/bin/bash

echo –e "This program adds entries to a family database file.\n"

echo –e "Please enter the name of the family member --> \c"

read NAME

echo –e "Please enter the family member’s relation to you

(e.g., mother) --> \c"

read RELATION

echo –e "Please enter the family member’s telephone number --> \c"

read PHONE

echo –e "$NAME\t$RELATION\t$PHONE" >> database

4. At the command prompt, type chmod u+x myscript2 and press Enter. Next, type ./myscript2 at the command prompt and press Enter. Answer the questions with information regarding one of your family members.

5. At the command prompt, type cat database and press Enter. Was the entry from Step 4 present? Why?

6. Re-execute the myscript2 script (from Step 4) several times to populate the database file with entries.

7. At the command prompt, type vi myscript2 and press Enter. Edit the text inside the

myscript2 shell script so that it reads:

#!/bin/bash

echo –e "Would you like to add an entry to the family database file?\n"

read ANSWER1

if [ $ANSWER1 = "y" –o $ANSWER1 = "Y" ]

then

echo –e "Please enter the name of the family member --> \c"

read NAME

echo –e "Please enter the family member’s relation to you (i.e. mother)

--> \c"

read RELATION

echo –e "Please enter the family member’s telephone number --> \c"

read PHONE

echo –e "$NAME\t$RELATION\t$PHONE" >> database

fi

echo –e "Would you like to search an entry in the family database

file?\n"

read ANSWER2

if [ $ANSWER2 = "y" –o $ANSWER2 = "Y" ]

then

echo –e "What word would you like to look for? --> \c"

read WORD

grep "$WORD" database

fi

8. At the command prompt, type ./myscript2 and press Enter. When prompted to enter an entry into the database, choose y and press Enter. Answer the questions with information regarding one of your family members. Next, when prompted to search the database, answer y and press Enter. Search for the name that you just entered a few seconds ago. Is it there?

9. At the command prompt, type ./myscript2 and press Enter. When prompted to enter an entry into the database, choose n and press Enter. Next, when prompted to search the database, answer y and press Enter. Search for a name that you entered in Step 6. Was it there? Why?

10. At the command prompt, type vi myscript2 and press Enter. Edit the text inside the myscript2 shell script so that it reads:

#!/bin/bash

echo –e "What would you like to do?

Add an entry (a)

Search an entry (s)

Enter your choice (a/s)-->\c"

read ANSWER

case $ANSWER in

a|A ) echo –e "Please enter the name of the family member --> \c"

read NAME

echo –e "Please enter the family member’s relation to you

(i.e. mother)-->\c"

read RELATION

echo –e "Please enter the family member’s telephone number --> \c"

read PHONE

echo –e "$NAME\t$RELATION\t$PHONE" >> database

;;

s|S ) echo –e "What word would you like to look for? --> \c"

read WORD

grep "$WORD" database

;;

*) echo "You must enter either the letter a or s."

;;

esac

11. At the command prompt, type ./myscript2 and press Enter. Choose y and press Enter. What error message do you receive and why?

12. At the command prompt, type ./myscript2 and press Enter. Choose a and press Enter. Enter information about another family member. Does it matter whether you entered a or A at the prompt earlier? Why?

13. At the command prompt, type ./myscript2 and press Enter. Choose s and press Enter. Search for the family member entered in Step 12. Does it matter whether you entered s or S at the prompt earlier? Why?

14. At the command prompt, type vi myscript2 and press Enter. Edit the text inside the myscript2 shell script so that it reads:

#!/bin/bash

while true

do

clear

echo –e "What would you like to do?

Add an entry (a)

Search an entry (s)

Quit (q)

Enter your choice (a/s/q)-->\c"

read ANSWER

case $ANSWER in

a|A ) echo –e "Please enter the name of the family member --> \c"

read NAME

echo –e "Please enter the family member’s relation to you

(i.e. mother)-->\c"

read RELATION

echo –e "Please enter the family member’s telephone number --> \c"

read PHONE

echo-e "$NAME\t$RELATION\t$PHONE" >> database

;;

s|S ) echo-e "What word would you like to look for? --> \c"

read WORD

grep "$WORD" database

sleep 4

;;

q|Q ) exit

;;

*) echo "You must enter either the letter a or s."

sleep 4

;;

esac

done

15. At the command prompt, type ./myscript2 and press Enter. Choose a and press Enter. Enter information about another family member. Does the menu appear again after you were finished? Why? Choose s and press Enter. Search for the family member that you just entered. Choose q to quit the shell script.

16. At the command prompt, type vi myscript3 and press Enter to edit a new file called myscript3 in your home directory.

17. Enter the following text into the myscript3 file. When finished, save and quit the vi editor.

#!/bin/bash

echo –e "This program copies a file to the /stuff directory.\n"

echo –e "Which file would you like to copy? --> \c"

read FILENAME

mkdir /stuff || echo "The /stuff directory could not be created."

cp –f $FILENAME /stuff && echo "$FILENAME was successfully copied to

/stuff"

18. At the command prompt, type chmod u+x myscript3 and press Enter. Next, type ./myscript3 at the command prompt and press Enter. When prompted for a filename, type /etc/hosts and press Enter. Was the /stuff directory created successfully? Why or why not?Was the /etc/hosts file copied successfully to the /stuff directory? Why or why not?

19. Type ./myscript3 at the command prompt and press Enter. When prompted for a filename, type /etc/inittab and press Enter. Was the /stuff directory created successfully? Why or why not? Was the /etc/inittab file copied successfully to the /stuff directory? Why or why not?

20. At the command prompt, type vi myscript4 and press Enter to edit a new file called myscript4 in your home directory.

21. Enter the following text into the myscript4 file. When finished, save and quit the vi editor.

#!/bin/bash

echo "These are the scripts that you have created previously:"

ls –l myscript myscript2 myscript3

sleep 2

echo "This script will now change the permissions on each script

such that the root user has exclusive rights only."

sleep 3

for FILE in myscript myscript2 myscript3

do

chmod 700 $FILE

done

echo "The new permissions are listed below:"

ls –l myscript myscript2 myscript3

22. At the command prompt, type chmod u+x myscript4 and press Enter. Next, type./myscript4 at the command prompt and press Enter. Were the permissions changed to rwx------ for myscript, myscript2, and myscript3?

23. Type exit and press Enter to log out of your shell.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 7