Flylib.com

Books Software

 
 
 

Random Numbers

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Chapter 6.  Performing Arithmetic


Random Numbers

The Korn shell provides a special variable, RANDOM , which is used to generate random numbers in the range from 0 to 32767. It generates a different random number each time it is referenced:


$ print $RANDOM


27291


$ print $RANDOM


5386


$ print $RANDOM


6884

You can also initialize a sequence of random numbers by setting RANDOM to a value. Here, RANDOM is set to 7 . When subsequently accessed, the values 2726 and 18923 are returned:


$ RANDOM=7


$ print $RANDOM


2726


$ print $RANDOM


18923

When RANDOM is reset to 7 again, the same numbers are returned:


$ RANDOM=7


$ print $RANDOM


2726


$ print $RANDOM


18923

If RANDOM is unset, the special meaning is removed, even if reset.


   
Top
 

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents


Chapter 7. The Environment

After You Log In

The Environment File

Environment Variables

Korn Shell Options

Aliases

Prompts

Subshells

Restricted Shell

Besides executing commands and being a programming language, the Korn shell also provides a number of commands, variables, and options that allow you to customize your working environment.


   
Top
 

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Chapter 7.  The Environment


After You Log In

After you login, the Korn shell performs a number of actions before it displays the command prompt. Usually it first looks for /etc/profile. If it exists, it is read in and executed. The /etc/profile file contains system-wide environment settings, such as a basic PATH setting, a default TERM variable, the system umask value and more. The Korn shell then reads and executes $HOME/.profile . This file contains your local environment settings, such as your search path, execution options, local variables , aliases, and more. A sample profile file is included in Appendix A.


   
Top
 

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Chapter 7.  The Environment


The Environment File

Once the profile files are processed , the Korn shell checks the environment file, which is specified by the ENV variable. The environment file usually contains aliases, functions, options, variables and other environment settings that you want available to subshells. Besides being processed at login, the environment file is processed each time a new Korn shell is invoked. There is no default value for ENV , so if not specifically set, this feature is not enabled. A sample environment file is included in Appendix B.

Because the environment file must be opened and read each time a new Korn shell is invoked, performance can be adversely affected by having a large environment file with lots of functions.


   
Top
 

   

Korn Shell: Unix and Linux Programming Manual, Third Edition, The
By Anatole Olczak

Table of Contents
Chapter 7.  The Environment


Environment Variables

There are a number of variables provided by the Korn shell that allow you to customize your working environment. Some are automatically set by the Korn shell, some have a default value if not set, while others have no value unless specifically set.

Table 7.1 lists some of the Korn shell variables. The following sections cover some of the important variables and how they affect your working environment. All the available variables are listed in the Appendix E.

Table 7.1. Some Korn Shell Environment Variables

CDPATH

search path for cd when not given a full pathname (no default)

COLUMNS

window width for in-line edit mode and select command lists (default 80 )

EDITOR

pathname of the editor for in-line editing (default /bin/ed )

ENV

pathname of the environment file (no default)

HISTFILE

pathname of the history file (default $HOME/.sh_history )

HISTSIZE

number of commands to save in the command history file (default 128 )

HOME

home directory

IFS

internal field separator (default space, tab, newline)

LANG

locale

MAIL

name of mail file

MAILCHECK

specifies how often to check for mail (default 600 seconds)

MAILPATH

search path for mail files (no default)

PATH

search path for commands (default /bin:/usr/bin: )

PS1

primary prompt string (default $ , # )

PS2

secondary prompt string (default > )

PS3 select

command prompt (default #? )

PS4

debug prompt string (default + )

SHELL

pathname of the shell

TERM

specifies your terminal type (no default)

TMOUT

Korn shell timeout variable (default 0)

VISUAL

pathname of the editor for in-line editing

The cd Command

New functionality has been added to the cd command. You can change back to your previous directory with:


$ cd ?/span>

It also causes the name of the new current directory to be displayed. Here, we start in /home/anatole/bin , then change directory to /usr/spool/news/lib :


$ cd /usr/spool/news/lib

Now we cd back to /home/anatole/bin:


$ cd ?/span>

/home/anatole/bin

Another cd ?/span>, and we are back in /usr/spool/news/lib :


$ cd ?/span>

/usr/spool/news/lib

You can also change directories by substituting parts of the current pathname with something else using this format:


cd


string1 string2

where string1 in the current pathname is substituted with string2 . The new current working directory is displayed after the move. In this example, we start in /usr/spool/uucp :


$ pwd


/usr/spool/uucp

By substituting uucp with cron , we change directory to /usr/spool/cron :


$ cd uucp cron


/usr/spool/cron

CDPATH

The CDPATH variable is provided to make directory navigation easier. It contains a list of colon -separated directories to check when a full pathname is not given to the cd command. Each directory in CDPATH is searched from left-to-right for a directory that matches the cd argument. A : alone in CDPATH stands for the current directory. This CDPATH :


$ print $CDPATH


:/home/anatole:/usr/spool

indicates to check the current directory first, /home/anatole , then /usr/spool when cd is not given a full pathname. Instead of typing cd /usr/spool/uucp , you could just type cd uucp :


$ cd uucp


/usr/spool/uucp

Or to change directory to /home/anatole/bin , you could type cd bin :


$ cd bin


/home/anatole/bin

There is no default for CDPATH , so if it not specifically set, this feature is not enabled.

Make sure that only frequently used directories are included, because if CDPATH is too large, performance can be adversely affected by having to check so many directories each time cd is invoked.

PATH

The PATH variable contains a list of colon-separated directories to check when a command is invoked. Each directory in PATH is searched from left-to-right for a file whose name matches the command name. If not found, an error message is displayed. A : alone in PATH specifies to check the current directory. This PATH setting specifies to check the /bin directory first, then /usr/bin , /usr/spool/news/bin , and finally the current directory:


$ print $PATH


/bin:/usr/bin:/usr/spool/news/bin:

Don't let PATH get too large, because performance can be adversely affected by having to check so many directories each time a command is invoked.

If not set, the default value for PATH is /bin:/usr/bin .

TMOUT

The TMOUT variable specifies the number of seconds that the Korn shell will wait for input before displaying a 60-second warning message and exiting. If not set, the default used is , which disables the timeout feature. To set a 10-minute timer, set TMOUT to 600 :


$ TMOUT=600

This variable is usually set by the system administrator in the /etc/profile file.

Mail

The Korn shell provides a number of variables that allow you to specify your mailbox file, how often to check for mail, what your mail notification message is, and a search path for mailbox files.

MAILCHECK

The MAILCHECK variable specifies how often, in seconds, to check for new mail. If not set, or set to zero, new mail is checked before each new prompt is displayed. Otherwise, the default setting is 600 seconds (10 minutes).

MAIL

The MAIL variable contains the name of a single mailbox file to check for new mail. It is not used if MAILPATH is set.

MAILPATH

The MAILPATH variable contains a colon-separated list of mailbox files to check for new mail and is used if you want to read multiple mailboxes. It overrides the MAIL variable if both are set. This MAILPATH setting specifies to check two mailbox files, /home/anatole/mbox and /news/mbox .


$ print $MAILPATH


MAILPATH=/home/anatole/mbox:/news/mbox

Just so you don't think you can go snooping around someone else's mailbox, this only works if you have read permission on the mailbox file.

If MAILPATH is not set, there is no default.

New Mail Notification Message

When you get new mail, the Korn shell displays this message on your terminal just before the prompt:


you have mail in


mailbox-file

You can also create your own mail notification message by appending a ? followed by your message to the mailbox files given in MAILPATH . If you wanted your message to be ' New mail alert ', then MAILPATH would be set like this:


$ MAILPATH=~anatole/mbox?'New mail alert'

What if you had two mailboxes set in MAILPATH ? How would you know which one to read? For this reason, the Korn shell has the _ ( underscore ) variable. When given in the new mail notification message, it is substituted for the name of the mail box file. This MAILPATH setting:


$ MAILPATH=~anatole/mbox?'Check $_':\


/news/mbox?'Check $_'

would cause " Check /home/anatole/mbox " or " Check /news/mbox " to be displayed if new mail was received in either of the mailboxes.

TERM

The TERM variable specifies your terminal type, and is usually set by your system administrator in the global /etc/profile file. If it's not set there, then it's probably in your ~/.profile file. You can tell if it's not set correctly by invoking vi on an existent file. If you get garbage on your screen or the vi commands are not working correctly, try resetting the TERM variable to something else:


$ typeset x TERM=


term-type

Then try running vi again and see what happens.


   
Top