Using the Korn Shell Command Line

Дата канвертавання21.04.2016
Памер59.49 Kb.

Using the Korn Shell Command Line

This document is for the system-administrator type of Array user who interacts with the UNIX command line. It explains how to use Korn Shell command line history, and how to use aliases to modify your UNIX environment so you can use an editing technique and aliases to move forward and backward through your previous commands.

What is the Korn Shell?
Using the history command.
Using the r command.
Creating your own .profile
Dropping in and out of Array
Making an alias.
Linking filenames to one another.
Using an editor with the command line.
Using your keyboard’s arrow keys with the command line.
How to reset your environment to your own profile.
Unsetting the EDITOR environment variable.
Book recommendation to learn more about the Korn Shell.

What is the Korn Shell?

The command line that you interact with in UNIX is actually a program called a Command Line Interpreter (sometimes called CLI). That program is called a shell, presumably because it follows the “plant” metaphor that UNIX is patterned after – the center of UNIX is the “kernel”, the beginning is “root”, the directory “branches” into “trees”, etc. The shell provides a protective wrapper for the kernel, as well as a method for you to communicate with the kernel.
There are many shell programs, but a guy named David Korn wrote one that has some advanced features, especially in the areas of shell scripting and history**. The Korn shell is actually the /bin/ksh program – and you can control what shell you are using in the user controls with smit.
To find out what shell you are using, query the SHELL environment variable by typing:

$ echo $SHELL

Chances are you are already set up to use the Korn shell, since the default configuration from TSS is set up that way.

Using the history command.

If you type

$ history

at the command line you will see the last 15 commands you ran.
You can control how many lines you always want the history command to show you, but you can also change the amount it shows you only when you want to see more.

$ history -55

shows the last 55 lines for example.
Here is an example of the history command:

$ history

12860 pg cust1.cq

12861 pg cust2.cq

12862 join cust1.cq cust2.cq >> cust3.cq

12863 pg cust3.cq

12864 uuencode cust3.cq cust3.csv | mail 123

12865 exit

12866 arr

12867 join -a1 cust1.cq cust2.cq > unmatched

12868 pwd

12869 arr

12870 exit

12871 arr

12872 pg arr

12873 echo "try this"

12874 echo "try this"

12875 history

The number at the beginning of each history record is the command number itself. Note that at command 12865 the exit command was run, so that history from that point and before is from the last time I was logged in.

Using the r command.

It is possible that the “r” command does not work on your system. If that is the case you can build an r alias that does it by creating this alias: r='fc -e -' (We will discuss aliases later.)
But, if “r” does work on your system – when you enter the letter “r” at the command line, the system repeats your last command.
Try it:

$ echo “try this”

try this

$ r

echo “try this”

try this


You can see that these were actually issued as commands 12873, 12874 in the history section above. Note that the “r” does not appear in the history list – the “real” command it executed does.

This can be used with the history command as well. I can list a history, see that the number of the command I want to execute is 12868, and then issue this command to repeat that specific command:

$ r 12868



Another way to use the command is to tell the “r” command which one I want to execute by giving it a head start. For example, if I want to repeat the “pwd” command, I could do this:

$ r p



Be sure you know what you are doing though – the system will execute the command without asking you, and you may find that the system runs a command you didn’t realize would happen.
You can also take the previous command and pipe it to another utility.

Try piping the ls –la command to the pg utility:

$ ls -la

$ r | pg

Creating your own .profile

You may want to be able to access the shell frequently without continually logging in as another user. Note that if you are in an environment where multiple users share the same login (all logging in as prog for example) you may see the other users’ commands in the history list. This can be dangerous if you use the r command and accidentally execute someone else’s command. You really should have your own session that you log into with your own password, and brings you into your own Korn shell session. If you find that you frequently need to get into another user – say root – then use the “su” command to switch users.
The .profile file is an executable file that the system runs whenever you login. Actually the sequence is the system first runs the /etc/profile for everyone – even when root logs in – and then it runs /home/yourname/.profile. If the user is a typical user it will also then run /tss/live/.profile, which will present the Array login and not allow the user access to the UNIX shell. You want to leave the .profile for most users alone – they should not be messing with the shell.
One login that behaves the way we want it to is already on your system: prog. You can copy the profile of prog into your /home directory and you will be brought into a shell instead of going on to the Array prompt.
First make a copy of your existing .profile:

$ cd /home/sshuman

$ cp /home/sshuman/.profile /home/sshuman/.profile.old

Then copy in prog’s .profile.

$ cp /home/prog/.profile /home/sshuman/.profile

Make sure it is executable.

$ chmod 777 /home/sshuman/.profile

Dropping in and out of Array.

Once you have modified your .profile, you now need to find a way to login to Array when you want to. This little script will accomplish that:

#! /bin/ksh


# ARRAY (log-in to Array)

# Steve Shuman 9-18-99


cd /tss/live

. .profile # this line begins dot, space, dotprofile


Copy it into your home directory and name it what you want, then make it executable with the chmod command.

In my case this script is named “arr”. Whenever I want to login to Array I type:

$ arr

When I exit Array I am returned to the command line.

Making an alias.

Now we have a way to get to the shell easier, but so what? What good is it?

Well, one good thing about having your own shell is setting up permanent aliases. To demonstrate this, lets make an alias command for the history list command so that we can just type the letter “h” and get the history.
The thing that is interesting here is that “history” is already an alias!

Issue this command:

$ alias history

history='fc -l'

So the real command isn’t really “history” anyway. In fact, it points to a command called “fc” (means fix command) and it uses the “l” argument (that is lower case “L” – not the number one – and it means list.)
If you want to see ALL the aliases on your system just type

$ alias | pg

So – back to the point here – if we want to create another alias for history we are going to have to use the “real” command. Modify your .profile to include this line at the end of the file:

alias h='fc -l'; export h

After you get your .profile modified it won’t take effect until it runs again. You can either logoff and back in again or execute it like this:

$ . .profile

Note that the above command begins with a dot, which is a command that means “execute the next thing right from this directory”, then a space, then the name of the executable.
Here is another useful alias. When I drop from Array I always forget to change to lowercase, so I made this one to logoff.

alias EXIT='exit'; export EXIT

As time goes on you will probably find other uses for aliases.

Linking filenames to one another.

Something that isn’t really related to aliases, but does play a role in making the environment more friendly is that it is possible to have a logical link to an executable file that is hard to remember or type.
For example, you could have the command You want to change it – so here are your choices.

  • Copy it to another short name and make it executable. (And risk having two different copies on the system that may not always be the same thing.)

  • Try the alias route.

  • Link the file to a short name

$ ln M

Now you can just type “M” and the original command will execute. You may need to read the man pages on this – there is much more involved with the ln command than I have listed here. Of special interest to anyone who has ever struggled with maintaining a central programming repository is the ability to link entire directories.

Using an editor with the command line.

If you specify what editor you want the system to use, you can use the editor on the commands you are typing and on the history listing. Use the EDITOR environment variable.

To see what your current EDITOR is type:

$ echo $EDITOR

If it returns blank, then the EDITOR environment variable is not set. Two possible editors that may be set are vi and emacs.
To set your editor to vi, you will need to modify your .profile again.

Add this line at the end:

EDITOR=vi; export EDITOR
Now your . profile has the correct information in it, but you need to execute it again to make it take effect.

$ . .profile

If you use vi with the command line, then you may already know how to navigate using vi, but since I can never remember I wrote the following little script, made it executable, and now whenever I want to know a vi command I just type “vihelp”. Note that the middle section explains how to use vi when searching command history. If you are using vi as your environment-specified EDITOR, then you can use these commands.

#! /bin/ksh



# vihelp (no filename extension)


# provides help text for reading at the command line.


# - Steve Shuman 9/15/00


echo "

At the command line (vihelp Steve Shuman 9/15/00)

h history (also accepts number: -10 last 10)

r repeat previous (also accepts line #, or start of last command)

Searching command history using vi mode (Enter vi with escape escape)

- (or k) back one line (keep hitting to keep going back...)

+ (or j) forward one line

? string search back for string

/ string search forward for string

n repeat search in same direction

N repeat search in opposite direction

Working with the command in vi modes. (Must be preceded by escape escape.)

h move right R replace typing

l move left

i insert (before cursor) U undo all on this line

I insert at beginning of line u undo last change

a append (after cursor) . redo last change (dot)

A append at end of line

x delete one character :q! quit without saving

dd delete entire line :wq! write (save) and quit"

You don’t have to go to the history list to execute something. If you just hit “escape k” or “escape -” you will get the last command that you entered brought back and waiting for you to do something with it.

I need to credit the proper person for this feature: I was first shown this by Josh Given at United Electric Supply in Delaware.

Using your keyboard’s arrow keys with the command line.

Despite being quite comfortable with vi, I have never been as happy using it at the command line as I would like to be.

But – eventually I read about a better way. The emacs editor will accept commands from the command line - “control P” will access the previous command for example, but it will also work with aliases for the arrow keys on your keyboard. This means that when you want to see the previous command, you just hit the up arrow key.
You need to set up your aliases at the end of your .profile like this:

# the below makes the arrow keys work on the command line

# set the editor – can’t have both, must choose between emacs and vi. EDITOR=emacs; export EDITOR

# set aliases for arrow keys and home keys.

alias _A='^P’; export _A # up one line (up arrow)

alias _B='^N'; export _B # down one line (down arrow)

alias _D='^B'; export _D # back one character (left arrow)

alias _C='^F'; export _C # forward one character (right arrow)

alias _H='^A'; export _H # Home key - beginning of line (home key)
The way that you edit the .profile is with vi. In order to get a control character into the file, you will have to hit “Control V” first then the “^” character, then the rest of the line. Once you get this working you will love it. At one time I had the “delete” and “end” keys aliased, but had problems when moving between different machines, so I no longer have them aliased.
Here is a script that displays emacs help:
#! /bin/ksh


# emacshelp

# script to assist use of emacs at the command line


echo "\

At the command line: (clihelp Steve Shuman 9/18/00)

h history (also accepts number: -10 would be the last 10)

r repeat previous (also accepts line #, or start of last command)

Searching command history using emacs mode:

CTRL-P Previous line (Control +P)

CTRL-N Next line

CTRL-R Reverse search backward

ESC < Move to first line of history file

ESC > Move to last line of history file

Working with the command in emacs modes:

CTRL-B Move Back one char (without delete)

CTRL-F Move Forward one character

CTRL-A Move to beginning of line

CTRL-E Move to End of line

DEL Delete one character backward (Delete or backspace key)

CTRL-D Delete one character forward

CTRL-K Kill (Delete forward to end of line)

CTRL-Y Yank (retrieve/undo last item deleted)"

Not that you will ever use any of this once you get your arrow keys working…

How to reset your environment to your own profile.

If you access the shell through the “UTILITIES” window from VCQ, you will notice that you will have lost the environment variables and aliases despite still being in your own home directory. You need to run the .profile again to get them back:

$ . .profile

In my case I have linked the .profile to .p, this means that I can issue this command when I want to run the .profile:

$ . .p

Unsetting the EDITOR environment variable.

Some commands are made to work with the vi editor, which has a long history with UNIX and will always be a big part of UNIX. This is especially true if you have modified the /etc/profile so that root also is using an EDITOR environment variable.

For example, if you work with the cron daemon it wants to invoke vi when you issue the “contab –e” command. You can’t just issue the vi command and work on the file directly – it will screw up the cron daemon even if everything looks OK. (Take it from someone who learned the hard way.) If you have EDITOR set to vi everything is fine, but if it is set to emacs it won’t work. The way to do this is to unset the EDITOR variable.

$ unset EDITOR

Then you can proceed like normal

$ crontab –e (or whatever you are trying to do)

Book recommendation to learn more about the Korn shell.

I mentioned that one of the great advantages of the Korn shell is the great additions to scripting ability. If you feel you have gotten familiar with scripting, and are ready to sink your teeth into a little more, then get this book, and especially study the details of variable declaration. There are some very advanced features that make the writing of a shell script more like a typical programming language.

Title: Learning The Korn Shell

Author: Bill Rosenblatt

Publisher: O’Reilly

Pages: 336

Price: $29.95
** This isn’t really about the Korn Shell, but the place where I read about using emacs with the command line is in this book below. Since I mentioned it I figured I had better do my part to avoid plagiarism. It is an excellent book for system administrators, but it is for AIX 3.25, so it is a little dated. Even so, it contains good notes on security and other miscellaneous subjects:

Title: The AIX Survival Guide

Author: Andreas Siegert

Pages: 462

Price: $ doesn’t say

База данных защищена авторским правом © 2016
звярнуцца да адміністрацыі

    Галоўная старонка