MODULE 11- Basic Scripting Flashcards

1
Q

Which file type contains a series of shell commands that can be executed in sequence?

A

Shell script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of using a shell script instead of typing commands manually each time?

A

To automate repetitive tasks, ensure consistency, and save time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which error occurs if you try to run a script directly without execute permissions?

A

Permission denied

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which command is used to run a shell script by passing it as an argument to the shell?

A

sh scriptname.sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What command do you use to make a script executable?

A

chmod +x scriptname.sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is ./ used before a script name when running it directly?

A

Because the current directory is usually not in the $PATH, so ./ specifies the current location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which line at the beginning of a script tells the system to run the script using the /bin/sh shell?

A

!/bin/sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the command ./test.sh do after the script is made executable?

A

Runs the script directly from the current directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the name given to the #! characters at the beginning of a script file?

A

Shebang (or crunchbang)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which two shells are commonly used in shebang lines for traditional shell scripts?

A

/bin/sh and /bin/bash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which two text editors are mentioned in the LPI Essentials syllabus for editing shell scripts?

A

nano and vi (or vim)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What command opens a file named test.sh in the nano editor?

A

nano test.sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which text editor is recommended for beginners due to its simplicity?

A

nano

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the echo -n command in a script?

A

Prints text without adding a newline at the end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

This key combination exits nano and prompts you to save if the file was modified.

A

→ Ctrl + X

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which text editor is more powerful but has a steep learning curve?

A

vi or its improved version, vim

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which command prints the current system date and time in a shell script?

A

date

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

This key combination saves the current file in nano without exiting.

A

→ Ctrl + O

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

This key combination cuts the current line or selected text into the copy buffer.

A

→ Ctrl + K

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

This key combination begins a text search within the open file.

A

→ Ctrl + W

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

This key combination pastes the content from the copy buffer at the cursor’s position.

A

→ Ctrl + U

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

This key sequence lets you search and then replace text in the document.

A

→ Ctrl + W, then Ctrl + R

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

This key combination opens the nano help menu listing all available commands.

A

→ Ctrl + G

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

These key combinations let you page up and down in the file one screen at a time.

A

→ Ctrl + Y (up), Ctrl + V (down)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
This key combination shows the current cursor position and file size.
→ Ctrl + C
18
This scripting feature stores temporary information using a name like ANIMAL or NAME.
Variable
19
Which syntax is used to assign a value to a variable in Bash, without spaces on either side of the equals sign?
VAR=value
19
Which symbol is used before a variable name to retrieve its contents in a script?
Dollar sign ($VARNAME)
20
Which term describes replacing a variable reference with its value during script execution?
Interpolation
21
Which syntax assigns the output of a command like pwd to a variable i?
VAR=`pwd`
22
Which command lets you prompt the user for input and store it in a variable?
read
22
Which special variable refers to the first argument passed to a script, such as “World” in ./test.sh World?
$1
23
Which special variable contains the name of the script itself?
$0
24
Which special variable stores the numeric exit status of the last executed command?
$?
25
Which command do you use to explicitly set the exit code of a shell script?
exit [#]
26
Which numeric exit code indicates that a command or script completed successfully?
0
27
Which option in the grep command suppresses output and returns only the exit status?
→ -q (quiet mode)
28
Which symbol is used to begin a comment in a Bash script, making the line ignored by the shell?
#
28
Which structure in a script lets the program take different actions based on whether a condition is true or false?
A conditional
29
In the statement if somecommand; then, what must somecommand return for the block to execute?
→ Exit code 0, which means success
29
Which keyword starts a conditional test that checks the exit status of a command?
if
30
Which keyword is used to define the block of code that runs when the if condition is false?
else
30
Which keyword ends a conditional block started with if in a Bash script?
fi
31
Which character separates the command being tested from the then keyword in an if statement?
→ A semicolon ;
32
Which keyword block will execute if the command inside the if fails (returns non-zero)?
else
33
What must follow an else block to properly close a conditional structure?
fi
33
What happens in a conditional block if the grep command returns exit code 1?
→ The else block will run, meaning the string was not found
34
Which command is used inside conditionals to perform file, string, or numeric comparisons in shell scripts?
Which command is used inside conditionals to perform file, string, or numeric comparisons in shell scripts?
34
What command is a commonly used alias for test that allows conditions to be written in a shorter, bracketed form?
→ [ (left square bracket)
35
What must always be included when using the [ alias form of test?
→ A closing square bracket ] separated by spaces
36
What does the -d operator test when used in a conditional?
→ Whether the specified path exists and is a directory
37
Which command would return exit code 0 if the file /dev/ttyS0 exists?
→ test -f /dev/ttyS0
37
What does the -f test operator check for when used with test or [?
→ Whether a file exists and is a regular file
38
Which operator tests whether the user has execute permission on a given file?
-x
38
Which command would succeed only if the file /dev/ttyS0 does not exist?
→ test ! -f /dev/ttyS0
39
Which test command checks whether two numbers are not equal?
→ test 1 -ne 1
40
Which test command would return 0 if the numbers 1 and 1 are equal?
→ test 1 -eq 1
41
Which test command checks whether two strings are exactly equal?
→ test "a" = "a"
41
Which operator Performs a logical OR; the test passes if either condition is true?
the -o operator
42
Which test command returns 0 if two strings are different?
→ test "a" != "a"
43
Which operator Performs a logical AND; the test passes only if both conditions are true?
the -a operator
43
Which test command checks whether either 1 = 1 or 2 = 2 using OR logic?
→ test 1 -eq 1 -o 2 -eq 2
43
Which test command checks whether both 1 = 1 and 2 = 2 using AND logic?
test 1 -eq 1 -a 2 -eq 2
44
Which command would be equivalent to if test -f /tmp/foo; then using bracket syntax?
if [ -f /tmp/foo ]; then
45
Which command lets you check another condition if the first if didn’t match?
elif which stands for "else if"
45
Which control structure allows you to check multiple conditions in sequence by chaining else if blocks?
→ if/elif/else
46
Which keyword is is used in shell scripting to test an additional condition if the initial if test fails?
elif
47
Which operator is used for comparing strings in if or elif statements?
=
48
Which structure allows you to perform pattern-based matching instead of writing many elif statements?
case
48
In a case statement, what keyword marks the start of the pattern matching?
→ case followed by the variable or expression
48
In a case statement, how are multiple matching patterns separated when testing a value?
→ With the vertical bar | symbol (OR operator)
49
What command block is used in a case pattern to execute commands if the pattern matches?
→ A list of commands ending in ;;
49
Which pattern acts like a default or else condition in a case statement and matches anything?
*
50
When does a case statement stop checking additional patterns?
→ As soon as the first matching pattern is found and executed
50
Which keyword ends a case block after all patterns have been defined?
esac
51
Which scripting feature allows a command or group of commands to be executed repeatedly?
A loop.
51
How are commands inside a case block terminated after a match is handled?
→ With ;;
52
Which scripting feature allows a command or group of commands to be executed repeatedly?
→ A loop.
52
Which two main types of loops are used in shell scripts?
→ for loops and while loops.
52
Which loop type is best used when iterating over a known collection such as a list of files or names?
→ A for loop.
53
In the script below, what does $SERVERS expand into? SERVERS="servera serverb serverc" for S in $SERVERS; do echo "Doing something to $S" done
→ It expands into the list of server names: servera serverb serverc.
53
Which type of loop should you use when working with a list of unknown or changing size?
→ A while loop.
54
How does a while loop determine whether to continue running?
→ By testing a condition that must remain true for the loop to continue.
55
What command is used to perform arithmetic inside a shell script?
→ The $(( )) arithmetic expansion syntax.
55
Which symbol must follow a for loop's do statement?
done
55
How are for and while loops similar in their structure?
→ Both use do to begin the block of commands and done to end it.