CST8177 - Lab #4

Student Name:

Student Number:

Lab section:




Shell Features and File Globs

In-Lab Demo

  1. Execute a command line using redirection

  2. Demonstrate shell features, including at least variable expansion and command substitution

  3. Demonstrate file globbing of several varieties

Working with the shell

In Unix and Linux, "shell" is the term used to describe the command-line interpreter. In this course, we will use the bash shell because of its wide popularity on Linux, although there are other shells available such as csh and sh.

Exercise #1 (review): Working with shell features

File name completion

Purpose

Command line example

Complete a file name (to avoid typing long file names or making a typo in a complex name)

cp .bashrc abc.bashcopy

vi abc[TAB]

Aliasing

Purpose

Command line example

List all aliases

alias

Create an alias

alias ll="ls -l"

Remove an alias

unalias ll

History Mechanism

Purpose

Command line example

Display the current history buffer

history | less

Re-execute the last command

!!

Re-execute any previous command using keyboard keys

Use the up and down arrow keys

Re-execute any previous command using the event number

!n (where n is the event number as listed in the history output)

Edit a command

Use the up and down arrow keys to select the command and edit it




Redirection & command grouping

A file descriptor is a numeric identifier (small unsigned integer) that a UNIX system uses to identify an open file that is attached to a process. Note: A file descriptor is created by a process through issuing an open() system call for the file name. A file descriptor ceases to exist when it is no longer held by any process. By default a process is set up with three file descriptors:

Note that pts is used for a terminal window, a pseudo-terminal. An actual terminal will be tty.

Create an empty directory and touch x to create a file in it. Use the command ls x y to provide examples of the actions listed below. Use only one command for each.

Purpose

Command line solution

Redirect stdout to a file named out and view the file.


Redirect stderr to a file named err and view the file.


Redirect the output such that stdout is written to out and stderr is written to err from the single command.


Append stdout to a file named out and view the file.


Append stderr to a file named err and view the file.


Redirect stdout to a file named out and redirect stderr so it is written to the same file as stdout (but do not name the file for stderr). View the file out.


Redirect both stderr and stdout to a file named out and view the file.


Reverse the redirection sequence in the question above and repeat. What's the difference, if any?




There are also some useful command separators you should be aware of. Try each of these (to cause mkdir dir to fail, create it twice in the same directory):

Exercise #2: Working with shell expansion/substitution

Execute the commands listed below, record the output here, and examine the output.

Brace expansion

How many directories have been created (excluding teacher)? _______________________

Tip: Use find /home/teacher | wc -l and adjust as needed.

Tilde expansion

Variable expansion

Command substitution

Briefly describe the significant differences ____________________________________________

________________________________________________________________________________________

Arithmetic Expressions

File globbing

Exercise #3: Working with metacharacters

Exercise #4: Working with quotes

Remove the special meaning that the space, the newline, and the # have for the shell.

sailor[Enter]