Programming in Unix Environment: Using the Shell

Command Line Structure

In a Unix shell, commands are typically executed by pressing Enter. However, there are different ways to structure and control the execution of commands:

  • Command Terminators:
    • A command usually ends with a newline but can also be terminated with a semicolon (;).
    • Parentheses () can be used to group commands and execute them in a subshell.
    • The ampersand (&) allows a command to run in the background, letting the user continue with other tasks.
  • Redirection and Piping:
    • The pipe (|) allows passing the output of one command as input to another.
    • The tee command captures output from a pipeline and writes it both to a file and to the standard output.

Examples:

# Grouping commands using parentheses and piping output to wc (word count)
$ (date ; who) | wc

# Capturing output in a file and continuing the pipeline
$ (who ; date) | tee output.txt | wc

# Running a long-running command in the background
$ long-running-command &

# Sleeping for 5 seconds before executing date
$ sleep 5 ; date

# Running a command in the background while executing another immediately
$ (sleep 5 ; date) & who

In the last example, who executes immediately, while (sleep 5 ; date) & waits for 5 seconds before printing the current date in the background.

Metacharacters in the Shell

Metacharacters have special meanings in Unix shells. To use them literally, enclose them in single quotes (').

Example:

$ echo '**'  # Prints ** instead of interpreting * as a wildcard

Other common metacharacters include:

  • * (Wildcard for multiple characters)
  • ? (Wildcard for a single character)
  • {} (Brace expansion)
  • [] (Character class matching)
  • $ (Variable substitution)
  • > and < (Redirection operators)
  • | (Pipe operator)

Escaping Metacharacters

If you need to use a metacharacter without its special meaning, escape it using a backslash (\) or enclose it in single quotes:

$ echo \$HOME    # Prints the string "$HOME" instead of expanding it to the home directory
$ echo 'Hello > World'  # Prints "Hello > World" instead of treating '>' as a redirection operator

Additional Notes

  • The nohup command allows a process to continue running after the user logs out.
  • Job control commands like fg, bg, and jobs help manage background processes.
  • Using &> redirects both standard output and standard error to a file.

Example of nohup:

$ nohup long-running-command &  # Keeps running even if the session is closed

Programming in a UNIX Environment: Essential Commands

Understanding Basic UNIX Commands

UNIX provides a powerful command-line environment where users interact with the system via a shell. Here, we explore some fundamental commands and concepts that every UNIX user should know.

User Identification Commands

who am i

This command displays the current user’s name along with the terminal (TTY), date, and time of login:

$ who am i
john_doe    tty1    Mar 19 10:15

whoami

This command prints only the username of the currently logged-in user:

$ whoami
john_doe

Directory Navigation

In UNIX systems:

  • The parent directory is denoted by ..
  • The current directory is denoted by .

Removing a Directory

  • rmdir <directory_name> removes an empty directory. To remove non-empty directories, use rm -r <directory_name>.

About the Shell

The shell is an ordinary program that interprets commands and executes them. It processes wildcards (like * and ?) before passing arguments to commands.

Using Wildcards in Commands

Wildcards allow flexible pattern matching in filenames.

cat Command and Wildcards

  • cat c* prints the contents of all files starting with ‘c’.
  • The * wildcard represents any string of characters.

echo Command

The echo command prints arguments to the terminal:

$ echo Hello, UNIX!
Hello, UNIX!

Wildcards with echo:

$ echo ch*
changelog checklists chapter1.txt

$ echo *
file1.txt file2.txt script.sh test.c

rm (Remove Files)

  • rm * deletes all files in the current directory (use with caution!).
  • rm *.txt deletes all .txt files.
  • rm te[a-z]* removes all files starting with te followed by a lowercase letter.

Advanced Wildcard Usage

The [ ] brackets specify a range of characters:

$ pr r[1234]*

Prints all files starting with ‘r’ followed by 1, 2, 3, or 4.

The ? wildcard matches exactly one character:

$ ls ?.txt

Lists files that have a single-character name followed by .txt.

Input and Output Redirection

UNIX allows redirecting command outputs using > and <.

Redirecting Output

  • ls > filelist saves the list of files into filelist.
  • cat file1.c file2.c > file3.c merges file1.c and file2.c into file3.c.
  • cat file4.c >> file3.c appends file4.c to file3.c.

Redirecting Input

  • pr -3 < filelist prints the contents of filelist in three-column format.
  • grep main < source.c searches for occurrences of ‘main’ in source.c.

Conclusion

Understanding these essential UNIX commands enhances productivity and efficiency when working in a UNIX-based environment. Wildcards, redirection, and basic command utilities provide a powerful toolkit for managing files, directories, and data. Master these, and you’ll navigate UNIX with ease!