530 likes | 837 Views
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
E N D
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
The role of a shell • Provides a user interface • Interprets commands • Can manipulate command input and output using shell metacharacters
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
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
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
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
Using Filters • What does this command do? cat prologue | tr a A | sort | pr –d |less
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
The sed and awk commands • sed • cat prologue |sed s/the/THE/
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
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
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'
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
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
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.
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
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.
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.
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
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
Environment variables • Set by default in the BASH shell • The set command • Displays a list of environment variables and their current values
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
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
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
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
Environment files • Store defined variables • Can define different variables for different users • Load variables at log in
Environment file execution Order • /etc/profile • ~/.bash_profile • ~/.bash_login • ~/.profile
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
Escape sequences • Character sequences that have special meaning inside the echo command
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
Decision constructs continued
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
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”
The && and || constructs • Shortcut constructs that take less time form making only one decision • Syntax • command && command • command || command
The bash history feature • Recalls previous commands • Reduces the number of number of keystrokes
Customizing BASH history • Update the bash_history file • Specify number of commands to remember • Set HISTSIZE environment variable