![]() |
![]() |
SECTION 3 PREV
Shell and
Environment Variables
The set Command
The echo Command
The printenv Command
The setenv Command
The .login and .cshrc
Files
Running Programs on UNIX
Compilers
on the UNIX Systems
Example 1: Sample C
Program
Example 2: Sample C++ Program
Example 3: Sample FORTRAN Program
Example 4: Sample Pascal Program
UNIX Command Chart
Viewing Files
Managing Files
Input/Output Redirection
Managing Directories
Multitasking
Miscellaneous
Setting Variables
|
|
The set
Command
The echo Command
The printenv Command
The setenv Command
The .login and .cshrc Files
UNIX systems use two types of variables to define your working environment: shell variables and environment variables.
Shell variables are known only to the shell in which they were created. If you start a new shell (command interpreter), the shell variables created in the previous shell are not transferred to the current shell.
Environment variables are global variables and are independent of the shell. Environment variables stay in effect regardless of what shell is used.
On a UNIX system, some shell and environment variables are predefined by the system. Also, environment variables are usually represented by uppercase names whereas shell variables are represented by lowercase names. A shell variable with the same name as an environment variable still has the same function.
Some of the definable shell variables are:
| Variable | Description |
| argv | Argument to the shell. |
| autologout | Number of minutes of idle time before a user is logged out. |
| cdpath | Alternative directory tree search. |
| history=n | Remember last n commands. |
| home | Home directory of user. |
| Directory where shell checks for user's mail. | |
| noclobber | Prevents overwriting an existing file during redirection. |
| user | Login User ID. |
| savehist=n | Remember last n commands at the beginning of next terminal session. |
| prompt=string | Change the default command prompt to string. |
| path | Current path specification - list of directories the system may search to resolve command requests. |
| shell | Shell used to process commands. |
| term | Terminal type. |
| status | Returned by last command: 0=no error, 1=error. |
Some of the definable environment variables are:
| Variable | Description |
| HOME | Home directory. |
| SHELL | User's login shell. |
| TERM | Terminal type. |
| USER | Login User ID |
| PATH | Search path used to resolve command requests. |
| DISPLAY | Name of X Windows capable display device. |
The set command displays current shell variables and their values. You can also designate a new value for a variable with the set command. In the following example, current shell variables are shown using the set command:
$ set <return>
argv ()
autologout 0
cdpath /home/dept/jsmith
cwd /home/dept/jsmith
history 20
home /home/dept/jsmith
mail /usr/spool/mail/jsmith
path (. /home/dept/jsmith/bin /home/dept/bin )
prompt %
shell /bin/csh
status 0
term vt52
user jsmith
$
In the following example, the shell variables term, prompt, and autologout are assigned values using the set command:
$ set term=vt100 <return>
$ set prompt="UNIX> " <return>
UNIX> set autologout=20 <return>
UNIX> set <return>
argv ()
autologout 20
cdpath /home/dept/jsmith
cwd /home/dept/jsmith
history 20
home /home/dept/jsmith
mail /usr/spool/mail/jsmith
path (. /home/dept/jsmith/bin /home/dept/bin )
prompt UNIX>
shell /bin/csh
status 0
term vt100
user jsmith
UNIX>
To display the value of a shell variable, type echo followed by a dollar sign ($) and the name of the variable. You must place a dollar sign before the variable name to display the value of the variable otherwise the name of the variable is displayed. In the following example, the echo command is used to display both text and the current values for the cwd and user variables:
$ echo "Hello there" <return>
Hello there
$ echo "cwd" <return>
cwd
$ echo "$cwd" <return>
/home/dept/jsmith
$ echo "$user" <return>
jsmith
$
In the following example, the echo command displays the values of the environment variables USER and TERM:
$ echo "USER" <return>
USER
$ echo "$USER" <return>
jsmith
$ echo "$TERM" <return>
vt100
$
The printenv command displays all environment variables and their values. In the following example, the printenv command displays the environment variables:
$ printenv <return>
TERM=vt100
HOME=/home/dept/jsmith
SHELL=/bin/csh
USER=jsmith
PATH=.:/home/dept/jsmith/bin:/home/dept/bin
$
The setenv command sets the value of environment variables. In the following example, the environment variable TERM is set to vt102 with the setenv command:
$ setenv TERM vt102 <return>
When you log in to a UNIX system using the C shell (the default shell), the system searches your home directory for two files, .cshrc and .login, and executes all of the shell commands in those files. The commands in the .cshrc file are executed first, then commands in the .login file are executed. The two files differ in that the commands in .cshrc are executed every time you start a new C shell, whereas the commands in .login are executed only when you log in. If you start a new C shell, the commands in .login are not re-executed. For users who invoke the Bourne shell when they login, the system searches for a .profile file and executes all the shell commands in that file.
Usually, all of the environment variables are defined in .login and all of the shell variables are defined in .cshrc. In the following example, the cat command displays the contents of files .login and .cshrc:
$ cat .login <return>
setenv SHELL /bin/csh
set mail=/usr/spool/mail/$user
$ cat .cshrc <return>
set path=(. $HOME/bin /home/dept/bin /bin)
set history=20
alias dir 'ls -l'
alias rm 'rm -i'
alias cp 'cp -i'
alias mv 'mv -i'
set noclobber
set autologout=20
$
You can start a new C shell under the parent shell by issuing the csh command. All of the commands defined in the .cshrc file are automatically executed in this newly created C shell. Pressing <ctrl-d> kills the most recently created C shell.
|
|
Compilers on the
UNIX Systems
Example 1: Sample C Program
Example 2: Sample C Program
Example 3: Sample C++ Program
Example 4: Sample FORTRAN Program
Example 5: Sample Pascal Program
Currently there are two C compilers (DEC C and GNU C), a GNU C++ compiler, and a FORTRAN compiler available on both Academic Computing supported UNIX systems. There is also a Pascal compiler available on EAGLE. The file extension expected by the C compiler is .c. The file extension expected by the C++ compiler is .C, .cc, or .cxx. The file extension expected by the FORTRAN compiler is .f. The file extension expected by the Pascal compiler is .p. Sample programs for each of these compilers are given in this section
The cc command invokes the DEC C compiler and linkage editor. The gcc command invokes the GNU C compiler. The cc command takes a C source file as input and, if no syntax or linkage errors are found in the C source code, creates an executable image file called a.out. If a file named a.out already exists, it is deleted when the new image file is created.
In the following example, the C program random.c generates ten random numbers with the help of the C rand function.
$ cat test.c <return>
#include <stdio.h>
main ()
{
int i, k;
srand(1);
for (k=0; k !=5; k++)
{
i = rand();
printf ("Random number %2d = %d\n", k, i);
}
exit (1);
}
$ cc test.c <return>
$ a.out <return>
Random number 0 = 1103527590
Random number 1 = 377401575
Random number 2 = 662824084
Random number 3 = 1147902781
Random number 4 = 2035015474
$
In the following example, the C++ program test.cc displays two values contained within the program.
$ cat test.cc <return>
#include <iostream.h>
#include <String.h>
#include <stdlib.h>
class myclass {
public:
int a;
myclass (int i) {a = i;};
void showit() { cout << "a = " << a <<
"\n"; };
};
int main() {
myclass obj1(5), obj2(10);
obj1.showit();
obj2.showit();
exit(0);
}
$ g++ test.cc <return>
$ a.out <return>
a = 5
a = 10
$
In the following example, the FORTRAN program random.f generates ten random numbers with the help of the FORTRAN rand function.
$ cat random.f <return>
program randm
integer i, j
do 10 i=1,10
j = irand()
write (6,*) 'Random Number = ', j
10 continue
stop
end
$ f77 random.f <return>
$ a.out <return>
Random Number = 16838
Random Number = 5758
Random Number = 10113
Random Number = 17515
Random Number = 31051
Random Number = 5627
Random Number = 23010
Random Number = 7419
Random Number = 16212
Random Number = 4086
$
In the following example, the Pascal program random.p generates five numbers from a seed number that the user is prompted to supply.
$ cat random.p <return>
program random(input,output);
var RNumber,StartNumber,Counter:integer;
function Random(var Seed:integer):integer;
const
MODULUS=65536;
MULTIPLIER=25173;
INCREMENT=13489;
begin
Seed:=((MULTIPLIER*Seed)+INCREMENT)mod MODULUS;
Random:=Seed;
end;
begin
Counter:=1;
writeln('Enter a seed value.');
readln(StartNumber);
writeln('Here are five random numbers.');
while Counter<=5 do
begin
Rnumber:=Random(StartNumber);
write(Rnumber);
Counter:=Counter+1;
end;
writeln;
end.
$ pc random.p <return>
$ a.out <return>
Enter a seed value.
8 <return>
Here are five random numbers.
18265 63294 2119 8772 40261
|
|
Viewing Files
Managing Files
Input/Output Redirection
Managing Directories
Multitasking
Miscellaneous
Setting Variables
| cat | Displays file without stopping. |
| more | Displays file one screen at a time; use <ENTER> to continue or q (quit). |
| less | Displays file one screen at a time; use b (back), f (forward), and q (quit). |
| head -n | Displays first n number lines of file (default 10). |
| wc | Displays the number of lines, words, and characters in a file. |
| cmp | Compares then reports the differences in two files. |
| diff | Compares then reports the differences in two files and the ex editor commands that will make them identical. |
| grep xx | Searches for a regular expression xx in a file. |
| sort | Alphabetically sorts the lines in a file. |
| lpr | Prints a file. |
| tail -n | Displays last n number of lines of file (default 10). |
| ls | Displays contents of a directory. |
| rm | Removes (deletes) a file from a directory. |
| cp | Copies a file. |
| mv | Moves (renames) a file. |
| wc | Displays the number of lines, words, and characters in a file. |
| chmod | Changes the protection assigned to a file. |
| ls -l | Displays protection assigned to each file. |
| > | Redirect standard output. |
| >> | Redirect and append standard output. |
| >& | Redirect standard output and standard error. |
| >>& | Redirect and append standard output and standard error. |
| < | Redirect standard input. |
| <<xxx | Redirect standard input up to a line identical with xxx. |
| | | Redirect standard output to another command. |
| pwd | Prints (displays) the current working directory. |
| cd | Changes the current working directory. |
| mkdir | Makes a new directory. |
| rmdir | Removes a directory. |
| nice x & | Runs command x as a background job. |
| jobs | Displays the status of any background jobs. |
| fg | Returns a background job to the foreground. |
| stop | Stops a currently running background job. |
| kill | Cancels a currently running background job. |
| history | Displays the last 20 (by default) commands used. |
| !! | Executes the last command used. |
| !n | Executes the command numbered n on the history list (displayed by history). |
| !xxx | Executes the most recent command starting with xxx. |
| !-n | Executes command used n commands ago. |
| alias | Creates a nickname for a command. |
| cal | Displays a calendar for the current month. If specifying year, specify all four digits. |
| date | Displays the current date and time. |
| who | Displays all users who are logged into the system. |
| set | Displays current shell variables and their values. Also sets new value for specified variable. |
| echo $x | Displays current value for variable x. |
| printenv | Displays current environment variables and values. |
| setenv | Sets current value for environment variable. |
| who | Displays all users who are logged into the system. |
To see all options and arguments available with each command, type man (on EAGLE) followed by that command's name.
For an on-line help screen, type help.
|
|