User Tools

Site Tools


Sidebar

projects

wcp1 (due 20240828)
wcp2 (due 20240904)
pct0 (bonus; due 20240905)
pct1 (bonus; due 20240905)
pct2 (due 20240905)
abc0 (due 20240906)
dac0 (due 20240911)
pct3 (bonus; due 20240911)
wcp3 (due 20240911)
gtf0 (due 20240918)
pct4 (due 20240918)
wcp4 (due 20240918)
pct5 (bonus; due 20240926)
tpb0 (due 20240926)
wcp5 (due 20240926)
gfo0 (due 20241002)
pct6 (due 20241002)
tpb1 (due 20241002)
wcp6 (due 20241002)
pct7 (bonus; due 20241009)
usr0 (due 20241009)
wcp7 (due 20241009)
bwp1 (bonus; due 20241016)
pct8 (due 20241016)
tpb2 (due 20241016)
wcp8 (due 20241016)
pct9 (bonus; due 20241023)
wcp9 (due 20241023)
wpa0 (due 20241023)
fwg0 (due 20241030)
gfo1 (due 20241030)
pctA (due 20241030)
wcpA (due 20241030)
pctB (bonus; due 20241106)
upf0 (due 20241106)
wcpB (due 20241106)
ldg0 (due 20241113)
pctC (due 20241113)
wcpC (due 20241113)
pctD (bonus; due 20241120)
wcpD (bonus; due 20241120)
bwp2 (bonus; due 20241204)
gfo2 (due 20241204)
pctE (bonus; due 20241204)
wcpE (bonus; due 20241204)
EoCE (due 20241216)
haas:fall2024:unix:labs:lab4

Corning Community College

CSCS1730 UNIX/Linux Fundamentals

Lab 0x4: UNIX Shell Basics

~~TOC~~

Objective

Introduction to the UNIX shell and its capabilities.

Reading

In “UNIX for the Beginning Mage”, please read:

  • Chapter 6, pages 59-75.

In “Harley Hahn's Guide to UNIX and Linux”, please read:

  • Chapter 7 (“Using the Keyboard With UNIX”, pages 131-160).
  • Chapter 8 (“Programs to Use Right Away”, pages 161-188).
  • Chapter 10 (“Command Syntax”, pages 223-238).
  • Chapter 11 (“The Shell”, pages 239-254).
  • Chapter 12 (“Using the Shell: Variables and Options”, pages 255-276).
  • Chapter 13 (“Using the Shell: Commands and Customization”, pages 277-326).
  • Chapter 14 (“Using the Shell: Initialization Files”, pages 327-344).
  • Chapter 15 (“Standard I/O, Redirection, and Pipes”, pages 345-372).

In Learning the UNIX Operating System, 5th Edition, please read the following:

  • Chapter 5 (“Redirecting I/O”, page 87-96).

Reference

Additional commentary on the $PATH environment variable:

Background

Operating Systems provide a means of interfacing to the hardware of a computer (as well as interacting with other components of the OS). In a traditional UNIX system, there are 2-3 different levels of the OS:

  1. Kernel - the core of the OS. It handles everything- manages I/O, etc.
  2. Drivers - components that instruct the kernel how to function or deal with a piece of hardware.
  3. Userspace - non-kernel level. System applications, utilities, files. Users exist here, hence the name “user space”

This can vary depending on how a system is implemented. In some recent UNIX-like operating systems, it is possible to patch and recompile the whole kernel- including whatever drivers are needed. These same drivers can also exist on the filesystem as loadable modules. So different interpretations may vary.

At the userspace level can be found various processes running in memory. Daemons, or application servers, handle various services or functionalities of the system. When all is said and done- the users of the system have access via a shell.

The shell, or command interpreter (or command processor), is the user's interface to the operating system. It is responsible for:

  • interactive use
  • customization
  • programming

All of you have taken advantage of the first two. When you type ls and hit enter, the shell attempts to run a program called ls, and if it exists in your path, will execute it. If you want a more detailed listing, you provide the -l argument. The shell is responsible for parsing what you type at the command line and sending that information to the program you run. This is the interaction you have with the shell.

The shell can also provide useful settings for programs to use to better customize their operation, such as to better fit your terminal screen.

The shell's behavior can also be modified, or customized through built-in commands or through shell-specified configuration files (which include some dotfiles that exist in your home directories– these are also called personal configuration files).

How you use the system can depend on the particular shell you are using. Throughout the history of UNIX, several types of shells have appeared- the most common two being the bourne and C shells. Both families of shells are still in use today.

The bourne shell (or some functional equivalent) is found on practically every UNIX system. It is referred to as sh and usually lives in the /bin directory. The bourne shell is often preferred for programming/scripting.

The C shell (csh) was designed by the BSD people as an alternative to sh. While it possesses much of its functionality, csh's syntax can be different enough to make it incompatible with bourne shell scripts. The C shell is often preferred for interactive use.

On Lab46, the default shell is bash, which stands for (bourne again shell). It is a bourne-derived shell. It comes out of the Free Software Foundation's GNU project, and while it is a fully functional sh-compatible shell, it also has several improvements that enhance the user's session. bash has also become the de-facto shell to use on a Linux system, so that is what we will primarily use in this course.

Control Characters: As discovered in previous assignments, special control characters exist that possess special powers.

Control characters are issued by pressing down the CTRL key while hitting another key. It is often abbreviated as CTRL-X or ^X, where X is the desired character (usually lowercase, although represented in uppercase).

A list of common characters is as follows:

Control Code System Code Description
CTRL-C INTR interrupt
CTRL-D EOF issue end of file character
CTRL-G sound bell
CTRL-H BS send backspace
CTRL-J LF send linefeed
CTRL-L refresh screen
CTRL-M CR send carriage return
CTRL-Q XON start code*
CTRL-S XOFF stop code*
CTRL-V escape the following character
CTRL-Z SUSPEND suspend current job
CTRL-[ ESC send escape character

(*) NOTE: although most terminals are too fast, in the old days XON/XOFF was commonly used to halt all movement on the screen and restart it again. For the hardware people, XON/XOFF is also software flow control as used by some devices, including modems.

Dot-Files

In UNIX, hidden files are files whose names begin with a dot (.). These files, known as dotfiles, are used on login or for configuration purposes.

Everytime you login to the system, some of these files are parsed to configure your session.

When logging in with the bash shell, which is the default shell on Lab46, it will automatically search through a range of files, including .bash_profile. In this file, you can set up environment variables and command aliases for use in your session.

Aside from shell startup files, you may have additional dotfiles that serve a special purpose.

Below is a list of common dotfiles that may exist in your home directory:

dotfile description
.bash_profile The first personal initialization file bash searches
.bashrc Session personalization file called by .bash_profile
.cshrc A personal initialization file for the csh/tcsh shells
.exrc A configuration file for vi/ex
.signature Text file containing a signature banner for e-mail
.plan A personal banner file that is displayed on finger(1) lookups
.forward A file used in automatic e-mail forwarding
.pinerc A configuration file for pine
.vimrc A configuration file for vim

By editing the particular dotfile of an application, you can configure how that application program behaves.

1. Do the following:
a.At your prompt, list the files in the base of your home directory.
b.Now list them using the -a argument to ls.
c.Do you see new files?

In addition to the dotfiles, you should also see the . and .. directory files that were covered in Lab #2.

Configuring a dotfile

Here we will look at customizing some settings with a particular dot file.

2. Using vi, edit your .signature file. Personalize it with the following information:
a.Include your name.
b.Put in your current courses.
c.Add some quote or other information (personalize it).

Remember to keep your .signature file relatively short and organized- try to limit it to 3-5 lines.

SAVE AND EXIT.

Dotfile Exploration

3. Do the following:
a.Look at your ~/.bash_history file.
b.What do you think this file does?

Environment Variables

A major feature of the shell is its ability to store data in memory. There are several important environment variables used to modify the behavior of your terminal and login session. Some common ones are:

  • $PATH
  • $HOSTNAME
  • $USER
  • $TERM
  • $SHELL

Environment variables are identified with the $ prefixing an ALL-CAPITAL variable name. This is to visually distinguish them. Lowercase names can be used for personal variables that are declared.

4. Use the printenv utility to list your environment variables and display their contents.
a.What is your shell?
b.Where is your mail file or directory?
c.What type of terminal are you using?
d.What is your path?

The echo utility can also be used for displaying the contents of environment variables

5. Do the following:
a.Echo the PATH variable
b.How did you do it?

To set an environment variable in sh, you need to declare it. Prior to that, it doesn't exist.

lab46:~$ echo $var1

lab46:~$ 

So, to declare a variable var1 with the contents “UNIX SHELL” you would do the following:

lab46:~$ var1="UNIX SHELL" 
lab46:~$ 

To view the contents, use the echo utility:

lab46:~$ echo $var1 
UNIX SHELL
lab46:~$ 

Command Aliases

The shell also provides you with the ability to set up your own commands, or aliases. If you have a favorite incantation that you use on a regular basis, you could make an alias so that you would not have to type it each time.

6. You can view/create aliases with the alias command. Type alias and hit enter.
a.What is your current alias for the ls utility?
b.What argument(s) to ls are aliased?
c.Using the man page or your books, what do the argument(s) do?

removing aliases

The unalias command is used to remove aliases. Let's remove the alias for ls:

lab46:~$ unalias ls
lab46:~$ 
7. Now type ls and press enter:
a.Is your output different? How?
b.Is there still an alias for ls?
c.Using your resources, what argument to ls will append an indicator to (classify) files?

creating aliases

To create a new alias, we go back to the alias command:

lab46~$ alias nameofalias="utility -options --options"

* items in italics will vary from alias to alias.

Please note that the bourne shell requires NO spaces around the equal sign. If you pad any spaces around the equal sign, you will encounter an error.

8. Do the following:
a.Recreate an alias for ls, using the argument discovered in the previous question, as well as any original arguments.
b.What did you type to accomplish this task?
c.Is ls listed again as an alias?
d.List the files in your home directory. Is there any change?

All aliases last for your current login session only. When you log out the aliases are erased. In order to preserve desired aliases, they are placed in one of your home directory's dot-login files.

History

The shell provides a history mechanism for keeping track of command-lines you have entered. You can view your history with the history command.

The ! (pronounced “bang”) character has a special use with command history– history recall.

By entering: !history_number

you will run the indicated command represented by history_number

9. At your prompt, enter the following commands:
a.lab46:~$ echo “###“
b.lab46:~$ echo “You got it”
c.lab46:~$ echo ”—“
d.Type history. Do you see the echo statements listed?
e.What number is the “You got it” line?
f.Using the history recall operator, recall the number of the command in part b.
g.Do you “got it” again?

The up and down arrow keys also serve a useful purpose- they allow you to cycle through your command history!

10. Let's play with this feature:
a.Hit the up arrow once.
b.What happens?
c.Hit up some more. Keep going until you get to an ls; add the -l option and press enter.
d.Does this feature seem useful? Why?

Tab Completions

Perhaps one of the more useful features, tab completions can complete commands and pathnames for you. How to do you use it? Just hit the tab key.

11. To take advantage of tab completions, let's enter some commands (don't hit enter unless instructed):
a.lab46:~$ cd /u ← hit tab
b.What happens?
c.[same line]: b ← hit tab
d.What happens?
e.Press the enter key
f.What is your current working directory?
g.lab46:/usr/bin$ ls v ← hit tab
h.Does it beep?
i.Press tab again.
j.What happens?
k.Supply more characters to tab complete for the vimtutor utility
l.What did you do?

If you haven't provided unique enough information to the command-line, the tab completion cannot complete. Hitting tab a second time will give you a list of possible choices.

Conclusions

This assignment has activities which you should tend to- document/summarize knowledge learned on your Opus.

As always, the class mailing list and class IRC channel are available for assistance, but not answers.

haas/fall2024/unix/labs/lab4.txt · Last modified: 2014/02/09 15:00 by 127.0.0.1