Before you begin, you should have worked through the other chapters in this
book.
You should know which shell you are using, and you should be able to use
vi to edit a file.
Your environment
The UNIX system uses the term ``environment'' to refer to
all the settings that influence the way you work on the
computer.
You can define the following sort of information in your environment:
Each shell has certain control files that it reads when you log in. For the Bourne shell (sh), the control file is called .profile. The Korn shell (ksh) has both a .profile and a .kshrc, and the C shell (csh) has a .login and a .cshrc.
The difference between .profile and .kshrc, and between .login and .cshrc, is in when the files are read. The .profile and the .login are only read when you log in. However, the .rc files, .kshrc and .cshrc, are read each time you start a ksh or csh. (You can start a shell from the command line by typing the name of the shell just like you would type any command.)
These control files are shell scripts: ``shell'' because they are written in the shell programming language; ``scripts'' because they are text files that are read one line at a time, like a DOS batch file.
In shell scripts, you see commands you are already familiar with, as well as programming constructs for looping, branching, and setting variables.
For listings and explanations of a typical .profile,
.kshrc, .login, and .cshrc, see
Appendix D, ``Sample shell startup files'' in the Operating System User's Guide.
Changing your prompt
A prompt will appear after you have logged into your system.
The UNIX system stores this prompt in a
variable.
To change your prompt, you reset the value of the prompt variable.
In the Bourne and Korn shells, the prompt variable is called PS1 (prompt string 1). In the C shell, the prompt variable is called prompt.
All three shells have a secondary prompt as well as the main prompt. This secondary prompt is shown when you type a command that makes the shell expect further input.
For example, in the Bourne shell, the secondary prompt is ``>'' by default:
$ for i in *.tut
>
Here, you are saying to the shell ``for every file (i
represents every file) ending in .tut ...''
The shell gives you a secondary prompt because it needs more information
to complete your command.
In the Bourne and Korn shells, the secondary prompt is stored in the variable PS2 (prompt string 2), which you can reset. You cannot reset the secondary prompt in the C shell. To reset PS2 in sh or ksh, follow the instructions below, substituting PS2 for PS1.
To reset your prompt in the Bourne or Korn shells, type PS1=value where value is the value you want to assign to the prompt variable.
For example, to set your prompt to say ``Yo'', you would add the following line to your .profile or .kshrc:
PS1=YoC-shell users would add the following line to their .login:
set prompt=Yo
Q: When I change my prompt, I lose the space between my prompt and where the command line starts. How do I get this back?
A: To keep the space between the prompt and the command line, you need to put a space after your new prompt. To get the shell to notice the space, you need to enclose the whole prompt string in double quotes.
In ksh or sh, add the following line to your .profile:
PS1="Hello friend "In csh, add the following line to your .login:
set prompt="Hello friend "
Q: How do I get my prompt to show the current directory, like on DOS?
A: In sh, add the following line to your .profile:
nd() { cd $* ; PS1="`pwd` "; }
Now, use the command nd, which you just created, to change to a new
directory and display the directory as the prompt.
In ksh, add the following line to your .kshrc:
PS1='$PWD 'In csh, add the following lines to your .cshrc: (You do not need to add the lines that start with #. These are comments.)
# make a command doprompt that sets the prompt to the working directory alias doprompt 'set prompt="`pwd` "' # set the prompt the first time around doprompt # alias the cd command to change directories and reset the prompt alias cd 'chdir !* || doprompt'
When you see a message like ``not found'', it means your
shell could not find the command in any of the directories
listed in your path.
If you see a ``not found'' message for a command
that you know exists,
ask your system administrator what directory the command lives in,
then add that directory to your path definition.
In the meantime, you can type the full pathname of the command, for
example, /usr/bin/finger.
When you use the full pathname of a command, you tell the shell
exactly where the command lives, so it does not search through the
directories in your path definition.
A typical path setting in a sh or ksh .profile might look like this:
PATH=/bin:/usr/bin:$HOME/bin:.This says ``set the path to look in the bin directory, then /usr/bin, then the bin directory in the home directory, and finally, in the current directory.''
The same path setting in csh .login would be:
set path=(/bin /usr/bin $HOME/bin .)To add a directory to your path, simply edit the path statement in your .profile, .login, .kshrc, or .cshrc to contain the new directory. For example, to add the directory /usr/company/bin to your path in sh or ksh, you could change your path statement to read:
PATH=/bin:/usr/bin:/usr/company/bin:$HOME/bin:.
Q: Why would I want to put a new directory in the middle of the path definition instead of at the end?
A: You control the order in which directories are searched by the order you put those directories in the path definition. In general, you want to put nonstandard directories, like your company bin and your personal bin, after the standard /bin and /usr/bin. This is because most of the commands you want to use are in these standard directories, so putting them at the beginning of your path means your shell finds them more quickly.
Q: My path setting contains the PATH variable itself:
PATH=$PATH:$HOME/binWhat does this mean?
A:
A path setting like this says ``set the path to the current path, then add
in the bin in my home directory.''
When you log in, your shell first reads definitions from the system-wide
profile /etc/profile.
If your system has been set up so that /etc/profile contains path
definitions,
including $PATH in your path definition ensures that your shell
knows about any system-wide path definitions.
Default file permissions
You have already seen how the UNIX system uses file
and directory permissions to control who can access which files.
So far, you have learned to manipulate these permissions using
symbolic mode,
like:
chmod a+x newfile
This says, ``Change the mode of newfile so all
users have execute permission.'' Before you learn how to
control a file's default permissions, you need to
understand how to specify permissions using
absolute mode.
Changing permissions with absolute mode
When you specify permissions using absolute mode,
you use a three-digit octal number to specify the
permissions for owner, group, and other.
For example,
if you wanted to change the permissions on a file so that
the owner had read and write permission, members of the group had
read permission, and no one else had any permissions, you could type:
chmod 640 file
Here file is the name of the file.
In the preceding example, 640 is an octal number representing file permissions. The 6 represents the permissions for owner, the 4 is the permissions for group, and the 0 is the permissions for other. These digits are arrived at by taking the binary value of each permission, read, write, or execute, and adding them together to form one octal digit that represents the whole set (owner, group, or other).
Here are the octal values for some common permission settings:
------------------------------------------------ Permission Value ------------------------------------------------ r 4 w 2 x 1 r+w 6 r+x 5 all permissions 7To change a file's permissions to
r--------, you could type:
To change a file's permissions to rwxrwxr-x, you could type:
chmod 775 file
As you can see, once you are used to changing permissions using
absolute mode, it can be a quicker method than symbolic mode.
Setting your file creation mask
To control the default permissions that are given to
every new file you create, you use the
umask
(user mask) command.
The umask command sets up a file creation mask. Setting a mask is the opposite of setting the permissions themselves; when you set a mask, you are telling the computer the permissions you do not want, rather than the permissions you do.
To set the default file permissions on new files you create
to rw-r-----, you could add the following line
to your .profile or .login:
umask 137This is the opposite of saying chmod 640. If you wanted to set a umask for
rw-rw----, it would be:
umask 117A umask that allowed read and write permission for everyone would be:
umask 111A umask that denied permissions to everyone except the owner of the file would be:
umask 177You can see your current umask by typing umask and pressing <Enter>. If umask is not explicitly set in one of your shell startup files, the computer shows you a default umask.
You can change your umask at the command line by typing
umask, then the value you want your mask to have,
then pressing <Enter>.
Keep changing your umask and creating and listing files
until you get the default permissions you want.
Configuring mail
In this section you will learn how to control where your shell
looks for mail and when and how it tells you that new mail has arrived.
These are options you can control within your shell startup files.
You can also set a variety of mail options in mail's own startup
file, .mailrc.
For information about the options you can set in .mailrc, see
mail(C).
Depending on the shell you are using, you can specify where mail is looked for by setting the MAILPATH or the MAIL variable. Again, depending on your shell, you can control how often you are notified of new mail by setting the MAILCHECK or MAIL variable.
To tell your shell where to look for mail, set the appropriate variable to the pathname where you receive your mail. (Generally, you receive mail in /usr/spool/mail/loginname, where loginname is your login name. If you are unsure about where you receive your mail, ask your system administrator.) With sh and ksh, you can tell your shell how you want to be prompted for new mail using this same variable setting.
To set your mail path and new mail message in sh, add the following line to your .profile:
MAILPATH=pathname%messageHere pathname is the pathname and message is the message you want to be prompted with. For example:
MAILPATH=/usr/spool/mail/susannah%Yo, you've got some new mailTo set your mail path in ksh, add the following line to your .profile or .kshrc:
MAILPATH=pathname?messageThis is the same as with the Bourne shell, only you use a ? to introduce the message you want to see. If you leave out the message, sh prints
you have new mail and ksh prints
You have new mail.
To set your mail path in csh, add the following line to your .login or .cshrc:
set MAILPATH=pathnameHere pathname is the pathname from where you want your mail read. csh prompts you with
You have new mail when new mail
arrives; you cannot change this.
By default, each shell checks for mail every 10 minutes. You can change this by specifying a new time in seconds. In sh or ksh, add the following line to your .profile or .kshrc:
MAILCHECK=secondsHere seconds is the number of seconds you want to go by before the shell checks for new mail again. For example, if you want your ksh or sh shell to check for mail every half hour:
MAILCHECK=1800In csh, if you want to change how often the shell checks for mail, you need to add the new number of seconds before the pathname in the MAIL variable. To tell your csh to check for new mail every half hour:
set MAIL=(1800 /usr/spool/mail/susannah)
The way you create aliases depends on the shell you are using. In the Bourne shell, you need to set up a shell function, while in the Korn shell and the C shell, you can use the built-in alias command.
To set up an alias in sh, add the following lines to your .profile:
aliasname() { command
}
Here aliasname is the name you want to call the alias
and command is the command you want to alias.
When you choose a name for your alias,
be careful to choose a name that is not already the name of a UNIX command,
otherwise, when you type the name of your alias, the UNIX system may think
you mean the command of the same name instead.
For example, to create an alias called dir in sh that shows you a file listing, add the following lines to your .profile:
dir() { ls
}
You can make an alias that uses a filename as an argument,
but you need to tell your shell where to insert the filename.
You can do this by using the variable $1, which the
shell reads as
``insert the first argument here.''
For example, if you want to create an alias in sh
called print that runs a file through the pr
(``pretty print'') command before sending it to the lineprinter, you
could set up the following function:
print() { pr $1 | lp
}
To print a file using your new alias, you would type
print file where file is the name
of the file you want printed.
Aliases in the Korn and C shells are introduced by the built-in shell command alias. Aliases in the Korn shell have the following format:
alias aliasname='command'So, the dir alias would be:
alias dir='ls'And the print alias would be:
alias print='pr $1 | lp'Aliases in the C shell have this format:
alias aliasname 'command'The dir alias in csh would be:
alias dir 'ls'And the print alias in csh would be:
alias print 'pr -n :* | lp'
To change your prompt: In sh or ksh:
PS1=newprompt
In csh:
set prompt=newprompt
To add a directory to In sh or ksh:
your path: PATH=$PATH:newdir
In csh:
set path=(/bin /usr/bin $HOME/bin newdir .)
To change the default umask permsmask
file permissions:
To change where the In sh:
shell looks for mail MAILPATH=pathname%message
and the new mail In ksh:
message: MAILPATH=pathname?message
In csh:
set MAILPATH=pathname
(You cannot change the new mail message in
csh.)
To change how often In sh or ksh:
your shell looks for MAILCHECK=seconds
new mail: In csh:
set MAIL=(seconds pathname)
To create a command In sh:
alias: aliasname() { command
}
In ksh:
alias aliasname='command'
In csh:
alias aliasname 'command'
-------------------------------------------------------------------------
For more information about See
-------------------------------------------------------------------------
File permissions chmod(C)
File creation mask umask(C)
The files your shell reads Appendix D, ``Sample shell startup files''
at startup
The Bourne shell Chapter 11, ``Automating frequent tasks''
sh(C)
The Korn shell Chapter 11, ``Automating frequent tasks''
ksh(C)
The C shell Chapter 11, ``Automating frequent tasks''
csh(C)