6.1 Combining Commands Flashcards
A ________ is several commands that are normally written out one-by-one, chained together in order to create a new command.
A **compound command** is several commands that are normally written out one-by-one, chained together in order to create a new command.
What are the three components of a command?
program -options arguments
- The **program**, which is a binary program that you are running.
- The program’s **options**, which changes the behavior of the program being run in the first part of the command.
- The **arguments** provided to the program, usually a reference to a directory or file that you want the program to act on.
Compound commands typically follow this format:
program -options arguments | program -options | program -options | program -options
Breakdown this command:
file $(find / -iname *.txt 2>/dev/null) > ~/Desktop/text_files ; tail ~/Desktop/text_files
- Searches the entire computer for files ending in .txt;
- Verifies that the files found are text files; Ignores any errors it comes across; Creates a list of all found files before saving the list to the desktop
- Finally, it will open the file and print the last ten lines that were added.
Explain these command:
- ls > list.txt
- > list.txt
- ls >> list.txt
- This command takes the output of the
ls
command and sends it into a new file namedlist.txt
. _If_ the filelist.txt
already exists, it is overwritten with the output of thels
command. - In this case, we didn’t put a command in front of
\>
so there is no output to send to thelist.txt
file.
* However, the file is still written, just with no output. So a blank file is written. _If_ the filelist.txt
exists, it is overwritten with nothing. - >> will append the output of the
ls
command to thelist.txt
file.
- If the
list.txt
file does not exist, it is created. - Therefore, using
\>\>
instead of\>
is always safer, unless you want the file to be overwritten.
What do pipes ( | ) do?
The pipe ( |
) takes the output of one command and sends it to the input of another command.
Explain this command:
ls -l | grep *.txt
The pipe ( |
) takes the output of one command and sends it to the input of another command.
-
ls -l
creates a list of files. -
|
pipes the list fromls
into the command that follows. -
grep
searches the files fromls
for the string that follows. -
*.txt
matches any file that ends with.txt
.
Compound commands with pipes typically follow this format:
program -options arguments | program -options | program -options | program -options
What are some common programs users pipe to:(5)
- | head prints only the first 10 lines of output.
- | tail prints only the last 10 lines of output.
- | sort sorts the output alphabetically.
- | sed searches and replaces parts of the output.
- | awk display only specified parts of the output.
Explain this command:
cat /etc/passwd | grep sysadmin | awk -F ‘:’ ‘{print $6}’
-
cat /etc/passwd
dumps the contents of/etc/passwd
to output. -
|
pipes that output into the command that follows. -
grep sysadmin
displays lines that containsysadmin
. -
|
pipes that output into the command that follows. -
awk -F ':' '{print $6}'
prints only the sixth field of the line. -
awk
usually looks for a space to use as afield separator
, but in this case we want it to separate the line by a colon, because/etc/passwd
uses colons to separate its fields.
In addition to |, you can use ____ to run a series of commands back to back.
You can also use a ;
to run a series of commands back to back.
Rather than typing the following, how would you use ; :
bash $ mkdir dir $ cd dir $ touch file $ ls -l -rw-r--r-- 1 user user 0 Sep 4 15:33 file
mkdir dir; cd dir; touch file; ls -l
Each command will happen in succession.
- First, the mkdir
command, then cd
, touch
, and finally ls
.
Compound commands using ;
typically follow this format:
program -options arguments ; program -options arguments ; program -options arguments ; program -options arguments
For example: `mkdir dir; cd dir; touch file; ls -l
- The output would be:
bash $ mkdir dir; cd dir; touch file; ls -l -rw-r--r-- 1 user user 0 Sep 4 15:33 file
If you only want to run the second command if the fist command was successful, what operator would you use?
&&
Explain this command:
mkdir dir && cd dir && touch file && ls -l
cd
would only run if mkdir
were successful, touch
would only run if cd
were successful and ls
would only run if touch
were successful.
Compound commands using &&
typically follow this format:
program -options arguments && program -options arguments && program -options arguments && program -options arguments
Explain these operators:
- >
- >>
- |
- ;
- &&
- > to create files with the output of a command.
- >> to append the output of a command to a file. Creates a file if the file does not exist.
- | pipes the output of one command into another command.
- -; to chain commands together in succession.
- && to chain commands together. The second command runs only if the first command was successful.
Compound commands are useful but _do_ require a lot of typing. If you use a compound command often, it might be nice to save it somewhere so you can easily reference it. You can do this by creating an ________.
Alias
An _______ is a shorthand or custom command that you can define, which will launch any command or compound command, including arguments and redirection.
An **alias** is a shorthand or custom command that you can define, which will launch any command or compound command, including arguments and redirection.
In computer programing, a ________ is a location that stores some kind of data.
In computer programing, a variable is a location that stores some kind of data.
- We can think of it as a box that holds something so you can refer to it later.
- If you no longer need what is in the box, you can overwrite its contents with new contents.
Bash has a number of built-in variables called _______. They are also known as _______.
Bash has a number of built-in variables called **environment variables**. They are also known as **shell variables**.
True or False:
Built-in variables are always defined with all lower case letters.
False
They are always defined with all upper case letters. For example, $PWD
is an environment variable that returns the pwd
command.
What do these built-in variables do?
echo "My name is $USER"
echo "My home directory is $HOME"
echo "The name of my computer is $HOSTNAME"
echo "My type of computer is a $MACHTYPE"
-
echo "My user ID is $UID"
:
-
echo "My name is $USER"
: Provides the user name of the current user. -
echo "My home directory is $HOME"
: Provides the home folder of the current user. -
echo "The name of my computer is $HOSTNAME"
: Provides the name of the computer. -
echo "My type of computer is a $MACHTYPE"
: Provides the type of computer -
echo "My user ID is $UID"
: Provides theUID
of the current user.
________ in bash refers to any time something on the command line expands or morphs into something else.
**Expansion** in bash refers to any time something on the command line expands or morphs into something else.