Shell & Friends

Class: CSCE-313


Notes:

I/O redirection in shell

When a program is executed in Linux, it usually has access to three special files: stdin (fd 0), stdout (fd 1), and stderr (fd 2). They correspond to objects cincout, and cerr that you're familiar with from C++ programming. The first is connected to the keyboard, while the second and third are attached to the screen or terminal. See Chapter 6, The Linux Command LineLinks to an external site. on IO Redirection. Answer the following questions.


Question 1

To redirect the contents of a file into a command such as md5sum, we can do? In other words you want md5sum to read from file when it's reading from its stdin.

$ md5sum ____ file

Options:

Overall explanation:

Question 2

To redirect the output of a command such as date into a file, we can do?

$ date ____ file

Options:

Overall explanation:

Question 3

To append the output of a command such as uname to a file, we can do?

$ uname -a ____ file

Options:

Overall explanation:

Question 4

To redirect only the error messages of a command such as find to a file, we can do? That is, we want errors of the command to go to the file, but the output of the command to go to the terminal.

$ find /etc ____ file

Options:

Overall explanation:

Question 5

To redirect both the output and error messages of a command such as du, we can do? Try various things in your Linux shell to see which one works.

$ du /etc ____ file

Answer:

$ du /etc > file 2>&1

Question 6

To redirect the output of a command such as ls to the input of another command such as grep, we can do:

$ ls /etc ____ grep host

Options:

Overall explanation:


Date Formatting

Two useful features in a typical Unix shell are pathname expansion and brace expansion. Given the following output of ls, where all file names are in the format YEAR-MONTH.

2002-01  2002-07  2003-01  2003-07  2004-01  2004-07  2005-01  2005-07  2006-01  2006-07
2002-02  2002-08  2003-02  2003-08  2004-02  2004-08  2005-02  2005-08  2006-02  2006-08
2002-03  2002-09  2003-03  2003-09  2004-03  2004-09  2005-03  2005-09  2006-03  2006-09
2002-04  2002-10  2003-04  2003-10  2004-04  2004-10  2005-04  2005-10  2006-04  2006-10
2002-05  2002-11  2003-05  2003-11  2004-05  2004-11  2005-05  2005-11  2006-05  2006-11
2002-06  2002-12  2003-06  2003-12  2004-06  2004-12  2005-06  2005-12  2006-06  2006-12

Using bash's expansion syntax, complete the following prompts. See Chapter 7, The Linux Command LineLinks to an external site. for more information.


Question 7

To list all the files from 2002, we could use the following expressions (select all that work correctly):

$ ls ____
2002-01 2002-02 2002-03 2002-04 2002-05 2002-06 2002-07 2002-08 2002-09 2002-10 2002-11 2002-12

Options:

Overall explanation:

Question 8

To list all the files from the month of December, we could do:

$ ls ____
2002-12 2003-12 2004-12 2005-12 2006-12

Options:

Overall explanation:

Question 9

To list all the files from the month of January to June, we could do:

$ ls ____
2002-01  2002-04  2003-01  2003-04  2004-01  2004-04  2005-01  2005-04  2006-01  2006-04
2002-02  2002-05  2003-02  2003-05  2004-02  2004-05  2005-02  2005-05  2006-02  2006-05
2002-03  2002-06  2003-03  2003-06  2004-03  2004-06  2005-03  2005-06  2006-03  2006-06

Options:

Overall explanation:

Question 10

Are the following two pipelines equivalent?

$ du -h /etc 2> /dev/null | sort -h > output.txt
$ du -h /etc | sort -h > output.txt 2> /dev/null

Options:

Overall explanation:


Unix Shell Variables

Two other important features of a typical Unix shell are variables (i.e., parameter expansion) and command substitution. Using these features, complete the following prompts:


Question 11

To print the value of the HOME variable, we could do:

echo _____

Answer:

echo $HOME

Question 12

To set the value of the SHELL variable to /bin/bash, we could do:

Answer:

SHELL=/bin/bash

Question 13

To compute the md5sum of the ls command while also looking up its location, we could do? See command substitution in the shell. Also look at the where built-in command in the shell.

Answer:

md5sum $(which ls)

Question 14

To print all the environment variables in the shell, we could do:

Answer:

printenv

Question 15

What is the key difference between these two commands?

cmd > out.txt 2>&1
cmd 2>&1 > out.txt

Options:

Overall explanation:

  1. cmd > out.txt 2>&1
    • > sends stdout to out.txt
    • 2>&1 sends stderr to the same place as stdout → both go to out.txt
  2. cmd 2>&1 > out.txt
    • 2>&1 first sends stderr to the current stdout (the terminal)
    • then > sends stdout to out.txt
    • → stderr still goes to the terminal, stdout goes to the file.

Question 16

Assume you're using the bash shell. If you're unsure, type echo $SHELL in your terminal. Suppose the directory only contains files a1.txt, a2.txt, b1.txt. What does this command print?

 echo "*.txt"

Options:

Overall explanation:

Question 17

What is the key difference between the following two commands?

$ ls *.txt
$ ls "*.txt"

Options:

Overall explanation:

Question 18

Assume your current directory contains only the following two files: report_v1.txt, and report_v2.txt. Note that report_v3.txt does not exist in the directory. Which of the following commands will result in an error message (e.g., "No such file or directory") being displayed?

Options:

Overall explanation: