WGU D281 Linux Foundations - Udemy Section 2: Lesson 6 - Creating questions with complete verified solutions 2025Scripts

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/98

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

99 Terms

1
New cards

Describe the format of a basic shell script file:

Shell script files must be plain text files; they cannot be generated using a word processing application. The first line in the shell script should be the shebang combination (#!/bin/sh). This indicates the shell that should be used to run the script. The remainder of the shell script is a list of commands or shell statements arranged in the order in which they are to be run by the shell. Though not required, it's common to use the exit statement at the end of a shell script to control the exit status generated by the shell, especially if the shell exists with any type of error condition.

(MU1_Ch11_ExamEssentials)

2
New cards

Explain how using variables helps when writing a shell script:

Shell Scripts use variables to store data or other information for use later in the shell script. You assign data to a variable using the equal sign (=). The data can be any type of text or numerical data, or if you redirect the output of a command to store in a variable by placing backtick (`) characters around the command. To reference the value of a variable inside the shell script, precede the variable with a dollar sign. You can reference variables in shell commands as well as in shell statements. (MU1_Ch11_ExamEssentials)

3
New cards

Explain how to pass data to a shell script:

Shell scripts can use parameters, or arguments, to pass data from the command line to shell script. Just include the data parameters on the command line command when starting the shell script. Inside the shell script you can retrieve any command line parameters by using positional variables. The $0 positional variable is special; it references the shell command used to launch the shell script. Positional variable $1 references the first parameter entered on the command line, variable $2 the 2nd parameter, and so on.

(MU1_Ch11_ExamEssentials)

4
New cards

Describe the different types of programming features you can use in shell scripts:

Shell scripts can use conditional statements to evaluate data and make programming decisions based on the outcome of the evaluation. The if and case statements are two conditional statements that you can use for that. The if statement can be paired with the else statement to create a true false scenario for executing one set of statements if a condition is true, and another set of statements if a condition is false. The case statement allows you to specify a range of possible values that a condition can evaluate to and execute separate sets of statements for each possible value. Besides conditional statements, shell scripts can use loops to iterate through a set of statements multiple times, based on either a series of values or a condition. Finally, shell scripts can also support defining functions, which are a set of statements bundled together to be called from anywhere in the shell script as needed.

(MU1_Ch11_ExamEssentials)

5
New cards

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.

Set one or more executable bits using chmod.

(MU1_Ch11_ReviewQs)

6
New cards

Describe the effects 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 copies 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.

It copies the contents of big.cc to big.c, eliminating the old big.c.

(MU1_Ch11_ReviewQs)

7
New cards

What is the purpose of conditional expressions and shell scripts?

(A) They prevent scripts from executing if license conditions aren't met.

(B) They display info 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.

They enable the script to take different actions in response to variable data.

(MU1_Ch11_ReviewQs)

8
New cards

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

False

(MU1_Ch11_ReviewQs)

9
New cards

Valid looping statements in Bash include for, while, and until. True or False?

True

(MU1_Ch11_ReviewQs)

10
New cards

The following script launches three simultaneous instances of the terminal program.

#!/bin/bash

terminal

terminal

terminal

True or False?

False

(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 #!/bin/sh)

(MU1_Ch11_ReviewQs)

11
New cards

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

#!/bin/sh

(To ensure that the script works with most user shells, the first line should be #!/bin/sh)

(MU1_Ch11_ReviewQs)

12
New cards

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

echo

(MU1_Ch11_ReviewQs)

13
New cards

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

case

(MU1_Ch11_ReviewQs)

14
New cards

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

exit

(MU1_Ch11_ReviewQs)

15
New cards

Which one of the following choices is known as the shebang combination?

(i) (#/!/bin/sh)

(ii) (#!/bin/sh)

(iii) (#/bin/sh/!)

(iv) (#/bin!/sh)

#!/bin/sh

(The shebang combination is (#!/bin/sh).)

(MU1_KC)

16
New cards

Why is scripting considered important in Linux?

It saves time by automating repetitive tasks and allows users to focus on more meaningful work.

(MU1_USec12_96)

17
New cards

What is a Linux script essentially made of?

A Linux script is essentially a series of linked commands executed in a specific order.

(MU1_USec12_96)

18
New cards

What benefit does scripting provide in terms of task execution?

It ensures procedural integrity by consistently executing steps in the exact order specified.

(MU1_USec12_96)

19
New cards

What is the long-term benefit of investing time into writing a script?

Once created, a script can be reused repeatedly with accuracy and consistency.

(MU1_USec12_96)

20
New cards

Who especially benefits from scripting, according to the lesson?

People who are "inherently lazy" benefit because scripting automates tedious tasks.

(MU1_USec12_96)

21
New cards

(MU1_USec12_97)

22
New cards

What text editors can be used to create shell scripts on Ubuntu?

Nano, Pico, and VI can be used to create shell scripts, with Nano being the default on Ubuntu.

(MU1_USec12_97)

23
New cards

What command installs the full version of the VI editor?

sudo apt get install vim

(MU1_USec12_97)

24
New cards

What is the shebang line and why is it important in a shell script?

The shebang line (#!/bin/bash) specifies the program used to execute the script and must be the first line in the script.

(MU1_USec12_97)

25
New cards

What does the command `set background=dark` do in Vim?

It improves syntax highlighting by adjusting the color scheme for dark terminal backgrounds.

(MU1_USec12_97)

26
New cards

What file can be edited to apply persistent Vim settings like background color?

~/.vimrc

(MU1_USec12_97)

27
New cards

What abbreviation was created in Vim to insert the shebang line quickly?

_abbr _H was created to expand into #!/bin/bash when typed.

(MU1_USec12_97)

28
New cards

How do you insert a new line in Vim while editing?

Press `o` while in command mode to open a new line below the cursor.

(MU1_USec12_97)

29
New cards

How do you save and exit a file in Vim?

Press `Esc`, then type `:x` and press `Enter`.

(MU1_USec12_97)

30
New cards

How do you exit a file in Vim without saving changes?

Press `Esc`, then type `:q!` and press `Enter`.

(MU1_USec12_97)

31
New cards

Where should the shebang line be placed in a shell script?

The shebang line should always be the very first line of a shell script.

(MU1_USec12_97)

32
New cards

(MU1_USec12_98)

33
New cards

Why is it helpful to add a .sh extension to your script filenames?

It helps ensure uniqueness and enables syntax highlighting in editors like Vim or Nano.

(MU1_USec12_98)

34
New cards

Why should you avoid naming your script the same as existing system commands?

To prevent command name conflicts that could interfere with system behavior or your scripts.

(MU1_USec12_98)

35
New cards

Where should you place your scripts so they can be run from anywhere without specifying a path?

In a directory that is part of your PATH variable, such as a ~/bin directory.

(MU1_USec12_98)

36
New cards

How do you create a bin directory in your home folder?

mkdir bin

(MU1_USec12_98)

37
New cards

How can you add your ~/bin directory to your PATH variable temporarily?

PATH="$PATH:$HOME/bin"

(MU1_USec12_98)

38
New cards

How do you check if your PATH variable includes your ~/bin directory?

echo $PATH

(MU1_USec12_98)

39
New cards

What command is used to move a script into your ~/bin directory?

mv scriptname ~/bin

(MU1_USec12_98)

40
New cards

What happens if a script is placed in a directory listed in your PATH?

It can be run from any location in the file system without specifying the path.

(MU1_USec12_98)

41
New cards

What is the benefit of using an abbreviation like _sh in Vim?

It quickly inserts a shebang line like #!/bin/bash into your script.

(MU1_USec12_98)

42
New cards

How do you make a script executable only by the owner?

chmod u+x scriptname

(MU1_USec12_98)

43
New cards

What does the variable $USER represent in a shell script?

It holds the name of the current logged-in user.

(MU1_USec12_98)

44
New cards

What happens when you run a shell script that contains echo $USER?

It prints the username of the person running the script.

(MU1_USec12_98)

45
New cards

Why is placing scripts in ~/bin useful for future reuse?

It allows centralized access to personal scripts from any location in the terminal.

(MU1_USec12_98)

46
New cards

What does exporting a variable in a shell script do?

It makes the variable available to subshells created by the script.

(MU1_USec12_99)

47
New cards

Does exporting a variable in a script make it available to the parent shell?

No, the parent shell is not a subshell, so the variable is not available after the script ends.

(MU1_USec12_99)

48
New cards

What is a common behavior when a script is executed in Bash?

It creates a new fork or subshell process to run the script.

(MU1_USec12_99)

49
New cards

How can you run a script so that it modifies the current shell environment?

By using the `source` command to execute it in the current shell.

(MU1_USec12_99)

50
New cards

What does the command `source scriptname` do?

It runs the script in the current shell environment without forking a new process.

(MU1_USec12_99)

51
New cards

What special variable holds the process ID of the current shell or script?

$$

(MU1_USec12_99)

52
New cards

How can you demonstrate the difference in process ID when using source vs normal execution?

Run the script both ways and echo $$ inside the script; they will differ unless sourced.

(MU1_USec12_99)

53
New cards

What is the result of echoing `$1` inside a script?

It prints the first argument passed to the script.

(MU1_USec12_99)

54
New cards

What happens if no arguments are passed to a script that echoes `$1`?

It prints a blank line or an empty value.

(MU1_USec12_99)

55
New cards

What happens when you execute `./script.sh Fred` and the script contains `echo Hello $1`?

It prints `Hello Fred`.

(MU1_USec12_99)

56
New cards

What does `$2` represent in a shell script?

The second argument passed to the script.

(MU1_USec12_99)

57
New cards

If a script uses `$1` and you pass two arguments to it, which one will be used?

Only the first argument will be used unless `$2` is also referenced in the script.

(MU1_USec12_99)

58
New cards

Why is the `source` command important for setting variables in your current shell?

Because it avoids creating a new subshell and makes variables available immediately in the current shell.

(MU1_USec12_99)

59
New cards

What happens when a script is run with no arguments and it only echoes $1?

It prints just the default message (e.g., "Hello") because $1 is empty.

(MU1_USec12_100)

60
New cards

What command is used to test if a variable is set in Bash scripts?

test -z $1

(MU1_USec12_100)

61
New cards

What does the command `test -z $1 && exit` do in a script?

It checks if $1 is empty, and if so, exits the script early.

(MU1_USec12_100)

62
New cards

What is the purpose of using `&&` in `test -z $1 && exit`?

It runs the `exit` command only if the `test` command returns true.

(MU1_USec12_100)

63
New cards

What is a shorthand alternative to the `test` command in conditional expressions?

Using square brackets: `[ -z $1 ]`

(MU1_USec12_100)

64
New cards

What is required when using square brackets as a test in Bash?

There must be a space after the opening bracket and before the closing bracket.

(MU1_USec12_100)

65
New cards

What does `[ -z $1 ] && exit` do?

It checks if $1 is empty, and if so, exits the script without printing anything.

(MU1_USec12_100)

66
New cards

How can you write the same logic in full if-statement syntax in Bash?

if [ -z $1 ]; then exit; fi

(MU1_USec12_100)

67
New cards

What is the benefit of using the full `if...then...fi` structure instead of `&&`?

It allows more complex logic or multiple commands inside the conditional block.

(MU1_USec12_100)

68
New cards

What does the `fi` keyword do in Bash scripting?

It marks the end of an `if` statement block.

(MU1_USec12_100)

69
New cards

When would a longhand `if` statement be more appropriate than a short `&&` expression?

When multiple commands need to be executed conditionally.

(MU1_USec12_100)

70
New cards

What is the main purpose of conditional statements in shell scripting?

To control script behavior based on whether certain conditions are true or false.

(MU1_USec12_100)

71
New cards

What is the purpose of using a loop in Bash scripting?

To perform repetitive tasks automatically without rewriting code for each item.

(MU1_USec12_101)

72
New cards

What is the syntax structure for a basic for loop in Bash?

for variable in list; do commands; done

(MU1_USec12_101)

73
New cards

Why is `sudo` used before the `passwd` command?

To ensure proper privileges are granted for setting user passwords.

(MU1_USec12_101)

74
New cards

What command is used to verify that the users were added to the system?

tail -n 3 /etc/passwd

(MU1_USec12_101)

75
New cards

What is the purpose of viewing `/etc/passwd` with `tail -n 3`?

To see the last three user entries created by the script.

(MU1_USec12_101)

76
New cards

What is the difference between `/etc/passwd` and `/etc/shadow`?

/etc/passwd stores user account info, while /etc/shadow stores encrypted passwords.

(MU1_USec12_101)

77
New cards

Are loops limited to use inside scripts in Bash?

No, loops can be typed and executed directly from the command line.

(MU1_USec12_101)

78
New cards

What other loop types are mentioned besides for loops?

while loops and until loops.

(MU1_USec12_101)

79
New cards

How does a while loop differ from an until loop?

While loops execute as long as the condition is true; until loops run until the condition becomes true.

(MU1_USec12_101)

80
New cards

What is an alternative to the for loop for repeating code in Bash scripts?

The while loop.

(MU1_USec12_102)

81
New cards

Why should variables be quoted in Bash scripts?

To prevent errors when the variable contains spaces.

(MU1_USec12_102)

82
New cards

Why is the '$' not used when assigning input to a variable with `read`?

Because variable names should not be prefixed with `$` on the left-hand side of an assignment.

(MU1_USec12_102)

83
New cards

What keywords start and end a while loop in Bash?

while and done

(MU1_USec12_102)

84
New cards

What Bash loop runs as long as a condition is false?

The until loop.

(MU1_USec12_102)

85
New cards

What happens if the user presses Enter without inputting a name during the while loop?

The loop continues prompting for input.

(MU1_USec12_102)

86
New cards

What is the purpose of restructuring the Hello script with a while loop?

To make the script more functional and user-friendly by handling missing input.

(MU1_USec12_102)

87
New cards

How does the script behave when a valid name is eventually entered?

It echoes the entered name and exits the loop.

(MU1_USec12_102)

88
New cards

What is the benefit of using a `.sh` or similar extension when naming scripts?

It helps ensure name uniqueness and enables syntax highlighting in editors.

(MU1_USec12_104)

89
New cards

Where should you create your personal script directory?

In your home directory, typically as a bin directory.

(MU1_USec12_104)

90
New cards

What should you do after creating a script to make it runnable?

Use chmod +x followed by the path to the script to make it executable.

(MU1_USec12_104)

91
New cards

What is the purpose of the shebang (#!) line in a script?

It specifies the path to the script interpreter.

(MU1_USec12_104)

92
New cards

What happens to variables in a forked shell?

They are lost when the shell exits and are not available to the parent shell.

(MU1_USec12_104)

93
New cards

How can you make variables from a script available to the current shell?

By using the `source` command to run the script.

(MU1_USec12_104)

94
New cards

What does sourcing a script do differently than executing it normally?

It runs the script in the current shell rather than a new forked shell.

(MU1_USec12_104)

95
New cards

What is the purpose of using conditional statements in a script?

To execute different code paths depending on specific conditions.

(MU1_USec12_104)

96
New cards

What command can be used for conditional testing in Bash scripts?

The `test` command or square brackets `[]`.

(MU1_USec12_104)

97
New cards

What is the syntax structure of a for loop in Bash?

The loop is enclosed between `do` and `done` keywords.

(MU1_USec12_104)

98
New cards

What type of loop is used to repeat code while a condition is true?

The `while` loop.

(MU1_USec12_104)

99
New cards

What is the primary benefit of scripting according to the instructor?

It automates repetitive tasks and saves time.

(MU1_USec12_104)