Command Lists and Scripts Flashcards
Multiple commands can be separated with a…?
;
Upon exiting, every command returns an integer to its parent called a…?
return value.
The shell variable $? expands to the return value of….?
previously executed command.
&& and || conditionally separate…?
multiple commands.
The bash shell allows users to easily run commands in a subshell, by…?
wrapping the command with parenthesis.
Why would someone want to run a command in a subshell?
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).
The key to using Red Hat Enterprise Linux effectively is….?
automation.
A good Linux administrator should actually be extremely lazy when it comes to doing anything….?
boring or repetitive.
However, one important piece of your system administrator’s toolkit is still missing….?
scripting.
A script is, in its simplest form, just a text file with 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.)
Before you can begin writing scripts of your own, there are a few important things to remember:
- 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.
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….?
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.
Every process in Linux has a…?
Lifespan.
All processes start at the request of another…?
process (often a shell)
The requesting process is referred to as the…?
parent, and the newly born process the child