IT 244: Introduction to Linux/Unix
Homework 11
Due
Sunday, April 21st at 11:59 PM
What You Need to Do
- Create the following scripts
- parameters.sh
- log_users.sh
- envir.sh
- These scripts must be in an hw11
directory under your hw directory
Setup
-
From your home directory go to your it244
directory
cd it244
- Go to your hw directory
cd hw
- Create an hw11 directory
mkdir hw11
- Enter this new directory
cd hw11
Special Instructions
- Each of these scripts must have a hashbang line
- Each of these script must be made executable
using
chmod
chmod 755 *.sh
- Each script must have a comment that describes
what the script does
- If the script needs command line arguments the
script must check that the right number are provided
- If the script does not get the right number
of command line arguments it should print an error message and quit.
Specification
parameters.sh
- The script should
-
Print the the positional parameters 0 through 4 to the terminal.
-
Print the number of arguments given to this script from the command
line.
- Each positional parameter must be labeled and
printed on a separate line
- You do not need a
for
loop for this
script
- A series of
echo
commands will do
- Be sure to test this script several times to make sure
it works
- Your output should look something like this
$ ./parameters.sh foo bar bletch bling
$0 ./parameters.sh
$1 foo
$2 bar
$3 bletch
$4 bling
$# 4
log_users.sh
- The script should
-
Define a variable log whose value
is the filename for the log file, users.log
-
Append the date to the log file whose name is contained in
log
-
Append a line of dashes to this log file to act as a separator
-
Append a list of the users currently on to pe15
to this log file
-
Append a blank line to the log file.
- The value users.log should be
assigned to the variable at the top of the script
- Thereafter, never never write "users.log" in
your script
- Use the value of log instead
- Be sure you append the output so the log grows
each time you run the script
- Run
cat
on users.log
to see the results of running this script
- After running this script, the log file
users.log should look something like this
Wed Dec 5 10:38:58 EST 2012
-----------------
Login Name Tty Idle Login Time Office Office Phone
it244gh Dummy for Glenn Hoffman pts/0 Dec 5 10:29 (dsl092-066-161.bos1.dsl.speakeasy.net)
envir.sh
- The script should
-
Test that it has been given a command line argument.
It should print a usage message and exit if no argument is given.
-
List all global variables that contain the string given in
the command line argument.
- Use an
if ... then ... else
to check
for a command line argument
- Use
env
piped into grep
along with a positional parameter
- If I wanted to see all variables that contained "SH",
I would run
./envir.sh SH
$ ./envir.sh SH
SHELL=/bin/bash
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.TQqfDTv8LD/Listeners
SHLVL=2
Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.