L7 Working on BASH Shell

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

The following is a pretty sample shell script called "myscript":

#!/bin/bash echo "Today's date is:" date echo "" echo "The people logged on to your system include:" who echo "" echo The contents of the / directory are:" ls -F /

The following is a more fancy shell script called "myscript":

#!/bin/bash echo -e "Today's date is: \c" date echo -e "\nThe people logged on to your system include:" who echo -e "\nThe contents of the / directory are:" ls -F /

Another example of a shell script with an if construct called "ifthenscript2":

#!/bin/bash echo -e "Today's date is: \c" date echo -e "\nThe people logged on to your system include:" who echo -e "Would you like to see the consents of /?(y/n) -->\c" read ANSWER if [ $ANSWER = "y" -o $ANSWER = "Y" ] then echo -e "\nThe contents of the / directory are:" ls -F / fi

An example of a shell script with an if construct called "ifthenscript":

#!/bin/bash echo -e "Today's date is: \c" date echo -e "\nThe people logged on to your system include:" who echo -e "Would you like to see the contents is /?(y/n) -->\c"read ANSWER if [ANSWER = "y" ] then echo -e "\nThe contents of the / directory are:" ls -F / fi

The command on the right of the _____ construct is executed only if the command on the left of the construct completes successfully.

&&

alias dw="date;who"

...

alias mf="mount -t ext2 /dev0/fd0 /media/floppy"

...

_____ execute commands repetitively. They alter the flow of a program based on the results of a particular statement. They repeat parts of the program until it reaches the desired result. They can be used to process a list of objects, such as files, directories, users, printers and so on. The for construct is an example.

Loop Constructs

cat prologue | tr a A | sort | pr -d | tee newfile | less

This command takes the prologue file and translates the "a"'s to "A"'s and then sorts the lines in alphabetical order. In addition, it displays the file on the terminal double spaced and allows the user to view the results page-by-page. In addition, it uses the tee command to save a copy of the output as a file called newfile.

18. A for construct is a loop construct that processes a specified list of objects. As a result, it is executed as long as there are remaining objects to process. True or False?

True

3. The alias command can be used to make a shortcut to a single command. True or False?

True

T/F. A BASH shell has several variables in memory at any one time.

True

T/F. After /etc/profile finishes executing, the home directory of the user is searched for the hidden environment files .bash_profile, .bash_login and .profile.

True

T/F. Any command can also be placed inside any environment file.

True

T/F. If a match is found for the case construct, the commands to the right of pattern are executed.

True

T/F. Shell scripts may need input from the user executing the program. This input can then be stored in a variable for later use.

True

T/F. The BASH shell clears the contents of the goodoutput file if it exists and creates the file if it does not exist.

True

T/F. The case construct must end with esac ("case" backwards).

True

T/F. The if construct controls the flow of the program based on true/false decisions.

True

T/F. The ls and mount commands are not filter commands because they do not accept Standard Input from other commands.

True

T/F. There are shortcut constructs that take less time when only one decision needs to be made during the execution of the program.

True

T/F. To add a variable, add a line to the environment file that has the same format as the command used on the command line.

True

T/F. To change the value of a variable, the user can specify the variable name followed by the equal sign (=) and the new value.

True

T/F. To prevent the file from being cleared by the BASH shell and append output to the existing output, you can specify two ">" metacharacters alongside the file descriptor. The following is an example: date >>dateoutput

True

T/F. Using the echo -e command makes it easier to read the output of the shell scripts.

True

T/F. When exiting the BASH shell, all variables stored in memory are destroyed along with the shell itself.

True

The _____ construct begins with a test statement and the commands within the loop construct are executed as long as the test statement returns true. It contains a counter variable and the value of the counter changes with each iteration of the loop.

While

7. Which of the following will display the message welcome home if the cd /home/user1 command is successfully executed? a. cd /home/user1 && echo "welcome home" b. cat "welcome home" || cd /home/user1 c. cd /home/user1 || cat "welcome home" d. echo "welcome home" && cd /home/user1

a. cd /home/user1 && echo "welcome home"

17. Which construct can be used in a shell script to read Standard Input and place it in a variable? a. read b. sum c. verify d. test

a. read

The _____ command creates a shortcut to a command and is stored in a special variable.

alias

20. Consider the following shell script: echo -e "What is your favorite color?--> \c" read REPLY if [ "$REPLY" = "red" -o "$REPLY" = "blue" ] then echo "The answer is red or blue." else echo "The answer is not red nor blue." fi a) The answer is red or blue. b) The answer is not red nor blue. c) The code would cause an error. d) The answer is red or blue. The answer is not red nor blue.

b) The answer is not red nor blue

8. The current value for the HOME variable is displayed by which of the following commands? (Choose all that apply.) a. echo HOME= b. echo ~ c. echo $HOME d. echo ls HOME

b. echo ~ c. echo $HOME

11. What would be the effect of using the alias command to make an alias for the date command named cat in honor of your favorite pet? a. It cannot be done as there already is an environment variable cat associated with the cat command. b. It cannot be done as there already is a command cat on the system. c. When you use the cat command at the command prompt with the intention of viewing a text file, the date appears instead. d. There is no effect until the alias is imported as it is a user-declared variable.

c. When you use the cat command at the command prompt with the intention of viewing a text file, the date appears instead.

13. You have redirected Standard Error to a file called Errors. You view the contents of this file afterward and notice that there are six error messages. After repeating the procedure, you notice that there are only two error messages in this file. Why? a. After you open the file and view the contents, the contents are lost. b. The system generated different Standard Output. c. You did not append the Standard Error to the Error file and as a result, it was overwritten when the command was run a second time. d. You must specify a new file each and every time you redirect as the system creates the specified file by default.

c. You did not append the Standard Error to the Error file and as a result, it was overwritten when the command was run a second time.

2. Before a user-defined variable can be used by processes that run in subshells, that variable must be __________. a. imported b. validated by running the env command c. exported d. redirected to the BASH shell

c. exported

The _____ command lists all exported environment and user-defined variables in the shell.

env

Edit the environment file using the gedit editor and add the following line to the file.

export MYVAR2="This is another sample variable".

The following is the format for the if construct.

if this is true then do these commands elif this is true then do these commands else do these commands fi

A _____ is a string of commands connected by the "|" metacharacters. The shell then sends the stdout on the left to the stdin on the right.

pipe

The _____ command takes user input from stdin and places it in a variable specified by an argument to the command.

read

The _____ filter command searches for a certain string of text and replaces that text string with another text string.

sed

A _____ is a shell created by current shell.

subshell

The syntax of the while construct is as follows.

while this returns true do these commands done

Here is another shell script called "newscript":

#!/bin/bash echo -e "What is your name? -->\c" read USERNAME echo "Hello $USERNAME"

A sample shell script called "myscript":

#!/bin/bash #this is a comment date who ls -F /

State some special comparison operators for the if construct.

-o (OR) -a (AND) ! (NOT)

During execution, this program sets var_name to a string name, and executes the commands between do and done for that string. It repeats for all of the items in the string.

...

The _____ command created a variable that is available to the current shell. The_____ command displays the value of the variable.

MYVAR, echo

The _____ is used to test a condition. It generates a true value if they perform their function properly. The condition is inside of square brackets ([ ... ]) and there must have spaces after "[" and before "]".

Test Statement

T/F. Piping is commonly used to reduce the amount of information displayed on the terminal screen from commands that display too much information.

True

T/F. Redirection only occurs from a command to a file and vice versa.

True

4. Which of the following files is always executed immediately after a user logs in to a Linux system and receives a BASH shell? a. /etc/profile b. ~/.bash_profile c. ~/.bash_login d. ~/.profile

a. /etc/profile

15. What is wrong with the following command string ls /etc/hosts >listofhostfile? a. Nothing is wrong with the command. b. The file descriptor was not declared; unless 1 for Standard Ouput or 2 for Standard Error is indicated, the command will fail. c. The ls command is one of the commands that cannot be used with redirection; you must use | to pipe instead. d. The file listofhostfile will always only contain Standard Error as a file descriptor was not declared.

a. Nothing is wrong with the command.

9. Which of the following file descriptor numbers represents stdout? a. 2 b. 0 c. 1 d. 3

c. 1

5. Which command could you use to see a list of all environment and user-defined shell variables as well as their current values? a. ls /var b. env c. set d. echo

c. set

The _____ construct compares a value of a variable with several different patterns of text or numbers.

case

The syntax for the case construct is as follows:

case variable in pattern1 ) do this ;; pattern2 ) do this ;; pattern3 ) do this ;; esac

State the syntax of these time saving shortcuts.

command && command command || command

10. Which of the following operators reverses the meaning of a test statement? a. #! b. -o c. -a d. !

d. !

12. How do you indicate a comment line in a shell script? a. There are no comment lines in a shell script. b. Begin the line with #!. c. Begin the line with !. d. Begin the line with #.

d. Begin the line with #.

19. What does >> accomplish when entered on the command line after a command? a. It redirects both Standard Error and Standard Output to the same location. b. It does not accomplish anything. c. It redirects Standard Error and Standard Input to the same location. d. It appends Standard Output to a file.

d. It appends Standard Output to a file.

6. Every if construct begins with if and must be terminated with? a. end b. endif c. stop d. fi

d. fi

PS1="This is a new prompt: #"

echo $HOME echo $PWD echo $PATH

MYVAR="This is a sample variable."

echo $MYVAR

The _____ command is used to export user-defined variables to the subshells and it ensures that the programs started by the current shell have access to these variables.

export

A _____ command is any command that can take standard input and transform it to standard output.

filter

The syntax of the for construct is as follows.

for var_name in string1 string2 ... ... do these commands done

The syntax of the sed filter command is

s/search/replace/.

A_____ command is a filter command that takes information from Standard Input and sends that information to a file, as well as to Standard Output.

tee

State some examples of commands that may be placed in an environment file.

umask 077 date alias dw="date;who"

The command on the right of the _____ construct is executed only if the command on the left of the construct did not complete successfully.

||

The _____ file is typically used to set aliases and variables that must be present in each BASH shell. It is executed immediately after login for all users on the system as well as when a new BASH shell is created after login.

~/ .bashrc (BASH run-time configuration)

State some of the common BASH shell environment files and the order in which they are executed.

~/.bashrc /etc/profile ~/.bash_profile ~/.bash_login ~/.profile

_______ are the most common type of construct used in shell scripts. They alter the flow of a program based on whether a command completed successfully or based on user input to a question.

Decision Constructs

_____ are character sequences that have special meaning inside the echo command and are prefixed by \ character. The echo -e command must be used for this sequence.

Escape Sequences

1. Because Standard Error and Standard Output represent the results of a command and Standard Input represents the input required for a command, only Standard Error and Standard Output can be redirected to/from a file. True or False?

False

cat prologue | tr a A | sort | pr -d | less

This command takes the prologue file and translates the "a"'s to "A"'s and then sorts the lines in alphabetical order. In addition, it displays the file on the terminal double spaced and allows the user to view the results page-by-page.

State the common rules governing the if construct.

The elif (else if) and else statements are optional. There are an unlimited number of elif statements. The do these commands section may consist of multiple commands, one per line. The do these commands section is typically indented for readability. The end of statement must be a backward "if" (fi). The this is true part of the if syntax can be a command or a test statement.

The features of variable identifiers are the following:

They can contain alphanumeric characters (0 - 9, A - Z, a - z), the dash (-) character, or the underscore (_) character. They must not start with a number. They are typically capitalized to follow convention.

tr l L </etc/hosts

It does not modify the /etc/hosts file. It merely translates all of the "l"'s to "L"'s and displays the output on the screen.

tr l L </etc/hosts >newhosts

It does the same thing as the previous command which is translates all of the "l"'s to "L"'s. However, it does not display the output on the screen but rather saves the output to a file named newhosts.

export MYVAR2="This is another sample variable." set | grep MYVAR2

MYVAR2='This is another sample variable.' _=MYVAR2

env | grep MYVAR2

MYVAR2=This is another sample variable.

set | grep MYVAR

MYVAR='This is a sample variable.'

export MYVAR env | grep MYVAR

MYVAR=This is a sample variable.

env | grep MYVAR

Nothing is displayed.

_____ is a file descriptor that refers to the information processed by the command during execution, and is often takes the form of user input typed on the keyboard.

Standard Input (stdin)

_____ is a file descriptor that refers to the normal output of a command.

Standard Output (stdout)

Three file descriptors are available to each command that can be manipulated by the BASH shell. Name them.

Standard input (stdin) Standard output (stdout) Standard error (stderr)

Every file descriptor is represented by a number. State the file descriptors and their represented numbers.

Stdin is represented by the number 0. Stdout is represented by the number 1. Stderr is represented by the number 2.

set

This command displays a list of the environment variables that are set by the BASH shell.

cat prologue | awk '/the/ {print $1, $4}'

This command displays the first and fourth words only on lines of the prologue file that contains the word "the".

tail /etc/passwd | awk -F: '/user1/ {print $6, $7}'

This command displays the sixth and seventh words for lines that contain the word "user1" in the last 10 lines of the file.

echo $PS1

This command displays the value of the PS1 variable. [\u@h \W\$ \u - user \h - host name \W - name of the current directory

mount | grep /dev/sda

This command executes the mount command and displays only those lines in the output dealing with the /dev/sda directory.

cat prologue | sed s/the/THE/g

This command searches each line of the prologue file for the word "the" and replaces it with the word "THE". It now replaces all of the strings "the" it finds globally in the whole document.

cat prologue | sed s/the/THE/

This command searches each line of the prologue file for the word "the" and replaces it with the word "THE". It only replaces the first "the" it finds in each line.

cat prologue | sed /love/s/the/THE/g

This command searches each line of the prologue file that contains the string "love" and replaces all of the strings "the" with the string "THE".

cat prologue | sed 5,8s/the/THE/g

This command searches lines 5 through 8 of the prologue file for the string "the" and replaces them with the string "THE" globally.

cat prologue | sed /the/d

This command searches through the prologue file and deletes all of the lines that contain the string "the".

ls -l /etc | less

This command sends the output of the ls command to the input of the less command.

tr a A <prologue | sort | nl > newprologue

This command takes the prologue file and translates the "a"'s to "A"'s and then sorts the lines in alphabetical order and numbers them. The output is saved as a file called newprologue.

A command may be in the form of the following: ls /etc/hosts /etc/h 2>badoutput Explain what this command is doing.

This would simply send the stderr to the badoutput file. The stdout is still displayed on the screen because it was not redirected to a file.

A command may be in the form of the following: ls /etc/hosts /etc/h 1>goodoutput Explain what this command is doing.

This would simply send the stdout to the goodoutput file. The stderr is still displayed on the screen because it was not redirected to a file.

T/F. If the user redirects the Standard Output and Standard Error to the same file without any loss of data, special notation must be used. The following command can be used: ls /etc/hosts /etc/h >goodoutput 2>&1

True

T/F. If these files exist, the first one found is executed. As a result, only one of these files is typically used.

True

T/F. If you have read permission to a shell script, you can execute the shell script by starting another BASH shell and specifying the shell script as an argument. bash myscript

True

T/F. It is best to redirect the content of Standard Output and Standard Error to two separate files.

True

T/F. Most commands that are run by the shell are run in a separate subshell which was created by the current shell. Any variables created in the current shell are not available to those subshells and the commands running within them.

True

T/F. Redirecting the Standard Output to a file for later use is more common than redirecting the Standard Error to a file, the BASH shell assumes Standard Output in the absence of a numeric file descriptor, i.e., ls /etc/hosts /etc/h >goodoutput

True

T/F. Redirection and piping can be combined together. Input redirection must occur at the beginning of the pipe and output redirection must occur at end of the pipe.

True

T/F. Since the output of the set command and the env command is typically large, it is common to redirect the Standard Output of these commands to the grep command to display certain lines only.

True

T/F. Some commands on the Linux system only accept files when they are passed by the shell through Standard Input. Therefore to redirect stdin of a file, the user must use the "<" shell metacharacter.

True

T/F. The BASH shell can be used to redirect stdout and stderr from the terminal screen to a file on the filesystem using the ">" shell metacharacter followed by the absolute or relative pathname of the file.

True

T/F. The BASH shell is responsible for providing a user interface and interpreting commands entered on the command line. In addition, the BASH shell can manipulate command input and output, provided the user specifies certain shell metacharacters on the command line alongside the command.

True

T/F. The Bash shell clears a file that already exists before performing the redirection.

True

T/F. The PATH variable is searched in the order that the directories are listed. If the executable file is not in a directory listed in the PATH variable, the user must specify either the absolute or relative pathname to the executable file.

True

T/F. The awk command uses space or tab characters as delimiters for each field in a line. Most configuration files on Linux systems are delimited using the colon (:) character. To change the delimiter that the awk command uses, use the -F option to the command.

True

T/F. The order of redirection on the command line does not matter.

True

T/F. The other environment files are only executed after login after the ~/.bashrc file has been executed.

True

T/F. The user can create a single alias to multiple commands by using the ";" metacharacter.

True

T/F. The user can redirect both Standard Output and Standard Error to separate files at the same time, as shown in the following output, ls /etc/hosts /etc/h >goodoutput 2>badoutput

True

T/F. The user can redirect stdout and stderr to separate files.

True

T/F. The user can send the Standard Output of one command to another command as Standard Input. To do this, the user must use the pipe | shell metacharacter and specify commands on either side. The space before and after the | symbol is optional.

True

T/F. The user can use more than one pipe | metacharacter on the command line to pass information from one command to another over a series of commands.

True

T/F. The user should use separate filenames for stdout and stderr.

True

T/F. The user should use unique alias names.

True

T/F. To create new variables, specify the variable identifier followed by equal sign and the new contents.

True

T/F. You may want to execute cleanup tasks upon exiting the shell. Simply add those cleanup tasks to the .bash_logout file in your home directory.

True

T/F. Both stdout and stderr are displayed on the terminal screen by default.

True ls /etc/hosts etc/h

Filter commands must be at the _____ of a pipe.

beginning

If you have read and execute permission to a shell script, you can execute the shell script like any other executable program on the system.

chmod a+x myscript (gives the file "myscript" execute permission) ./myscript

MYVAR="This is a sample variable."

echo $MYVAR This is a sample variable.

The _____ store variables and values and are executed each time the BASH shell is started to ensure variables are always accessible. The user can put the values in a file to ensure that variables are accessible to a shell at all times.

environment files

Command input and output are represented by labels known as _____

file descriptors

A _____ is the first line in a shell script and it specifies the pathname to the shell that interprets the contents of the shell script.

hashpling

These _____ environment files allow a user to set customized variables independent of BASH shells used by other users on the system.

hidden

The _____ command can be used to list all environment variables in the BASH shell including user-defined variables.

set

A _____ is a text file containing a list of commands or constructs for the shell to execute. It may contain any command that can be entered on the command line.

shell script

The _____ command can be used to replace characters in a file sent via Standard Input.

tr

The_____ is the name of a variable.

variable identifier

From now on, type dw.

...

From now on, type mf.

...

The __________________________ filter command searches for patterns of text and perform some action on the text found. This command treats each line of text as a record in a database and each word in a line as a database field.

...

The_____ file is always executed immediately after login for all users on the system and sets most environment variables, such as HOME and PATH.

/etc/profile


Ensembles d'études connexes

Labor and Birth Complications: Chapter 17

View Set

Principles of Investments - Final (CH 8, 10, 11, 17 +)

View Set

Combo with Nclex Review: Hypertension and 3 others

View Set

Module 13: Checkpoint #2: Hypothesis Testing for a Population Proportion (all 4 attempts)

View Set

Introduction To Psychology Practice Exam 4

View Set

Chapter 27 Drugs for Seizure Disorders

View Set

Lesson 24: Circumferences and Areas of Circles

View Set

Chapter 11 - Buy-Sell Agreements

View Set