Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Linux related questions, Just answer questions of 70 and 73. In the exercises below you will: * use the shell to redirect the Standard Output

Linux related questions, Just answer questions of 70 and 73. 
In the exercises below you will: * use the shell to redirect the Standard Output and Standard Error to a file and take Standard Input from a file; * redirect Standard Output and Standard Input using pipe metacharacters; * create and use an alias, as well as view and change existing shell variables; * export user-defined variables and load variables automatically upon shell startup; * create a basic shell script and execute it on the system; * create a shell script that uses decision and loop constructs to analyze user input. 1. Boot your Fedora Linux virtual machine. After your Linux system has been loaded, 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 LNXrocks!. 2. At the command prompt, if the files sample1 and sample2 and do not already exist, type touch sample1 sample2 and press Enter to create two new files named sample1 and sample2 in your home directory. Verify their creation by typing ls F at the command prompt, and press Enter. If you see a file named sample3 in your directory listing, delete the file by typing rm sample3 and press Enter. When you are prompted to confirm deletion, enter y and press Enter. 3. At the command prompt, type ls -l sample1 sample2 sample3 and press Enter. Is there any Standard Output displayed on the terminal screen? Is there any Standard Error displayed on the terminal screen? Why? 4. At the command prompt, type ls -l sample1 sample2 sample3 > file and press Enter. Is there any Standard Output displayed on the terminal screen? Is there any Standard Error displayed on the terminal screen? Why? 5. At the command prompt, type cat file and press Enter. What are the contents of the file and why? 6. At the command prompt, type ls -l sample1 sample2 sample3 2>file and press Enter. Is there any Standard Output displayed on the terminal screen? Is there any Standard Error displayed on the terminal screen? Why? 7. At the command prompt, type cat file and press Enter. What are the contents of the file and why? Were the previous contents retained? Why? 8. At the command prompt, type ls -l sample1 sample2 sample3 > file 2>file2 and press Enter. Is there any Standard Output displayed on the terminal screen? Is there any Standard Error displayed on the terminal screen? Why? 9. At the command prompt, type cat file and press Enter. What are the contents of the file and why? 10. At the command prompt, type cat file2 and press Enter. What are the contents of file2 and why? 11. At the command prompt, type ls -l sample1 sample2 sample3 > file 2>&1 and press Enter. Is there any Standard Output displayed on the terminal screen? Is there any Standard Error displayed on the terminal screen? Why? 12. At the command prompt, type cat file and press Enter. What are the contents of the file and why? 13. At the command prompt, type ls -l sample1 sample2 sample3 2>file2 >&2 and press Enter. Is there any Standard Output displayed on the terminal screen? Is there any Standard Error displayed on the terminal screen? Why? 14. At the command prompt, type cat file2 and press Enter. What are the contents of file2 and why? 15. At the command prompt, type date >> file and press Enter. 16. At the command prompt, type cat file and press Enter. What are the contents of the file and why? 17. At the command prompt, type date >> file and press Enter. 18. At the command prompt, type cat file and press Enter. What are the contents of the file and why? Can you tell when each date command was run? 19. At the command prompt, type tr o O /etc/hosts and press Enter. What error message do you receive and why? 20. At the command prompt, type tr o O < /etc/hosts and press Enter. What happened and why? 21. At the command prompt, type cat /etc/nsswitch.conf and press Enter to view the /etc/ nsswitch.conf file. Next, type cat /etc/nsswitch.conf | less at the command prompt and press Enter to perform the same task page-by-page. Explain what the | metacharacter does in the previous command. How is this different from the less /etc/nsswitch.conf command? 22. At the command prompt, type cat /etc/nsswitch.conf | grep nisplus and press Enter. How many lines are displayed? Why did you not need to specify a filename with the grep command? 23. At the command prompt, type cat /etc/nsswitch.conf | grep nisplus | tr n N and press Enter. Explain the output on the terminal screen. 24. At the command prompt, type cat /etc/nsswitch.conf | grep nisplus | tr n N | sort r and press Enter. Explain the output on the terminal screen. 25. At the command prompt, type cat /etc/nsswitch.conf | grep nisplus | tr n N | sort r | tee file and press Enter. Explain the output on the terminal screen. Next, type cat file at the command prompt and press Enter. What are the contents? Why? What does the tee command do in the pipe above? 26. At the command prompt, type cat /etc/nsswitch.conf | grep nisplus | tr n N | sort r | tee file | wc l and press Enter. Explain the output on the terminal screen. Next, type cat file at the command prompt and press Enter. What are the contents and why? 27. At the command prompt, type cat /etc/nsswitch.conf/ | grep nisplus | sed /#/d and press Enter. Explain the output on the terminal screen. Can this output be obtained with the grep and tr commands instead of sed ? 27a. At the command prompt, type cat /etc/hosts. Next, type cat /etc/hosts | awk '/localhost/ {print $1, $3}' and press Enter. Explain the output on the terminal screen. 28. At the command prompt, type set | less and press Enter to view the BASH shell environment variables currently loaded into memory. Scroll through this list using the cursor keys on the keyboard. When finished, press q to quit the less utility. 29. At the command prompt, type env | less and press Enter to view the exported BASH shell environment variables currently loaded into memory. Scroll through this list using the cursor keys on the keyboard. Is this list larger or smaller than the list generated in Step 2? Why? When finished, press q to quit the less utility. 30. At the command prompt, type PS1="Hello There:" and press Enter. What happened and why? Next, type echo $PS1 at the command prompt and press Enter to verify the new value of the PS1 variable. 31. At the command prompt, type exit and press Enter to log out of the shell. Next, log in to the terminal using the user name of root and the password of LNXrocks!. What prompt did you receive and why? How could you ensure that the "Hello There:" prompt occurs at every login? 32. At the command prompt, type nano .bash_profile and press Enter. At the bottom of the file, add the following lines. When finished, press Ctrl-O and Enter to save and Ctrl-X to quit the editor. echo -e "Would you like a hello prompt? (y/n) -->\c" read ANSWER if [ $ANSWER = "y" ] then PS1="Hello There: " fi (Youve just entered your first shell script!) Explain what the preceding lines will perform after each login. 33. At the command prompt, type exit and press Enter to log out of the shell. Next log in to the terminal using the user name of root and the password of LNXrocks!. When prompted for a hello prompt, type y and press Enter. What prompt did you receive and why? (Ignore any error messages you may see.) 34. At the command prompt, type exit and press Enter to log out of the shell. Next, log in to the terminal using the user name of root and the password of LNXrocks!. When prompted for a hello prompt, type n and press Enter to receive the default prompt. 35. At the command prompt, type MYVAR="My sample variable" and press Enter to create a variable called MYVAR. Verify its creation by typing echo $MYVAR at the command prompt and press Enter. 36. At the command prompt, type set | grep MYVAR and press Enter. Is the MYVAR variable listed? Why? 37. At the command prompt, type env | grep MYVAR and press Enter. Is the MYVAR variable listed? Why? 38. At the command prompt, type export MYVAR and press Enter. Next, type env | grep MYVAR at the command prompt and press Enter. Is the MYVAR variable listed now? Why? 39. At the command prompt, type exit and press Enter to log out of the shell. Next, log in to the terminal using the user name of root and the password of LNXrocks!. 44. At the command prompt, type echo $MYVAR and press Enter to view the contents of the MYVAR variable. What is listed and why? 41. At the command prompt, type nano .bash_profile and press Enter. At the bottom of the file, add the following line. When finished, press Ctrl-O and Enter to save and Ctrl-X to quit the editor. (By the way, you may want to remove the lines creating the Hello prompt while you are editing the file...) export MYVAR="My sample variable" 42. At the command prompt, type exit and press Enter to log out of the shell. Next, log in to the terminal using the user name of root and the password of LNXrocks!. 43. At the command prompt, type echo $MYVAR and press Enter to list the contents of the MYVAR variable. What is listed and why? 44. At the command prompt, type alias and press Enter. What aliases are present in your shell? 45. At the command prompt, type alias asample="cd /etc ; cat hosts ; cd - ; ls -F" and press Enter. What does this command do? 46. At the command prompt, type asample and press Enter. What happened and why? What environment file could you add this alias to so that it is executed each time new BASH shell is created? 47. At the command prompt, type nano myscript and press Enter to open a new file for editing called myscript in your home directory. 47. Enter the following text into the myscript file using the nano editor. When finished, press Ctrl-O and Enter to save and Ctrl-X to quit the editor. #!/bin/bash echo -e "This is a sample shell script. \t It displays mounted filesystems \a" mount 49. At the command prompt, type ls -l myscript and press Enter. What permissions does the myscript file have? Next, type bash myscript at the command prompt and press Enter. Did the shell script execute? What do the \t and \a escape sequences do? 50. Next, type ./myscript at the command prompt and press Enter. What error message did you receive and why? 51. At the command prompt, type chmod u+x myscript and press Enter. Next, type ./myscript at the command prompt and press Enter. Did the script execute? Why? 52. At the command prompt, type nano myscript2 and press Enter to open a new file for editing called myscript2 in your home directory. 53. Enter the following text into the myscript2 file. When finished, press Ctrl-O and Enter to save and Ctrl-X to quit the nano editor. #!/bin/bash echo -e "This program adds entries to a family database file. " 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 54. 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. 55. At the command prompt, type cat database and press Enter. Was the entry from Step 54 present? Why? 56. Re-execute the myscript2 script (from Step 54) several times to populate the database file with entries. 57. At the command prompt, type nano 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? \c" 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? \c" 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 When finished, press Ctrl-O and Enter to save and Ctrl-X to quit the nano editor. 58. 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? 59. 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 56. Was it there? Why? 60. At the command prompt, type nano 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 When finished, press Ctrl-O and Enter to save and Ctrl-X to quit the nano editor. 61. At the command prompt, type ./myscript2 and press Enter. Type y and press Enter. What error message do you receive and why? 62. 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? 63. At the command prompt, type ./myscript2 and press Enter. Choose s and press Enter. Search for the family member Entered in Step 62. Does it matter whether you Entered s or S at the prompt earlier? Why? What construct was used in the shell script to select the correct action based on the key Entered? 64. At the command prompt, type nano 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 When finished, press Ctrl-O and Enter to save and Ctrl-X to quit the nano editor. 65. 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? What construct was used in the script to ensure this happens? 66. Choose s and press Enter. Search for the family member that you just Entered. Choose q to quit the shell script. 67. At the command prompt, type nano myscript3 and press Enter to edit a new file called myscript3 in your home directory. 68. Enter the following text into the myscript3 file. When finished, press Ctrl-O and Enter to save and Ctrl-X to quit the nano editor. #!/bin/bash echo -e "This program copies a file to the /stuff directory. " 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." 69. 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? 
 70. 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? 71. At the command prompt, type nano myscript4 and press Enter to edit a new file called myscript4 in your home directory. 72. Enter the following text into the myscript4 file. When finished, press Ctrl-O and Enter to save and Ctrl-X to quit the nano 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 73. 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? Why? What construct was used in the script to accomplish this? 74. Type exit and press Enter to log out of your shell. 

Just answer questions of 70 and 73. 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions