Working in the Unix Environment
Syntax of Unix Command Lines
Types of Commands
The Unresponsive Terminal
Before you can use Unix, a system staff person has to set up a Unix account for you. The account is identified by your username, which is usually a single word or an abbreviation. Think of this account as your office--it's your place in the Unix environment. Other users may also be at work on the same system. At many sites, there will be a whole network of Unix computers. So in addition to knowing your username, you may also need to know the hostname (name) of the computer that has your account. Alternatively, your account may be shared between all computers on the local network, and you may be able to log into any of them.
Once you've logged in to your account, you'll interact with your system by typing commands at a command line, to a program called a shell. You'll get acquainted with the shell, enter a few commands, and see how to handle common problems. To finish your Unix session, you'll log out.
Each user communicates with the computer from a terminal. To get into the Unix environment, you first connect to the Unix computer. (Your terminal is probably already connected to a computer.[1] But Unix systems also let you log into other computers across a network. In this case, log into your local computer first, then use a remote login command to connect to the remote computer. See Section 6.1 in Chapter 6.)
[1] Some terminals can connect to many computers through a kind of switchboard called a port contender or data switch. On these terminals, start by telling the port contender which computer you want to connect to.
After connecting your terminal, if needed, you start a session by logging in to your Unix account. To log in, you need your username and a password. Logging in does two things: it identifies which user is in a session, and it tells the computer that you're ready to start work. When you've finished, log out--and, if necessary, disconnect from the Unix computer.
WARNING: If someone else has your username and password, they probably can log into your account and do anything you can. They can read private information, corrupt or delete important files, send email messages as if they came from you, and more. If your computer is connected to a network--the Internet or a local network inside your organization--intruders may also be able to log in without sitting at your keyboard! See Section 6.1 in Chapter 6 for one explanation of one way this can be done.Anyone may be able to get your username--it's usually part of your email address, for instance. Your password is what keeps others from logging in as you. Don't leave your password anywhere around your computer. Don't give your password to anyone who asks you for it unless you're sure they'll preserve your account security. Also don't send your password by email; it can be stored, unprotected, on other systems and on backup tapes, where other people may find it and then break into your account.
If you suspect that someone is using your account, ask system staff for advice. If you can't do that, setting a new password may help; see Section 3.6 in Chapter 3.
Unix systems are case sensitive. Most usernames, commands, and filenames use lowercase letters (though good passwords use a mixture of lower- and uppercase letters). Before you log in, be sure your CAPS LOCK key is off.
If you see a message from the computer that looks something like this:
login:
you're probably connected! You can skip ahead to Section 1.1.2 and log in.
Otherwise, if someone nearby uses the same kind of computer system you do, the easiest way to find out if you're connected is probably to ask for help. (We can't cover every user's situation exactly. There are just too many possibilities.)
If there's no one to ask, look ahead at Section 1.1.2, later in this chapter, as well as Section 2.2 in Chapter 2 and Section 6.1 in Chapter 6. You may recognize your situation.
If that doesn't help, but your computer seems to be running an operating system other than Unix (such as Microsoft Windows), check your menus and icons for one with the name of the Unix computer you're supposed to connect to. You might also find a program named either telnet, eXceed, ssh, VMware, procomm, qmodem, kermit, or minicom, or something relating to remote access.
The process of making yourself known to the computer system and getting to your Unix account is called logging in. If you've connected to the Unix host from another operating system, you may have been logged into Unix automatically; in this case, you should be able to run Unix programs, as shown later in this chapter in Section 1.1.4 and Section 1.1.5. Otherwise, before you can start work, you must connect your terminal or terminal window to the computer you need (as in the previous section) and identify yourself to the Unix system.
There are generally two ways to log in: graphically and nongraphically. If your screen has a window or windows floating in it, something like Figure 2-2A, you probably need to log in graphically, as explained by Section 2.2.1 in Chapter 2.
Otherwise, to log in nongraphically, enter your username (usually your name or initials) and your private password. The password does not appear as you enter it.
When you have logged in successfully, you'll get some system messages and finally the shell prompt (where you can enter Unix commands). A successful login to the system named nutshell could look like Example 1-1.
nutshell login: john Password: Last login: Mon Oct 8 14:34:51 EST 2001 from joe_pc Sun Microsystems Inc. SunOS 5.7 Generic October 1998 ------------- NOTICE TO ALL USERS ----------------- The hosts nutshell, mongo, and cruncher will be down for maintenance from 6 to 9 PM tonight. --------------------------------------------------- My opinions may have changed, but not the fact that I am right. Tue Oct 9 12:24:48 MST 2001 $
In this example, the system messages include a maintenance notice, a "fortune," and the date. Although this example doesn't show it, you may be asked for your terminal type, accounting or chargeback information, and so on. The last line to appear is the Unix shell prompt. When you reach this point, you're logged in to your account and can use Unix commands.
Instead of a shell prompt, you may get a menu of choices ("email," "news," and so on). If one choice is something like "shell prompt" or "command prompt," select it. Then you'll be able to follow descriptions and examples in this book.
The messages you see at login time differ from system to system and day to day. Shell prompts can also differ. Examples in this book use the currency sign $ as a prompt.
Let's summarize logging in nongraphically, step by step:
If needed, connect your terminal or terminal window to the Unix system.
Get a "login:" prompt.
Type in your username in lowercase letters at the prompt. For example, if your login name is "john," type:
login: john
The system should prompt you to enter your password. If passwords aren't used on your system, you can skip the next step.
If you were assigned a password, type it at the prompt. For security, your password is not displayed as you type it:
Password:
Press the RETURN key.
The system checks your account name and password, and if they're correct, logs you in to your account.
If you still fail after trying to log in a few more times, check with the person who created your account to confirm your username and password.
Once you have a shell prompt, you're working with a program called a shell. The shell interprets command lines you enter, runs programs you ask for, and generally coordinates what happens between you and the Unix operating system. Common shells include Bourne (sh), Korn (ksh), and C (csh) shells, as well as bash and tcsh.
For a beginner, differences between shells are slight. If you plan to work a lot with Unix, though, you should learn more about your shell and its special commands.[2]
[2] To find out which shell you're using, run the commands echo $SHELL and ps $$. (See Section 1.1.6, later in this chapter.) The answer, something like bash or /bin/bash, is your shell's name or pathname.
If you're using a window system, as described in Chapter 2, get a shell by opening a terminal window--if you don't already have a terminal window open or iconified (minimized) somewhere, that is. (Figure 2-1 shows an example, but yours may look different; the important thing is that the window have a shell prompt in it.) Check your menus and icons for a command with "terminal" or "term" in its name, or a picture of a blank terminal (like a TV screen) in its icon; one common program is xterm.
When the system is ready to run a command, the shell outputs a prompt to tell you that you can enter a command line.
Shell prompts usually end with $ or %. The prompt can be customized, though, so your own shell prompt may be different.
A prompt that ends with a hash mark (#) usually means that you're logged in as the superuser. The superuser doesn't have the protections for standard users that are built into the Unix system. In this case, we recommend that you stop work until you've found out how to access your personal Unix account.[3]
[3] This can happen if you're using a window system that was started by the superuser when the system was rebooted. Or maybe your prompt has been customized to end with # when you aren't the superuser.
Entering a command line at the shell prompt tells the computer what to do. Each command line includes the name of a Unix program. When you press RETURN, the shell interprets your command line and executes the program.
The first word that you type at a shell prompt is always a Unix command (or program name). Like most things in Unix, program names are case sensitive; if the program name is lowercase (and most are), you must type it in lowercase. Some simple command lines have just one word, which is the program name. For more information, see Section 1.2, later in this chapter.
An example single-word command is date. Entering the command date displays the current date and time:
$ date Tue Oct 9 13:39:24 MST 2001 $
As you type a command line, the system simply collects your keyboard input. Pressing the RETURN key tells the shell that you've finished entering text and that it can run the program.
Another simple command is who. It displays a list of each logged-on user's username, terminal number, and login time. Try it now, if you'd like.
The who program can also tell you who is logged in at your terminal. The command line is who am i. This command line consists of the command (who, the program's name) and arguments (am i). (Arguments are explained in Section 1.2, later in this chapter.)
$ who am i cactus!john tty23 Oct 6 08:26 (rose)
The response shown in this example says that:
"I am" John (actually, my username is john).
I'm logged on to the computer named "cactus."
I'm using terminal 23.
I logged in at 8:26 on the morning of October 6.
I started my login from another computer named "rose."
Not all versions of who am i give the same information.
Modern Unix shells remember command lines you've typed before. They can even remember commands from previous login sessions. This handy feature can save you a lot of retyping common commands. As with many things in Unix, though, there are several different ways to do this; we don't have room to show and explain them all. You can get more information from sources listed in Section 8.1 in Chapter 8.
After you've typed and executed several command lines, try pressing the up-arrow key on your keyboard. If your shell is configured to understand this, you should see the previous command line after your shell prompt, just as you typed it before. Pressing the up-arrow again recalls the previous command line, and so on. Also, as you'd expect, the down-arrow key will recall more recent command lines.
To execute one of these remembered commands, just press the RETURN key. (Your cursor doesn't have to be at the end of the command line.)
Once you've recalled a command line, you can also edit it. If you don't want to execute any remembered commands, cancel the command line with CTRL-C. Next, Section 1.1.8 explains both of these.
What if you make a mistake in a command line? Suppose you typed dare instead of date and pressed the RETURN key before you realized your mistake. The shell will give you an error message:
$ dare dare: command not found $
Don't be too concerned about getting error messages. Sometimes you'll get an error even if it appears that you typed the command correctly. This can be caused by typing control characters that are invisible on the screen. Once the prompt returns, reenter your command.
As we said earlier (in Section 1.1.7) most modern shells let you recall previous commands and edit command lines. If you'll do a lot of work at the shell prompt, it's worth learning these handy techniques. They take more time to learn than we can spend here, though--except to mention that, on those shells, the left-arrow and right-arrow keys may move your cursor along the command line to the point where you want to make a change. Here, let's concentrate on simple methods that work with all shells.
If you see a mistake before you press RETURN, you can use the erase character to erase and correct the mistake.
The erase character differs from system to system and from account to account, and can be customized. The most common erase characters are:
CTRL-H is called a control character. To type a control character (for example, CTRL-H), hold down the CTRL key, then press the letter "h." In the text, we will write control characters as CTRL-H, but in the examples, we will use the standard notation: ^H. This is not the same as pressing the ^ (caret) key, letting go, and then typing an H!
The key labeled DEL may be used as the interrupt character instead of the erase character. (It's labeled DELETE or RUBOUT on some terminals.) This key is used to interrupt or cancel a command, and can be used in many (but not all) cases when you want to quit what you're doing. Another character often programmed to do the same thing is CTRL-C.
Other common control characters are:
Find the erase and interrupt characters for your account and write them here:
_______ Backspace and erase a character
_______ Interrupt a program
To end a Unix session, you must log out. You should not end a session by just turning off your terminal!
If you're using a window system, first close open windows and then close the window system; see Section 2.9 in Chapter 2 for more information. If you logged in graphically, that should end your login session. But, if you logged in nongraphically before you started the window system, closing the window system should take you back to a shell prompt (where you originally typed xinit or startx). In that case, use the following steps to finish logging out.
If you aren't currently using a window system, you can log out by entering the command exit at a shell prompt. (In many cases, the command logout will also work.) Depending on your shell, you may also be able to log out simply by typing CTRL-D.
What happens next depends on the place from which you've logged in: if your terminal is connected directly to the computer, the "login:" prompt should appear on the screen. Otherwise, if you were connected to a remote computer, the shell prompt from your local computer should reappear on your screen. (That is, you're still logged in to your local computer.) Repeat the process if you want to log out from the local computer.
After you've logged out, you can turn off your terminal or leave it on for the next user. But, if the power switch for your terminal is the same as the power switch for the whole Unix computer system, do not simply turn off that power switch! Ask a local expert for help with shutting down your Unix system safely.
The first few times you use Unix, you aren't likely to have the following problems. But you may encounter these problems later, as you do more advanced work.
Copyright © 2003 O'Reilly & Associates. All rights reserved.