Assignment 11.doc

advertisement
CST334
Assignment 11 Chapter14
Due:
BASIC SCRIPTS -- a shell is both a command line interpreter, and a programming language
Expand and explain the difference between the following shells:
bash
perl
csh
ksh
mkdir bin
cd bin
1.
In your home directory, make a bin subdirectory (if it doesn't exist)
Go into that subdirectory
2.
In your bin subdirectory, create the following file named commands using Emacs or vi
# a simple shell script
cal
date
who
3.
Save the file, Leave the editor, try to run the script
What happens?
commands
4.
Make the script executable
chmod
5.
Now try to run the script
commands
6.
Make sure the bash shell is always executed when you run this script
Go back into the Editor and add this statement to the top of the file
ug+x
commands
#!/bin/bash
POSITIONAL PARAMETERS
When you call a shell script with arguments, you can access them inside the script with the special
variables
$1, $2, etc..
$* refers to all of the arguments
The variable $0 refers to the name of the calling program or script.
7.
Create the following script called echo.args
#!/bin/bash
# illustrate the use of positional parameters
echo $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
8.
Make the script executable and try it out with this command:
echo.args To be or not to be
9.
Change the script so it looks like this:
#!/bin/bash
# illustrate the use of positional parameters, user defined
# variables and the read command
echo 'what is your name?'
read name
echo "well, $name, you typed $# arguments:"
echo "$*"
10.
Save the file, exit, try out the script
echo.args To be or not to be
THE SET COMMAND takes the output of a command and stores it in the positional parameters
date
11.
Try out the date command
Number the different pieces of the output 1, 2, 3 etc.
12.
Set the positional parameters to the output of date
set $(date)
13.
Look at the value of the positional parameters now:
echo $1 $2 $3 $4 $5 $6
14.
Make a new script file called setdate
with the following lines:
#!/bin/bash
# demonstrate the set command in a script
set $(date)
echo "Time: $4 $5"
echo "Day: $1"
echo "Date: $3 $2 $6"
15.
Save the file, make it executable, run the file
APPLICATION:
The wc ("word count") filter the counts the words, lines, and characters in a file. For example, try
running wc on the setdate file you just made:
$ wc setdate
6
23
124
setdate
(Q: what positional parameter is the filename in the output?)
The output tells us there are 6 lines, 23 words and 124 characters. To improve on wc, you are going to
make a new script called mywc, which will provide a modified output labelling each portion so it is
more readable. The output of your script mywc, used in the following command
mywc setdate should read setdate count… lines: 6 words: 23 characters: 124
To do this create the script mywc using the following plan: 1) select the bash shell, 2) run wc on the
$1 positional parameter and capture the output using set, 3) print the filename ($4) , 4) print the
number of lines ($1), number of words ($2) and number of characters ($3) using appropriate labels.
Use your script setdate as a model for this problem.
Leave the scripts commands, echo.args, setdate, and mywc in your bin directory for grading on
the due date.
Download