4.5 First Shell Script |
To do this use the command:#!/bin/tcsh echo your-name echo TABER CSC137 echo Lab 4.5: UNIX shell script echo -n "pwd >> " pwd echo -n "who am i >> " who am i echo "id >> " `id` echo '$HOME >> ' $HOME
Please note that the "id" is surrounded by grave accents, not single quotes. Also note that the string:gort:~>
pico cmd
is quoted by single quotes, not grave accents.$HOME >>
This command, cat, is short for catenate. It is the UNIX "type" command. It can also be used to concatenate files together. The output you get from this command should match the file shown above.gort:~>
cat cmd
tcsh
with spell-checking on.
You might just get permission denied
if you are
running csh
.
gort:~>
cmd CORRECT>mcd (y|n|e|a))? n cmd: permission denied.
This is showing that I, the user, have read and write access. My group has read access. Everyone else also has read access. No one has eXecute permission.gort:~>
ls -l cmd -rw-r--r-- 1 ltaber 167 Jan 16 10:21 cmd
Thegort:~>
chmod a+x cmd
chmod
command also takes octal arguments, for
example:
I find the symbolic names easier to remember.gort:~>
chmod 755 cmd
r read w write x eXecute u user g group o other a all users (user, group, and other) + add permission - remove permission = set permission
Note the new x's in the permission bits.gort:~>
ls -l cmd -rwxr-xr-x 1 ltaber 167 Jan 16 10:21 cmd
csh
, not tcsh
, you will need
to issue the rehash
command. This looks "down" your search path for
executable files and builds an internal hash table to speed up locating
executable files.
The c-shell (and tcsh
) maintains an internal
hash table of where executable programs are
located on the path.
If you add a new executable program, it will
not be on your current list.
The rehash
rebuilds your hash table from scratch.
With tcsh
you only need to do this
if the program is not in the current directory.
With csh
it always needs to be done.
Because the
hash table itself is inside the shell,
the rehash
command is an
internal command, built into the shell.
Because it is an internal command, you may need to look
in the shell's manual pages
to find out the details of the rehash
command.
The hash table keeps the system from re-searching the path for each program that is run, thus speeding up overall operation at the expense of complexity and storage.
gort:~>
rehash
You may need to edit the shell script and try again. If you do, you will not need to set the eXecute bits a second time or issue thegort:~>
cmd Louis Taber TABER CSC137 Lab 4.5: UNIX shell script pwd >> /home/ltaber who am i >> gort!ltaber tty04 Jan 16 10:18 id >> uid=69(ltaber) gid=10(staff) $HOME >> /home/ltabergort:~>
rehash
command.
Once is enough. UNIX keeps the existing protection bits.
The location of the file will not change so a rehash
is not necessary.
gort:~>
cmd | lpr
gort:~>
cmd >! tempgort:~>
lpr temp
Place the lab in the instructor hand-in box in BUS R6E, the "terminal room".your-name Lab 4.5: First Shell Script TABER CSC137
Command | Usage & Function |
cal | Displays the current calendar. |
cat | Types a file to the terminal. |
date | Shows current date and time. |
echo | Echoes whatever is on the line. |
lpr | Prints a file on the line printer. |
ls | Lists a directory. |
ls -l | Lists a directory with permission bits. |
id | Shows your user id number and name, |
and your group id number(s) and name(s). | |
more | A paging program. |
pico | A simple to use text editor. |
pwd | Shows the path to the current working directory. |
tty | Shows the communications port you are connected to. |
vi | The standard UNIX text editor. |
w | Shows who is using the system. |
who | Also shows the users that are logged on to the system. |
who am i | Displays information about you. |
Special shell symbols Symbol
Meaning > Send output to a file | Send output to another program < Get input from a file >> Append output to an existing file & Run command in the background ; Used to separate two commands || Run second command if first fails && Run second command if first succeeds >! Overwrite existing output file
Grave accents | - Near the character "1" & "!" on most |
or | keyboards. Causes the enclosed string to be |
Back-ticks | passed to a sub-shell. |
or | The result is passed back to the original |
Back-quotes | shell with all new line characters removed. |
These can be nested with the proper use of | |
escape characters. | |
environment variables | When referenced in a shell script they need |
to be proceeded with the "$" character. | |
When the variable is set no, "$" is used. | |
single quotes | Prevents the shell from doing any substitution |
of the string. It is passed | |
directly to the command. | |
double quotes | Allows substitution of variables prior to |
passing the arguments to a command. Leaves | |
spaces alone and passes it as a single | |
argument. | |
4.5 First Shell Script |