CSCI 157: UNIX Power Utilities and Shell Programming
Laboratory Assignment 4
1. TURN-IN a hard copy of the following:
- On all homework please include
- Your Name
- Course Number
- Date
- ecst login name
- No questions from the book this lab
- Write 3 scripts
- Not in perl
- We can do some perl scripts later
- Bash is good enough
- TURN-IN printouts of all three scripts
Write a simple log rotate script
- Write a script called logr
- This script will take one or more arguments (passed on the command
line). (you have to use a for loop to
- For each argument it will move that file to filename.1 (adds
the .1 extension)
- Next it will compress that file using gzip (gzip will add the
additional extension .gz). Use for "force" option so that this script
does not prompt whether or not filename.1.gz should be overwritten
on subsequent invocations (read the man page).
- Finally it will create an empty file of the same name
- If this script is called without any arguments print some kind of error
message.
- If this script is called with any arguments that do not exist print
an error message saying so.
- Make sure this script works by actually running it.
- I recommend bash but you can use any of the following for implementation
sh, ksh, csh, tcsh, zsh, but NOT perl.
- Include at least one line of documentation with the name of the script,
what it is for, and YOUR NAME
- TURN-IN
- print out of this script.
- Some additional explanation
- If I run logr on a file named "somelog" in the same directory
as somelog there would now be a file named "somelog.1.gz" and an empty file
named "somelog"
- If I run logr * in a directory that contained files a, b and
c. There would now be:
- a.1.gz (the original file compressed)
- b.1.gz (the original file compressed)
- c.1.gz (the original file compressed)
- a (an empty file)
- b (an empty file)
- c (an empty file)
- Hints:
- You can use gzip to compress and gunzip to decompress .gz files
- You can use the command touch to create empty files (for example,
"touch somefile" creates an empty file named "somefile").
- The exists script found in http://www.ecst.csuchico.edu/~toddj/257/scripts/
gives an example of testing for if a file exists.
- Look at the other example scripts I've written to see how to use
for loops and if statements.
- Alternatively, write any sort of log rotate that is cooler than this in some way.
- Possiblities include
- After archiving the log file, ftp'ing it to another machine
- Script only takes argument of one file to rotate but lets you specify things like how many backups to keep
- Instead of specifying how many backups to keep the script
automatically uses how ever many you already have, a.1.gz a.2.gz a.3.gz
- Command line parameters that can turn compression "on" or
"off" or allows you to specify the type of compression gzip, bzip2,
zip, etc.
- Instead of naming files with the extension .1 .2 .3 it could give dates like .2-11-2004 or 02112004 or similar
Text2dos Script
- Write a script called text2dos
- This will have some similarities to the addextension script
found in http://www.ecst.csuchico.edu/~toddj/257/scripts/
- This script is for converting text files from unix format to dos format.
It will take one or more arguments (passed on the command line) which
are the names of unix style text files and then for each argument it will
run the program unix2dos and then add the file extension .txt to
all of the files
- When this script is called with the -r option it will do the
opposite. For each argument it will run the command dos2unix
and remove the file extension .txt if
it exists. (You can see my rabbit script or bash
documentation to see how you can match an extension and remove it in bash).
- If this script is called without any options it should print a usage
message. Additionally if it was called with the -r option but without
any file arguments this script should print a usage message.
- I recommend bash but you can use any of the following for implementation
sh, ksh, csh, tcsh, zsh, but NOT perl.
- Include at least one line of documentation with the name of the script,
what it is for, and YOUR NAME
- TURN-IN
- a printout of this script
- Hints:
- Use a for loop for dealing with a large list of arguments
- Use an IF statement to see if it was called with the -r option
- If it was called with the -r option you will want to use a shift
- Use an IF statement to see if it was called with the right number
of arguments
- The JPGtojpg script found in http://www.ecst.csuchico.edu/~toddj/257/scripts/
gives an example of testing for a file extension.
- In unix you rename files with the mv command
Infomenu Script
- Write a script called infomenu
- Use bash's select statement to generate your menu
- If you do not want to use bash you can use any of the following for implementation
sh, ksh, csh, tcsh, zsh, but NOT perl. Whatever you use must have a select statement though
- Include at least one line of documentation with the name of the script,
what it is for, and YOUR NAME
- This script is for someone who does not know unix commands but wants to find out information on unix systems
- Somewhere you need to store the commands they can run
- This should be in a data file like infomenu.data
- There should be pairs, for example:
- Process Information : ps -elf | more
- Current Disk Usage : df -h
- Current Users : who
- Server Uptime : uptime
- Quit : exit
- The pairs can be delimited however you want
- The above examples of pairs need to work though
- To add another pair no modifications should need to be done to the script, only to the data file
- The script will generate a menu from these pairs like so
1) Process Information
2) Current Disk Usage
3) Current Users
4) Server Uptime
5) Quit
What would you like to view?
- If the user selects "1" then the command "ps -elf | more" will get ran and display process information
- Next the menu is displayed again and the user can select something else from the menu
- These kind of scripts actually get used quite a bit by people
that use UNIX but without even knowing what UNIX is. A user
account could have a script like this in their profile and when a
person logs in they would not even see a command prompt. They
probably would be running different commands than these but the idea is
still the same.
- TURN-IN
- a printout of this script