5.1 Given a scenario, deploy and execute basic BASH scripts Flashcards

1
Q

What is the PATH variable used for?

A

Scripts should be located in one of the directories defined by the $PATH variable

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

export

A

The export command can also be used to display all environment variables:

To convert an existing local variable to an environment variable, use the export command:

[student@OCS ~]$ echo $NAME
Sarah
[student@OCS ~]$ export NAME

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

env

A

The env command displays environment variables in the current shell. Local variables are not displayed when the env command is executed

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

set

A

The set command displays all shell variables and values when executed with no arguments. The output also includes any functions that have been declared within the shell.

The set command can also be used to modify the behavior of the shell

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

printenv

A

Like the env command, the printenv command is used to display environment variables; however, the env command has additional features, whereas the printenv command is used solely to display environment variables

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

echo

A

The echo command is used to display information. Typically, it is used to display the value of variables

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

!/bin/bash

A

The first line of a script should include the path to the interpreter. For BASH shell scripts, this should be the path to the executable bash command. This path can be discovered by executing the following command:

[root@OCS ~]$ which bash
/bin/bash

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

!/bin/bash

A

The first line of a script should include the path to the interpreter. For BASH shell scripts, this should be the path to the executable bash command. This path can be discovered by executing the following command:

[root@OCS ~]$ which bash
/bin/bash

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

What is meant by sourcing scripts?

A

Most commands are executed as separate processes that have their own environment. The source command allows you to execute a bash script as if the commands within the script were executed directly on the command line

source ./functions.sh

Use the . command to perform the same function as the source command, as shown next:

[root@OCS ~]$ . ./functions.sh

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

File Blobbing

A

A file glob (also called a wildcard) is any character provided on the command line that represents a portion of a filename. The following globs are supported:

Glob

Description

*

Matches zero or more characters in a filename.

?

Matches any single character in a filename.

[ ]

Matches a single character in a filename as long as that character is represented within the [ ] characters.

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

File Blobbing

A

A file glob (also called a wildcard) is any character provided on the command line that represents a portion of a filename. The following globs are supported:

Glob

Description

*

Matches zero or more characters in a filename.

?

Matches any single character in a filename.

[ ]

Matches a single character in a filename as long as that character is represented within the [ ] characters.

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

How would you copy all files that end in .txt

A

[student@OCS ~]$ cp *.txt ~

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

What command would you use to remove all files that are four characters long?

A

[student@OCS ~]$ rm ????

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

What command would you use to view the file type for all files in the current directory that begin with a, b, or c

A

[student@OCS ~]$ file [abc]*

or

[student@OCS ~]$ file [a-c]*

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

What does this shell expansion do: ${}

A

With the variable expansion BASH feature, you can return a different value. For example, the following will return “Bob” if the $name variable isn’t set:

$ echo “Hello, ${name:-Bob}”
Hello, Bob$
echo “Hello, $name”
Hello,

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

What does this shell expansion do: $() or cmd

A

Command substitution is the process of executing a subcommand within a larger command. This is typically used to gather data and store it in a variable. For example, the following command stores the output of the date command in the $today variable:

today=$(date)
Command substitution can be performed by using one of two methods:

$(cmd)

‘cmd’

17
Q

What are exit codes?

A

When a command executes, it returns a value of success (0) or failure (>1) to the shell or script from which it was executed. This status is stored in the $? variable

18
Q

What are meta characters?

A

Metacharacters are special characters to the BASH shell. For example, file globbing characters (*, [ ], and ?) are considered metacharacters

19
Q

Positional Parameters

A

Your script can be run with arguments, like so:

./example.sh arg1 arg2 arg3
Each of the arguments is stored in $1, $2, $3, and so forth. $0 is the name of the script itself.

The special BASH variable $# contains the number of arguments passed to the script

20
Q

Escaping Characters

A

To escape a character is to have a special character (aka, a metacharacter) treated as a plain character. For example, the following disables the special meaning of the redirection character, using double quotes:

$ echo “this | that”
this | that

Here are some escaping character essentials:

Double quotes disable the special meaning of redirection characters (, and |) and globbing characters (*, ?, [, and ]).

Use single quotes to disable all metacharacters, including redirection, globbing, command substitution (the ‘ ‘ and $() characters), and variable substitution (the $ character).

Place a \ character (backslash character) before any metacharacter to make it a plain character for the BASH shell.