Command Lists and Scripts Flashcards

1
Q

Multiple commands can be separated with a…?

A

;

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

Upon exiting, every command returns an integer to its parent called a…?

A

return value.

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

The shell variable $? expands to the return value of….?

A

previously executed command.

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

&& and || conditionally separate…?

A

multiple commands.

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

The bash shell allows users to easily run commands in a subshell, by…?

A

wrapping the command with parenthesis.

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

Why would someone want to run a command in a subshell?

A

Subshells are used to avoid side effects. What happens in the subshell should not effect the original shell’s environment (just as, in English, what happens in parenthesis should not change the surrounding sentence’s context).

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

The key to using Red Hat Enterprise Linux effectively is….?

A

automation.

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

A good Linux administrator should actually be extremely lazy when it comes to doing anything….?

A

boring or repetitive.

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

However, one important piece of your system administrator’s toolkit is still missing….?

A

scripting.

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

A script is, in its simplest form, just a text file with a…?

A

list of commands in it.

(The commands are sent through a specified program, called an interpreter, which runs each command in turn. Usually this interpreter will be the bash shell (referred to as /bin/bash or /bin/sh) and each command is an ordinary Linux command. Other interpreters allow you to use more powerful programming languages like Perl, Python and Ruby.)

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

Before you can begin writing scripts of your own, there are a few important things to remember:

A
  • The first line of your script must specify which interpreter to send instructions to. This is done with a special string called a “shebang” (pronounced “shuh-bang”), which looks like this: #!. The shebang is followed by the name of the interpreter for this script. So, for example, to use bash as your interpreter you would use #!/bin/sh or #!/bin/bash. Most scripts just use #!/bin/sh. Referring to the interpreter as #!/bin/bash enables some extra features but limits the script’s compatibility with older Unix systems and is rarely necessary.
  • Before you can run a script, you must enable the “executable” permission on it (otherwise, it’s just a text file). The command to do this is chmod u+x . This grants you (and only you) permission to run this script just like you would any other command. The chmod command will be discussed in much more detail later in this class.
  • If you created a script called foo.sh in your home directory and then just typed foo.sh you would get a “no such file or directory” error. This is because when you type a command, there is a fixed set of directories that Linux looks for that command in. These directories are referred to collectively as your PATH and, for security reasons, your PATH never includes the current directory. To solve this problem you have two choices:
    1. You can explicitly specify the location of the script by typing ~/foo.sh or ./foo.sh (“.” always refers to the current directory).
    2. You can place the script in a directory that is part of your PATH. Non-root users do not have permission to place files in most of these directories, but all users have a personal bin, to which they can write, in their home directory. So if foo.sh were moved to ~/bin it could be run by simply typing foo.sh at the command line. This is the preferred technique.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Suppose you are an administrator who often needs to see which users are logged into the system. This information can be obtained by running the….?

A

w command (yes, that’s the whole thing) but, while this provides a nice summary of who is logged into the system, it does not print the time at which this snapshot of user activity was taken. Another command, called date prints out the current date and time, but no user information. If only you could combine those two commands into one….

Suppose you created a script called wdate.sh in your personal bin directory:

see https://academy.redhat.com/courses/rha030-6.1/rha030_bash_cmdlists.html for the example.

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

Every process in Linux has a…?

A

Lifespan.

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

All processes start at the request of another…?

A

process (often a shell)

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

The requesting process is referred to as the…?

A

parent, and the newly born process the child

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

Usually, the child process performs its duties (which might involve spawning children of its own), and then…?

A

elects to die.

17
Q

An exiting process leaves a little piece of information behind when it dies, called the…?

A

process’s return value, or exit status.

18
Q

The parent process is responsible for collecting the…?

A

return values of dead children.

19
Q

Return values come in the form of integers which range from..?

A

0 to 255

20
Q

Programs are free to choose what….?

A

value to return upon exiting

21
Q

Often, what implications are meant by different return values are…?

A

part of a program’s well defined interface, and are documented in that program’s man page. (If you are familiar with the diff command, the “DIAGNOSTICS” section of its man page provides an example)

22
Q

A Linux-wide (and Unix-wide) convention is that a program returns a…?

A

0 to imply “success” at whatever it was trying to accomplish, and a non zero return value to imply some form of failure.

23
Q

The bash shell stores the return value of the previously executed command in a special variable called…?

A

simply ?

24
Q

The bash shell uses && and || to…..?

A

join two commands conditionally

25
Q

When commands are conditionally joined, the…?

A

first will always execute. The second command may execute or not, depending on the return value of the first command.

For example, a user may want to create a directory, and then move a new file into that directory. If the creation of the directory fails, then there is no reason to move the file.

By coupling two commands with &&, the second command will only run if the first command succeeded (i.e., had a return value of 0). This is similar to the “and” operation found in may programming languages. In the above example, the mkdir command succeeded, and the file was moved.

26
Q

Similarly, multiple commands can be combined with ||. In this case…?

A

bash will execute the second command only if the first command “fails” (has a non zero return value). This is similar to the “or” operator found in programming languages. In the following example, elvis attempts to change the permissions on a file. If the command fails, a message to that effect is echoed to the screen.