- Create the following shell script file called cmd
using the pico (or vi) text editor:
#!/bin/bash
echo your-name
echo TABER CIS135
echo Lab 4.8: UNIX shell script
echo -n "pwd >> "
pwd
echo -n "who am i >> "
who am i
echo "id >> " `id`
echo '$HOME >> ' $HOME
To do this use the command:
username@gort ~ $
pico cmd
Please note that the "id" is surrounded by grave accents, not single quotes.
Also note that the string:
$HOME >>
is quoted by single quotes, not grave accents.
- When you have finished, verify your work with the following command:
username@gort ~ $
cat cmd
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.
- Try to run your shell script. This should fail. It might fail in a different
way. The example below assumes that you are running
bash
.
You might just get permission denied
if you are
running csh
.
username@gort ~ $
cmd
bash: ./cmd: Permission denied
- Check the file protection bits. Use the ls command. This is
the UNIX "dir" command. The -l is an option for the "long"
format output.
username@gort ~ $
ls -l cmd
-rw-r--r-- 1 ltaber 167 Jan 16 10:21 cmd
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.
- Change the protection bits. To give everyone eXecute
permission, use the chmod command:
username@gort ~ $
chmod a+x cmd
The chmod
command also takes octal arguments, for
example:
username@gort ~ $
chmod 755 cmd
I find the symbolic names easier to remember.
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
- Check the protection bits.
username@gort ~ $
ls -l cmd
-rwxr-xr-x 1 ltaber 167 Jan 16 10:21 cmd
Note the new x's in the permission bits.
- Run your shell script and check your output.
username@gort ~ $
cmd
Louis Taber
TABER CIS135
Lab 4.8: 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/ltaber
username@gort ~ $
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.
Once is enough. UNIX keeps the existing protection bits.
The location of the file will not change so a rehash
is not necessary.
-
Print out a copy of your results (when your output is correct).
There are two ways to print out your results:
- Pipe your cmd's output
into the print program. To do this, use the pipe symbol "|".
The command will look like:
username@gort ~ $
cmd | lpr
- Create a temporary file by using re-direction.
Then print the file. The output redirection symbol is ">".
When used with an exclamation point, it can be used to over-write
an exisiting file. (">!").
The commands will look like this:
username@gort ~ $
cmd >! temp
username@gort ~ $
lpr temp
The print command may be lp on your
system.
To look at the printer queue use the lpq command.
Turn in your output from step Several UNIX commands
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. |
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. |
|