UNIX For Dummies [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

UNIX For Dummies [Electronic resources] - نسخه متنی

John Levine, Margaret Levine Young

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید






History Repeats Itself

We make fun of the C shell a lot (and rightly so), but when Bill wrote it, he added a lovely feature called history . BASH does history too, even more nicely than the C shell. And the Korn shell has a way to do history that is clunky but serviceable.
The history command enables you to issue UNIX commands again without having to retype them, a big plus in our book. Bourne shell users may as well skip the rest of this chapter because it will just make you jealous (or it’ll make you bite the bullet and switch to the BASH shell, by typing bash).

Here’s how history works. The shell stores in a history list a list of the commands you’ve given. Then you can use the list to repeat commands exactly as you typed them the first time or edit previously used commands so that you can give a similar command.


History in the key of C


In the C shell, you can type !! and press Enter to repeat the last command you typed. The shell displays the command and then executes it.

You can also rerun the last command line that begins with a particular bunch of letters. If you type
!find

the C shell repeats the last command line that began with the text find. You don’t have to type an entire command. If you type
!fi

it looks for the last command you typed that started with fi, which may be a find command or file command.

To see the history list, type history. You see a list like this:

1 20:26 ls
2 20:26 ls -l
3 20:26 ls -al
4 20:26 history
5 20:26 cat junk3
6 20:26 cat .term
7 20:26 history
8 20:27 history
This example shows the commands you just typed, in the order you typed them. Because the list is numbered, you can refer to the commands by number. After the number comes the time you gave the command (if you care), followed by the command you typed.

If you want to repeat a command, you can type ! followed by the number of the command. For example, if you type the C shell repeats command number 3 on the list (in this case, ls -al ).

!3









Technical Stuff Instant script — just add water


For those of you who know what a shell script is, you can use the history command to create an instant script. (For those of you who don’t, read Chapter 12.) Here’s how to use the history command to create a script. Give the commands you want to include in the script, in the order in which you want them to occur. Then type history to display the list of commands. Note how many of the previous commands you want to include in the scripts; for this example, perhaps it’s the last eight commands. Type a command like this (for the C shell):


history -h 8 >myscript

If you use BASH, type
history 8 > myscript

In the Korn shell, type
fc -l 8 > myscript

This command lists the last eight commands and stores the list in a file called myscript . Requesting from your history list more commands than you think you need is a good idea, because you can always delete them from your script. You have to use a text editor to clean up the script anyway, deleting the command numbers and times.











You can also repeat a command with a modification. Suppose that you just typed this command:


find . -name budget.04 -print

Now you want to give the same find command, but this time you’re looking for a file named budget.05 . Rather than tediously, arduously retype the line, character by character and keystroke by keystroke, worrying anxiously about a possible typo with every key you press, you can tell the C shell to repeat the last command, substituting 05 for 04. The command is

^04^05

You type a caret (^), the old text, another caret, and the text to substitute. Voil! The C shell displays the new command and then executes it.


BASHing through commands


BASH can do all the cool history tricks the C shell can, with some additional acrobatics. When BASH displays your history list, it usually stores the last 500 commands you typed, so the list can be huge. To see it a page at a time, type this command:

history | more

To see the last nine commands on the history list, type
history 9

Here comes the neat part — you can press the arrow keys to flip back through your commands. When you press the up-arrow key (or Ctrl+P, for previous), BASH shows you the previous command from the history list. You can press Enter to execute the command. You can keep pressing the up-arrow key (or Ctrl+P) until you get to the command you want. If you go past it, you can move back down your history list by pressing the down-arrow key (or Ctrl+N, for next).

This feature is downright useful and typo-saving! DOS windows in Windows have it too, of course, but who’s counting?

After you display a command from your history list on the command line, you can edit the command before you press Enter to execute it. Press the left- and right-arrow keys (or Ctrl+B and Ctrl+F, for backward and forward) to move the cursor. When you type characters, BASH inserts them on the command line where the cursor is.

The folks at the Free Software Foundation who wrote BASH are big emacs fans (as are we) because you can use most emacs editing commands to edit the command on the command line. For example, pressing Ctrl+A moves your cursor to the beginning of the line, Ctrl+E moves it to the end of the line, Esc+F moves it forward by a word, and Ctrl+K deletes everything to the right of the cursor. For users who, for some reason, prefer vi to emacs , if you press Esc+Enter, BASH changes to a vi -like editor, where you search for history commands by pressing Ctrl+R and Ctrl+S.

Enough about BASH and history . You get the general idea!


A Korn-ucopia of commands


We don’t use the Korn shell much because we’ve become rather fond of BASH, but the Korn shell can do history, too. The history command lists your history list, as does the more cryptic fc -l command. To repeat the last command, just type r and press Enter. That’s it — just r. To repeat the last cat command, type

r cat

To repeat the last command, but replace 03 with 04, type

r 03=04

The Korn shell enables you to edit your previous commands in all kinds of fancy ways, although it’s confusing to do, so we suggest that you switch to the BASH shell if you long to edit and reissue commands.

/ 213