CS 497C - Introduction to UNIX Lecture 4: Understanding the UNIX Command Chin-Chih Chang chang@cs.twsu.edu The PATH: Locating Commands * UNIX obtains the list of directories that has to be searched from of an environment variable – PATH. * If you evaluate the value of PATH, you'll find a directory list separated by colons: $ echo $PATH /bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:. The PATH: Locating Commands * There are five directories in this list, and when you issue a command, the system will search this list in the sequence specified to locate and execute it. * . indicates the current directory. * To know in which directory the command is located, you can use the type command. $ type ls ls is /bin/ls Internal and External Commands * The agency that actually does all this work is known as shell - a command interpreter. * Since ls is a file having an independent existence in the /bin directory (or /usr/bin), it is called an external command. Most commands are external in nature. * The commands built in shell are known as internal commands. Internal and External Commands * The type command itself is a shell built-in. Whether or not you are able to execute it depends on the shell you use. * In some versions of the C shell, the type command won’t work. In this case, which or whereis can be used. * The shell uses its internal command even though there is an external command of the same name. Command Structure * An UNIX command is composed of two parts: command and argument. * Commands and arguments are separated by any number of spaces or tabs know as whitespace. $ ls-l ls-l: Command not found. * The argument that begins with a - symbol is called an option. Options and Filenames * Every command has a fixed set of options. * The command with its arguments and options is entered in one line that is referred to as the command line. * Some commands accept a single filename, some accept more than one filenames. * Wrong options will be indicated. * An option can have its own arguments. * pine -f mail-may Combining Options * Options that begin with a – sign can normally be combined with only one - sign. * For instance, this command has three options: ls -l -a -t * The -l option provides most details of a file's attributes. The -t option sorts files according to the time of modification. The -a option lists the hidden files. Combining Options * You can combine these options in this way: ls -lat ls -atl * This facility reduces your typing load. The shell parses (break up) the option combination into separate options. * Some commands won't let you combine options in the way you did just now. tar -cv -f /dev/fd0 -b 18 Combining Options * There are four options here, but two of them (-f and -b) has their own arguments. These arguments are called option parameters. * We combined the -c and -v options here. We can combine the -f and -b options, provided that their own parameters are placed in the same sequence. tar -cvfb /dev/fd0 18 * The following one won't work: tar -cvfb 18 /dev/fd0 Exceptions and Variations * All commands don’t compulsorily use options and arguments. * Commands like clear don't accept any argument. * The who and date commands may or may not be specified with arguments. Exceptions and Variations * The ls command permits more variations: - Without any argument (ls) - With only one option (ls -l) - With only filenames (ls chap01 chap02) - Using a combination of both (ls -la chap01 chap02) - This text discusses commands that use options. The vast majority will conform to the option rules that have been discussed but there are some exceptions. Flexibility of Command Usage * UNIX allows you to specify more than one command in the same command line. Each command is separated from the other by a ; (semicolon): who; ls -l * The ; is one of special characters that are understood by the shell. These special characters are known as metacharacters. A Command Line Can Overflow * When a command has more characters than the terminal width, it simply overflows to the next line. * You may want to spread the command into multiple lines. The shell issues a secondary prompt, usually > (or ?). $ echo "This is a two-line > text message" In C shell you have to enter a \ (backslash). Flexibility of Command Usage * Subsequent commands can be entered at the keyboard without waiting for the prompt. There is a type-ahead buffer (a temporary storage area) which stores all these commands and passes them on for execution. * A command may not behave in the way you want because UNIX comes in many flavors. Flexibility of Command Usage * The uname can print system information. $ uname Linux The uname -r command shows the version number of the kernel. $ uname -r 2.2.19