The three major parts are
login: _In fact, the exact text for the prompt could be completely different from the text shown here. Just look for the string login:. This is the UNIX way to request input. In some cases, you will see a welcome prompt in addition to the login prompt:
Welcome to Emu mpsi login:The login prompt is UNIX's way of asking you for your login name. For instance, my login name is kbh, so I would type kbh at the prompt. Generally, the login name (usually your initials) is assigned to you by your UNIX system administrator. There is nothing magical about a login name being your initials; it could your first name, the first two characters followed by your last name, or something else required by the policies at your UNIX site.
Tip: The login name is also referred to as the user name.
Type in your user name and press the Enter key. The Enter key is also called the Return key on some keyboards. After you type in your user name, you will be asked for a password. If you are not assigned a password by your UNIX system administrator, you may not be presented with this prompt.
login: kbh <Enter> password: _ (nothing will appear as you type; <Enter>The text (kbh) after the login prompt is my user name. After typing this in, I pressed the Enter key (<Enter>). Remember that your user name may will be different.
At the next line, UNIX requests a password with the password key. Type in your password at this prompt and press the Enter key. The combination of login name and password serves the purpose of uniquely identifying you to UNIX as an known user. Do not type in the string <ENTER>, just press the Enter key.
Note: My password, for example, is iamhere. Nothing appears
as you type in your password. This invisibility prevents others from looking over your
shoulder as you type in your password. It also means, however, that you have to
type your password without seeing what you type on the screen. Your password is
your secret key word into UNIX.
If this is the first time you are logging in to UNIX, you will have been
assigned a password. Your password is easy to change after you log in.
Depending on your site's policies, you may actually be allowed to continue
without a password.
Congratulations! You are logged into the UNIX operating system, and UNIX is ready to accept your commands The $ sign (for the Bourne and Korn shells) is your prompt. If you are using another shell, your prompt could be the % character, the > character, or a combination of characters.
The exact text users see when logging in differs from system to system. Some systems give the time of day, the mail status, and so forth.
Press the Enter key a few times, and you should see the prompt displayed as many times as you press Enter. The following shows an example of this:
$ $ $By pressing the Enter key several times, you are presented with a prompt indicating that UNIX is ready for more commands. $ _. The prompt can be thought of as "What now, master?" for egocentric users, or "Well, now what do you want me to do?" for meeker users. The prompt will always reappear after you finish using a UNIX command.
Caution: Getting on a system with an account and password is perhaps the hardest thing for new users, and it might be the biggest hurdle you will face when you work with UNIX.
Note: UNIX distinguishes lower- and uppercase. For example, in UNIX, the string EXIT is different from exit.
Thus, if your password were krlpt2 and you typed in krlll2, you would not be allowed access. Similarly, typing in an invalid user name would result in the same problem. See the following examples.
Welcome to Emu mpsi login: hbg password: (as before, nothing is shown while you type your password) Login Incorrect mpsi login:If you mistyped either your login name or password, you would not see the $ sign. Instead, you would see the word Sorry or the string Login Incorrect, and would be asked for your login name again.
See the following for another example.
Welcome to Emu mpsi login: Login timeout after seconds.Some systems have a timeout (a set time limit) for your to enter you user name after being presented with the login prompt. The timeout is generally set to 60 seconds but can be set to another time limit by your UNIX system administrator. If you see the timeout message, press Enter and you should be presented with a login name as before.
On some secure sites, especially using dialup lines, you will be allowed to log in three times before the system will disable logins on that particular terminal for a long time. Usually this is done on sites where security is an issue and the disable timeout is to prevent someone from trying to log in without having the proper authority. So, if you are particularly dyslexic one day and find the system not responding to your fourth attempt at logging in, contact your system administrator for help.
If UNIX found that your login name and password matched, it lets you log into the system. UNIX encrypts the password you typed in and compares it with the list of encrypted passwords it keeps on disk. The UNIX standard password scheme makes it very difficult to go from an encrypted password back to the original string. So it's faster and easier to compare the encrypted results of passwords.
UNIX then does some housekeeping and initializes your environment for you by starting up a shell for your login session. The shell provides a prompt and waits for your commands. Your environment includes your current directory and other variables about you as long as you're on UNIX. For more information, see Day 2, "UNIX Tools," and Day 3, "Shell Variables,"respectively.
Compiled code is generated in several stages, which includes creating the source file, compiling it with a compiler for language, and linking it with libraries of other code modules. The end product of these steps is a compiled binary file.
Shell scripts are text files with a set of UNIX commands that can be edited via a text editor and can be run without going through a compile cycle. Shell scripts are covered extensively in Days 8 through 14. The primary disadvantage of shell scripts (when compared with binary files) is that they run slower than a functionally equivalent, compiled program. Compiled code is better-suited for the kernel because it contains pre-translated read to run code. The shell script has to be interpreted by the shell during execution. UNIX shell scripts provide a flexible, fast way of hooking other scripts and programs together.
When using UNIX to perform a task, you have to decide how to proceed. Do you write a program, a shell script, or both? For example, to do a matrix inversion on a data file, you would be better off writing a compiled program. To do directory operations with types of files, you would write a shell script. In the latter case, your shell script will call other shell scripts, built-in commands, or programs on disk. For example, writing a shell script to do a sort is going to be much slower than writing a shell script that calls the UNIX compiled program 'sort" on disk to do the task for you.
UNIX utilities (such as the shell, binary programs, and shell scripts) reside on disk and are loaded into memory by the kernel when required. When a program is loaded into memory, it is called a process. Naturally, all of the programs on disk cannot be loaded into memory at one time because memory is an expensive commodity and is generally much smaller than disk space. UNIX loads only the programs and data on an as-needed basis. If UNIX sees that there is not enough memory for a newly loaded program, it will make some room for the new program by taking processes from memory to a special area on disk called a swap space. This action is called swapping. When a program finishes running, it is marked as the first one to go when a new program is loaded.
Note: All UNIX utilities do not necessarily have to reside on disk. They can exist on another disk, or even another machine. However, via a special program called mount, UNIX enables other machines to appear as though they are part of a machine. Using mount on disks is beyond the scope of this book. However, keep in mind that the next program you run may not really be on your disk.
$ echo s* sage sams sane sate sims site $ $ echo s?ms sams sims $ $ echo sa?e sane sate $ $ echo sa* sage sams sane sate $The first command (echo s*) causes the asterisk (*) to be expanded into all the files in this directory. The second command (echo s?ms) causes the question mark to be replaced with one character of the alphabet in the second place of filenames. The third command (echo sa?e) causes the question mark to be replaced with one character of the alphabet in the third place of filenames. The fourth command (echo sa*) causes the sa and asterisk (*) to be expanded to all files in the directory that begin with the letters sa.
For example, the who command reads a list of the currently logged-in users and sends this list to your screen; the wc command prints out a count of the number of lines, characters, and words in its input. You can use pipeline hookup to count the number of logged-in users. Look at the following commands.
$ who kamran tty1 Jun 4 20:56 kamran tty2 Jun 4 21:03 ishee tty3 Jun 4 21:41 $ $ who | wc 3 15 93The first command (who) gives the results of the who command. Currently, there are two users in this system, with user kamran logged in twice and user ishee logged in once.
The second command (who | wc) redirects the output of the who command to the input of the wc command with the | character. Day 4 covers this in greater detail. The | character is known as the pipe because it pipes the output of the who command to the input of the wc command.
Shell variables are commonly used as placeholders for arguments whenever long pathnames are too laborious to type. You can create a shell variable and assign it a value that is not so laborious to remember and type. Days 4 and 5 cover the components of the following example in greater detail.
1. $ cd /usr/lib/X11/xdm 2. $ x=--pwd-- 3. $ pwd /usr/lib/X11/xdm 4. $ ch $HOME 5. $ pwd /home/kamran 6. $ cd $x 7. $ pwd /usr/lib/X11/xdmFor this particular example and for other special examples, I'll number the lines for later reference. Line 1 requests a change of directory:
/(cd) to /usr/lib/X11/xdmLine 2 forces the output from the print working directory command (pwd) into the variable x. Note the backward quotes used to take the result of the pwd command as the input into variable x. Day 4 covers this character.
Line 3 represents the request to execute the pwd command, and the following line shows the result from the pwd command.
Line 4 shows how you can get back to your login directory called $HOME. This is the directory you are assigned when you first log into UNIX. Day 3 discusses using the HOME variable.
Line 5 represents the request to execute the pwd command; the next line shows the result from the pwd command. This confirms that you are no longer in the
/usr/bin/X11/xdm directory. Line 6 causes the variable in x to be expanded to the value of /usr/bin/X11/xdm. Thus, line 7 shows that the location of the directory was changed to /usr/bin/X11/xdm.
Remember that the "real" reason UNIX has cryptic commands is that sometimes programmers get lazy typing the same command over and over. The shorter the command, the less likely you are to mistype it. Of course, the fewer the keystrokes, the better. Reducing long pathnames, options, and command names to two-letter combinations is helpful. Only a programmer can really gloat in the feeling of running a four-hour job with the a program called r.
In addition to variable assignment, you also can have loops and tests for conditions. Day 9 covers these in greater detail.
The Korn shell also incorporates the features of the C shell (such as process control). The Korn shell was initially not available on all UNIX machines but is fast becoming the de facto standard for all later UNIX releases. One of the selling factors for the Korn shell is that it can run Bourne shell scripts without any modification at all.
During its first few releases, the Korn shell had to be purchased as an add-on to UNIX. Given its popularity, several shareware versions of the Korn shell were released after its birth. One is called pdksh and is a public domain Korn shell--thus, it is free.
The public domain version was written by Eric Gisin who made more enhancements to the original Korn shell. These include the emacs customizable keys and the tilde (~) command to default to items other than user names. All in all, this is a good alternative to the original Korn shell (if the original is not available to you on your system). However, it is not as complete as the original Korn shell.
Another later variation of the Korn shell is the windowing Korn shell from USL. This version incorporates the X Windows toolkit features and enables a programmer to write shell scripts that take advantage of the X Windows Xt toolkit. This is a major step toward developing shell scripts that take advantage of the X Windows environment and gives an alternative to the traditional character-based domain for shell scripts. For more information, contact Novell's UNIX System Labs at (800) 847-0240.
A shell can operate in two modes: interactive or noninteractive mode.
A shell is interactive when you, the user, interact with the kernel through this shell. A shell is noninteractive when it executes script files without user intervention. A script file is a set of commands stored in a file on disk. You will learn about script files in Days 8 and 9.
All of the shell scripts you have seen up to now are noninteractive scripts because they do not require any user input while running. In other words, they run from start to finish without requiring any input from the user. For a shell to be interactive, it must interact with the user. That is, it must be able to collect data from the user while running. Day 10 shows you how to determine whether your script is running in interactive mode.
# this is an example of a shell script # that runs interactively # echo "Source File" read src echo "Destination File" read dst cp $src $dstThe first three lines in this script file are comments only. Comments are notes for the script writer and are ignored by the shell. The shell ignores all characters from the first unquoted # character to the end of the line.
The next line echoes a request for a filename. The line after that reads the user input into a variable src. See Day 10 for more information. You can see that the read command is used to read strings of input (user or via a pipe). This string is then assigned to the variable in the read statement. The input to the read statement is terminated with the Enter key. In this example, the variable src is assigned the string typed in by the user.
The shell then echoes a request for a destination file and reads it in via the next read statement for the dst variable. It then executes the cp copy command using the values in the variable src and dst.
Note: Note the $ sign in front of src and dst to get the value stored in the variables src and dst. If the $ were left out, the cp command would copy the file called src to the file called dst.
Each time the user types in a command, the shell searches a known set of directories for a file with the name of the command. If it finds such a command, it requests the kernel to execute the command. The kernel then loads the command into memory and runs it as a process. The shell then goes to sleep until it receives a response back from the process via the kernel. It then sends the response back to the requesting task or via a pipe to the next process in line.
command [argument1 [argument2 [argument3 ...]]] <Enter>If present, the brackets around an argument (as in [argument]) indicate that an argument is optional. Special arguments are also called options when they affect the output of a command. All arguments to a command are referred to as the command's argument list. All commands are terminated when the user presses the Enter key. That means that a user can use the Backspace key to correct any typing mistakes.
Caution: If you see ^H every time you press the Backspace key, try the command:
$ stty erase ^Hwhere ^H is the character produced when the Backspace key is pressed.
DO remember to use the Backspace key to erase characters to the left of the cursor.
DON'T forget to press Enter to execute your command.
DON'T use the arrows keys on your keyboard; they do not always work. You can get the control character (^H) by pressing the control key (usually marked Ctrl) and lowercase h simultaneously (Ctrl+h).
Single spaces or tabs are used to separate arguments to a command. Spaces are not allowed within arguments. If you have to give spaces within an argument name, use double quotes (") or single quotes (') around an argument.
Note: The accent grave (--) character is a special character that forces the execution of a program of the name quoted between the characters and replaces the quoted string (between the grave characters) with the results of that command before proceeding.
Let's take a sneak preview at what quoting is all about in a shell. You will learn about quote characters and their special functions in Day 4.
$ echo I am learning about you I am learning about youThe command in this case is the echo command. Also, there are five arguments to this command: I, am, learning, about, and you. The echo command simply echoes the arguments back out. Note how the extra spaces in the argument list are truncated to one space each.
If you were to put quotes around the entire sentence, you would be requesting the shell to preserve the whitespaces between any shell variables. Consider the output from the following command:
$ echo "I am learning you I am learning about you $The quotes allowed the white spaces between learning and about to exist as they were typed in. However, the white spaces between the words about and you were truncated since they were not within quotes.
$ echo $SHELL /bin/sh $ _The output of /bin/sh shows that the current shell is indeed the Bourne shell.
Caution: An easy way to determine what shell you are running is to echo the value of the SHELL variable. This may not be the safest way, however, because any process you run has the potential for changing the SHELL variable to a different value (although it's unlikely). The hard way is to use the ps command to see what shell process you are actually running:
$ ps pid tty time command 19028 tty21 0:00 ps (you will may see other processes listed here as well) 18943 tty21 0:00 sh $This output gives process id (pid) for the sh program.
The $SHELL environment variable tells you about what kind of shell you are running. You will learn all about environment variables in Day 4, so hang in there. Just remember that an environment variable is something that holds data with information about your login session.
The echo $SHELL command tells you about your login shell's executable program currently being used.
Some of the other outputs from the echo $SHELL command include the following: For the Bourne-again shell:
$ echo $SHELL /bin/sh $For the C shell (note the lowercase $shell):
% echo $shell /bin/csh $For the Korn shell:
$ echo $SHELL /bin/ksh $Another quick way to tell if you are in the C shell is to see if the prompt is a percent (%) sign. Both the Bourne and Korn shells provide the dollar sign ($) as a prompt. The shareware version of the C shell (notably the shareware version, tcsh) sometimes presents the greater than (>) as its prompt.
However, I caution, as I did before, to use the SHELL environment variable when you want to be sure (you will see ways of changing the prompt in Day 4).
$ which csh /bin/csh $This command tells you that the C shell exists in the /bin directory. If the C shell did not exist in the path of executable files, the user would not see any output.
Note: The which command is a very useful tool to have in your toolbox. When writing shell scripts (later in this book), you will want to know which program is being executed. However, the which command may not be available on your system if you do not have the BSD extensions to UNIX. In this case, try the type command to find out which command is being executed. See Day 10 for more details on how to use the which command.
When you enter the command
$ /bin/csh %it will invoke the C shell on your terminal. The percent prompt is the shell's way of requesting input. When invoking the Korn shell from within the Bourne shell, you will see no real difference. You have now invoked a new interactive shell for yourself.
$ ksh $To get back to the original shell, type exit at the prompt and you will be returned to the calling shell. If the shell into which you typed exit had been your login shell, you would have been logged off the system. For more information, see the section later today titled "Logging Off." These shells are like onion skins wrapped around the kernel. Each new shell is a layer on top of the previous shell; each exit command takes this layer off. The last layer taken off logs you off the system. See Figure 1.3 for this type of layering.
$ csh command not found: cshThis tells you that the C shell, csh, does not exist on your current list of paths to look for commands. See Day 3 for details about setting this path.
Note: UNIX offers a find command that locates files for you. For example, to find a file called csh, use the find command. An example follows this note.
$ find / -name csh -print /usr/ucb/bin/csh $The arguments to the find command in this example are as follows:
$ sh $ sh $ sh $ sh $ exit $ exit $ exit $ exit $ exit bye! mpsi login: _The first four invocations of the Bourne shell (sh) required four exit commands. The fifth exit command logged you out completely because it terminated your original login shell. The bye! is a message from the system indicating that it has logged you off the system. You might see a different message, or nothing at all, depending on your system.
Caution: Note that Ctrl+d does not always work at logging you off a shell. Typing the exit command is the preferred, standard way of exiting a shell.
Issuing the ls by itself gives a directory listing of everything in your current working directory. Type ls at the prompt, followed by the Enter key.
$ ls Xconfig Xconfig.new advs chprog chuser docs faq quran mkcdrom sams screen.dump src startx tifs xmeThe first command line was ls by itself. The results of that were a listing of all the files in the present directory. Now try the same ls command with an argument. Try the following example.
$ ls mkcdrom mkcdromThis time, the command was of the form: $ ls filename. This format gives a directory listing of all the filenames specified in the command-argument list. In this case, the filename is an optional argument to the ls command.
Thus, because the file mkcdrom existed in my current directory, it was listed to the screen. You can request more than one file in the argument list as well.
$ ls /etc/passwd /etc/group /etc/passwd /etc/groupThis command requests the listings for two files. The command name is ls. The first argument is /etc/passwd, and the second argument is /etc/group. This command will only list the two files in the /etc directory.
Now try the same command (as before) with the -al option.
$ ls -al /etc/passwd /etc/group/etc -rw-r--r" 1 root root 258 Jan 28 20:10 /etc/group -rw-r--r" 1 root root 601 Apr 23 22:42 /etc/passwdThis is command requesting the listings for two files. The command name is ls, and the first argument is -al, which is the request to show all the file (a) and provide a long listing (l).
The second and third arguments are /etc/passwd and /etc/group, respectively. Since these are both files, this command will provide a long listing about these two files in the /etc directory.
Now try the same command, but this time use the ls -al /etc command.
$ ls total drwxr-xr-x root root May drwxr-xr-x root root Apr root root Feb DIR_COLORS root bin May crond.pid root root Feb csh.cshrc root root Feb csh.login drwxr-xr-x root root Mar default root root Jan disktab root root Jan fdprm drwxr-xr-x root root Apr fs root root Apr fstab root root Jan gettydefs root root Jan group lrwxrwxrwx root root Apr inet root root Feb inittab root root May issue root root May klog.pid root root Dec ksh.kshrc root root May ld.so.cache root root Mar ld.so.conf drwxr-xr-x root root Apr lilo root root Apr lilo.conf root root Apr lilo.conf.bak root root Dec magic root root Sep minicom.users root root Apr minirc.dfl root root May motd root root May mtab root root Mar mtools root root Apr passwd root root Apr passwd.OLD root root Apr profile drwxr-xr-x root root Apr rc.d root root Jan securetty root root Mar shells drwxr-xr-x root root Apr skel root root Jan syslog.conf root root May syslog.pid root root Mar termcap root root Nov ttys root root May utmpThis is a very long listing of a directory /etc. The -al is the command's first argument and contains the options -a for all and -l for long. The /etc string is the second argument. In this case, /etc happens to be a directory; so all the files in the /etc directory are shown.
Day 2 examines the output from the ls command in greater detail.
$ ls who pwd ls: who: No such file or directory ls: pwd: No such file or directoryThis attempts to give you a listing of all who and pwd files.
Now, type in the command again with semicolons between the three words to get:
$ ls;who;pwdThe following code lines show this command with its output.
$ ls;who;pwd ch01 ch02 ch03 ch04 ch05 ch06 ch07 ch08 ch09 ch11 dos2unix dos2unix.c newkorn.asc note numlines parlor phone pizza sage sams sane sate saveme sims site toc whoout kamran tty2 Jun 5 12:04 kamran tty1 Jun 5 12:00 /home/kamran/samsThe ls command is executed first, the who command next, and then the pwd command.
The semicolons are what differentiates the previous two commands. The semicolons force the execution of the commands in order from left to right, one per command. Spaces are not necessary between the commands and the semicolons.
Because the semicolon is a special character to the shell, you have to put quotes around it to make it an argument instead of a command terminator. Consider the command:
$ ls grunch; bronchialThe command sequence will be to list the file grunch and then execute the command bronchial. However, the command
$ ls "grunch;" bronchialwill be interpreted by the shell as list the file grunch; (that is, with the semicolon as part of the name) and the file bronchial. Notice how the quotes are removed by the shell by the time the argument is passed to the ls command. To see another feature in the shell, try the following command.
$ echo this is a "# " hash this is "#" hash $ echo # Hi there I am a shell user $ (nothing) $The # character is the pound sign directly above the 3 on a keyboard. This is often referred to as the hash character, or the pound sign. As you can see from the output, the shell stopped parsing everything from the # character to the Enter keypress. The next line shows that nothing in the command after the # is interpreted.
This example shows that # is also a command interpreter terminator and takes precedence over the ; operator.
$ pwd #who /usr/kamran/samples (the who command is not executed)The shell will not execute the who command because the # has requested that all further input on the line be ignored until the next command is entered. This is of great significance to those writing shell scripts because the # is used to insert comments into a shell script. So, what if you want to display the # as part of the command structure? Put it in double or single quotes.
$ echo this is "#" hash this is # hash $ echo this is "#" hash this is # hashAs before, the # can be displayed by putting quotes around it. Since the quotes are removed by the shell before the argument is sent to the command, the # is not parsed out and sent to the command.
This is a prelude to Day 4, which covers other complicated aspects of quoting.
Let's work with changing your password for this illustration. You might want to change your user-assigned password to one of your own preference. On some UNIX systems, it is mandatory to change your system-administrator-assigned password to one of your own when you first log in.
You can change your password by using the passwd command. See the following example.
$ passwd Enter old password: (nothing is shown as you type) Enter new password: (nothing is shown as you type) Re-enter new password: (nothing is shown as you type) $The passwd program requires you to type your old password in before allowing a new one. This prevents someone changing a password on an unattended terminal. Also, you have to type the new password in twice to ensure that it is entered correctly.
Use your judgment when choosing a password. Can it be kept secret? Is it long enough? Is this password written down somewhere, for example, like a driver's license number?
DO mix upper- and lowercase characters (for example, KrYpToNite, SuPerBirD, and cLARkKeNT).
DO use alphanumeric (a through z and 0 through 9) characters.
DO check to see that your password is not in the online dictionary.
DO make sure that the password is not associated with your name.
DO make sure your password is at least six alphanumeric characters.
DON'T use a common name such as jill, jack, roger, or mike. It's easy to guess the name of a child, spouse, pet, or other loved one.
DON'T use your user, first, or last name. These are easy for others to guess.
DON'T use words found in UNIX's online dictionary.
DON'T keep your accounts with no passwords at all.
DON'T use passwords of project names.
The application generating the output continues to run; only the output to the screen is suspended. To restart the scrolling display, press Ctrl+q . The output scrolling is restarted until either the output is finished or another Ctrl+s is pressed.
Caution: Pressing several Ctrl+q's at one time may disable the scroll-stopping capability of some terminals. So be cautious when using Ctrl+q. Also, Ctrl+q may not work on some terminals. Try using the more or pg command instead.
To stop the output from a command from scrolling any further, use the Ctrl+c command. This will generally stop the application from generating any more output to the screen.
In some cases, your system administrator may have disabled this feature to allow an indefinite login period. In such a case, you cannot rely on UNIX cleaning up after you.
There are two ways to log off the UNIX system:
The second command (Ctrl+d) at the prompt sends an end-of-file character to the shell. This tells the shell that there is no more input. Consequently, the shell just quits, leaving you with a login prompt. This is not the preferred way of logging off UNIX because it does not clean up after itself as well as the exit command. This method is included here if you find yourself looking at the login prompt after accidently typing in this combination.
As you learned earlier today, Ctrl+d does not always log you off on some UNIX systems. Typing the exit command is the preferred way of logging off UNIX.
$ exit bye! Welcome to Emu mpsi login:The exit command causes the shell to terminate. UNIX cleans up after you and then resets the terminal you were on to accept another user. You may see the word bye instead of the work logout, or some other string, to indicate that you have logged off the system.
On some systems, when you log out, you are presented with a message to indicate that you are indeed off the system.
The topics covered today were the following:
ls command: $ ls file; chapterWhy? How can I change this command to remedy this situation?
$ ls file; chapter $ ls "file cat > help This is a help file ctrl-d $cat > filename This is a file called filename ctrl-d
$ ls help; chapter $ ls "help;" chapter $ ls -al $ ls -a $ ls # $ who | wc $ date $ ls; date; who;