1 / 51

Working with the BASH shell

Working with the BASH shell. Unit objectives Redirect the input and output of a command Identify, manipulate, create and export shell variables and edit environment files to create variables

merton
Télécharger la présentation

Working with the BASH shell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with the BASH shell Unit objectives • Redirect the input and output of a command • Identify, manipulate, create and export shell variables and edit environment files to create variables • Describe the purpose of shell scripts, create and execute them, and use common decision constructs in shell scripts • Use and customize features of the BASH shell

  2. Topic A: Command input and output

  3. The role of a shell • Provides a user interface • Interprets commands • Can manipulate command input and output using shell metacharacters

  4. File descriptors • Standard Input (stdin) • Represent information inputted to a command during execution • Standard Output (stdout) • Represents the desired output from a command • Standard Error (stderror) • Represents any error message generated by a command

  5. Three common file descriptors

  6. Redirection • Redirect Standard Output and Standard Error to a file using the “>” • You can redirect both Standard Output and Standard Error to separate files at the same time • You can redirect both Standard Output and Standard Error to the same file using the “&” character

  7. Redirection • Append data • date >> dateoutput • Adds output of date command to dateoutput file • Redirect to St. Input • tr 1 L </etc/hosts • tr 1 L </etc/hosts >newhosts

  8. Types of redirection

  9. Pipes • Sends Standard Output of one command as Standard Input to another command • Pipe • A string of commands connected by “|” metacharacter • Example • Ls –l /etc| less

  10. Piping information

  11. Using multiple pipes

  12. Common filter commands

  13. Using Filters • What does this command do? cat prologue | tr a A | sort | pr –d |less

  14. The sed and awk commands • sed • Can search for text string • Can replace text with another string • awk • Treats each line as a database record • Treats each word as a database field

  15. The sed and awk commands • sed • cat prologue |sed s/the/THE/

  16. Any Questions?

  17. A user wants the script 'name1' to process the contents of the file 'name2', then redirect the output to the program 'name3'. Which of the following shell commands will do this? A. name1 | name2 > name3 B. name1 < name2 | name3 C. name1 > name2 | name3 D. name1 | name2 < name3

  18. A user wants the script 'name1' to process the contents of the file 'name2', then redirect the output to the program 'name3'. Which of the following shell commands will do this? A. name1 | name2 > name3 B. name1 < name2 | name3 C. name1 > name2 | name3 D. name1 | name2 < name3 Answer: A

  19. How could a user substitute all instances of PC with Computer in a file named instructions and display the results in the terminal window? A. sed s/PC/computer/g instructions B. sed ­s PC ­r computer instructions C. cat instructions | awk C omputer D. cat instructions | awk '/PC/ print omputer'

  20. How could a user substitute all instances of PC with Computer in a file named instructions and display the results in the terminal window? A. sed s/PC/computer/g instructions B. sed ­s PC ­r computer instructions C. cat instructions | awk C omputer D. cat instructions | awk '/PC/ print omputer' Answer: A

  21. A user wants the script name1 to process the contents of the file "name2", then redirect the output to the program name3. Which if the following shell commands will do this? A. name1 | name2 > name3 B. name1 < name2 | name3 C. name1 > name2 | name 3 D. name1 | name2 < name3

  22. A user wants the script name1 to process the contents of the file "name2", then redirect the output to the program name3. Which if the following shell commands will do this? A. name1 | name2 > name3 B. name1 < name2 | name3 C. name1 > name2 | name 3 D. name1 | name2 < name3 Answer: B.

  23. An administrator needs to append the list of files in the /tmp directory to the existing file "DoNotCreateBackup". What command would accomplish this goal? A. ls /tmp || DoNotCreateBackup B. ls /tmp && DoNotCreateBackup C. ls /tmp >> DoNotCreateBackup D. ls /tmp :: DoNotCreateBackup

  24. An administrator needs to append the list of files in the /tmp directory to the existing file "DoNotCreateBackup". What command would accomplish this goal? A. ls /tmp || DoNotCreateBackup B. ls /tmp && DoNotCreateBackup C. ls /tmp >> DoNotCreateBackup D. ls /tmp :: DoNotCreateBackup Answer: C.

  25. If the command below is executed in a user's home directory, which of the following would be accomplished? echo hello >> allusers A. Sends the message 'hello' to all users on the system. B. Creates a new file called 'hello' and sends it to all users. C. Sends the message 'hello' to all currently logged in users. D. Appends 'hello' to the end of the file llusers? if it exists.

  26. If the command below is executed in a user's home directory, which of the following would be accomplished? echo hello >> allusers A. Sends the message 'hello' to all users on the system. B. Creates a new file called 'hello' and sends it to all users. C. Sends the message 'hello' to all currently logged in users. D. Appends 'hello' to the end of the file llusers? if it exists. Answer: D

  27. Topic B: Shell variables

  28. Shell variables • Variable • A reserved portion of memory containing information that may be accessed • Environment variables • Typically set by the system • Contain information that the system and programs access regularly • User-defined variables • Your own custom variables

  29. Environment variables • Set by default in the BASH shell • The set command • Displays a list of environment variables and their current values

  30. The PATH variable • One of the most important variables • Helps shell find location of commands • Saves you having to enter full path to commands • Most commands located in /bin or /sbin

  31. BASH environment variables

  32. User-defined variables • Variable identifier • The name of a variable • Features of variable identifiers • Can contain alphanumeric characters, the dash character, or the underscore character • Must not start with a number • Typically capitalized

  33. Subshells • Most shell scripts that are run by the shell are run in a separate subshell • The subshell is created by the current shell • Variables created in the current shell are not available to subshells or the commands running within them

  34. Built-in commands • Other variables not displayed by the set or env commands • Perform specialized functions in the shell • The UMASK variable • A special variable is set by the umask command

  35. Environment files • Store defined variables • Can define different variables for different users • Load variables at log in

  36. Environment file execution Order • /etc/profile • ~/.bash_profile • ~/.bash_login • ~/.profile

  37. Topic C: Shell scripts

  38. Shell scripts • Text files that contain a list of commands or constructs for the shell to execute in order • Can enter commands just as a user would

  39. Escape sequences • Character sequences that have special meaning inside the echo command

  40. Reading standard input • Shell script may need input from the user • Input may be stored in a variable for later use • The read command • Takes input from Standard Input and places it in a variable specified by an argument to the read command

  41. Decision constructs continued

  42. Decision constructs, continued

  43. if construct rules • elif (else if) and else statements are optional • Can have unlimited number of elif statements • do these commands section can consist of multiple commands • do these commands section is typically indented from the left • End of statement is backwards if, “fi” • this is true part of syntax may be command or test statement

  44. Common test statements

  45. Special operators

  46. The case construct • The case statement • Compares variable value with several patterns of text or numbers • If there is a match, commands to the right are executed • Ended by a backwards case, “esac”

  47. The && and || constructs • Shortcut constructs that take less time form making only one decision • Syntax • command && command • command || command

  48. Topic D: BASH command history

  49. The bash history feature • Recalls previous commands • Reduces the number of number of keystrokes

  50. Customizing BASH history • Update the bash_history file • Specify number of commands to remember • Set HISTSIZE environment variable

More Related