LPI Linux Essentials 010 V1.6 - Chapter 11 Quiz

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

You've written a simple shell script that does nothing but launch programs. To ensure that the script works with most user shells, the first line should be ____________________________. A. #!/bin/sh B. /bin/sh C. # /bin/sh D. bash E. #!bash

A. A shell script should contain the shebang line to indicate the shell required to run the script. The shebang line contains the #! characters, followed by the shell path. Thus, option A is correct. Options B and D are both incorrect because they just specify the shell, not the shebang characters. Option C is incorrect because it uses only the pound symbol, which makes the line a comment. Option E is incorrect because it doesn't specify the full path to the shell.

After using a text editor to create a shell script, what step should you take before trying to use the script by typing its name? A. Set one or more executable bits using chmod. B. Copy the script to the /usr/bin/scripts directory. C. Compile the script by typing bash scriptname, where scriptname is the script's name. D. Run a virus checker on the script to be sure it contains no viruses. E. Run a spell checker on the script to ensure it contains no bugs.

A. Before you can run a shell script directly from the command line, you need to allow execute permissions for at least yourself, so option A is correct. You don't need to have the shell script file located in any specific directory as long as you have access to the directory, so option B is incorrect. Typing the bash scriptname will run the script, not compile it, so option C is incorrect. Viruses are extremely rare in Linux, and because you just created the script, the only way in which it could contain a virus would be if your system was already infected or if you wrote it as a virus, so option D is incorrect. Most spell checkers are intended for English or other human languages, so they lack the ability to check for valid Bash commands, such as esac. Furthermore, even if every keyword is spelled correctly, the script could still contain logic bugs. Thus, option E is incorrect.

The ________________ Bash scripting command is used to control the program flow based on a variable that can take many values (such as all the letters of the alphabet). A. case B. while C. if D. echo E. exit

A. The case command can compare a variable against multiple values and execute different blocks of code based on the matching value, so option A is correct. The while command performs loops, it doesn't compare a variable against multiple values, so option B is incorrect. The if command tests a variable against a single condition, not multiple values, so option C is incorrect. The echo command displays text to the script user; it doesn't compare multiple values, so option D is incorrect. The exit command stops the script and returns to the shell, it doesn't compare a variable against multiple values, so option E is incorrect.

What is the purpose of conditional expressions in shell scripts? A. They prevent scripts from executing if license conditions aren't met. B. They display information about the script's computer environment. C. They enable the script to take different actions in response to variable data. D. They enable scripts to learn in a manner reminiscent of Pavlovian conditioning. E. They cause scripts to run only at specified times of day.

C. Conditional expressions return a true or false response, enabling the script to execute one set of instructions or another or to continue or terminate a loop; thus, option C is correct. Conditional expressions have nothing to do with licensing conditions, so option A is incorrect, nor do they have anything to do with displaying environment information, making option B incorrect as well. Conditional expressions also don't implement Pavlovian conditioning by themselves (you can create a script to implement that, but the conditional expressions by themselves don't), so option D is incorrect. The conditional expressions also don't cause the script to run only at a specified time of day—you need to use the at or cron facility on the Linux system to do that—so option E is incorrect.

Describe the effect of the following short script, cp1, if it's called as cp1 big.c big.cc: #!/bin/bash cp $2 $1 A. It has the same effect as the cp command—copying the contents of big.c to big.cc. B. It compiles the C program big.c and calls the result big.cc. C. It copies the contents of big.cc to big.c, eliminating the old big.c. D. It converts the C program big.c into a C++ program called big.cc. E. The script's first line is invalid, so it won't work.

C. The cp command is the only one called in the script, and that command copies files. \Because the script passes the arguments ($1 and $2) to the cp command in reverse order, their effect is reversed; whereas cp copies its first argument to the second name, the cp1 script copies the second argument to the first name. Option C correctly describes this effect. Option A ignores the reversed order of the arguments, so this option is incorrect. The cp command has nothing to do with compiling C or C++ programs, making options B and D incorrect. The first line in the script is a valid shebang line, indicating the shell to use to run the script, so option E is incorrect.

The ________________ Bash scripting command is used to display prompts for a user in a shell script. A. case B. while C. if D. echo E. exit

D. The echo command is used to display text to the shell user, so option D is correct. The case command compares a value to multiple answers, not display text, so option A is incorrect. The while command performs a loop on a block of code, and doesn't display text, so option B is incorrect. The if command tests a condition and if true, executes a block of code, it doesn't display text, so option C is incorrect. The exit command stops the running script and returns to the shell, passing a numeric exit value, not a text value to display, so option E is incorrect.

The ________________ Bash scripting command controls the return value generated by a script, independent of the other commands used in the script. A. case B. while C. if D. echo E. exit

E. The exit command stops the script and returns a specified value back to the shell, so option E is correct. The case command compares a variable against multiple values, it doesn't control the script return value, so option A is incorrect. The while command performs a loop on a code block, but doesn't control the script return value, so option B is incorrect. The if statement allows you to test a variable against a value, but not control the script return value, so option C is incorrect. The echo statement displays text for the script user, but doesn't control the script return value, so option D is incorrect.

True or false: A user types myscript laser.txt to run a script called myscript. Within myscript, the $0 variable holds the value laser.txt.

False. The $0 variable contains the name of the script, which would be myscript in this example. The first parameter (laser.txt) would be held in the $1 positional variable.

True or false: The following script launches three simultaneous instances of the terminal program. #!/bin/bash terminal terminal terminal

False. The terminal commands don't have the ampersand (&) sign after them to indicate that they should run in background mode, so they will run serially, only one at a time.

True or false: Valid looping statements in Bash include for, while, and until.

True. You can use the for statement to execute a loop a fixed number of times, whereas while and until execute until a test condition is no longer met or is met, respectively.


Ensembles d'études connexes

Chapter 10.6 The Stages of Translation

View Set

E-Commerce- Review Test 3: 8.03 Classes Link/Tables (Test 3)

View Set

Nclex Peds Growth and Development Chapter 25-29

View Set

CH 9 MASTERING - BIOTECHNOLOGY & DNA TECHNOLOGY

View Set

cell bio test #1; part one- chapters 1-2

View Set