4.5 First Shell Script |
#!/bin/bash echo your-name echo TABER CIS137 echo Lab 4.5: UNIX shell script echo -n "pwd >> " pwd echo -n "who am i >> " who am i echo "id >> " `id` echo '$HOME >> ' $HOMETo 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.
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.
bash
.
You might just get permission denied
if you are
running csh
.
username@gort ~ $
cmd
bash: ./cmd: Permission denied
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.
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
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.
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 ausername@gort ~ $
cmd Louis Taber TABER CIS137 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/ltaberusername@gort ~ $
rehash
is not necessary.
username@gort ~ $
cmd | lpr
username@gort ~ $
cmd >! tempusername@gort ~ $
lpr temp
your-name Lab 4.5: First Shell Script TABER CIS137Please turn your lab to Louis Taber or to Pima Community College employee in room A-115 of the Santa Rita Building. Ask them to place it in the dark blue folder in Louis Taber's mailbox.
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. |
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 |