Unix Software Tools. More On Scripts As usual, work in your scratch

advertisement
Unix Software Tools. More On Scripts
As usual, work in your scratch directory. When you have finished a question
copy your scripts into your ~/scripts directory. I will start checking these in
the labs (and tick your name), and these scripts will be helpful for your
coursework! Read all of the questions in order. These programs are again
underspecified (for example, precisely what messages to type out), so try to
get the basic functionality correct.
Unix file permissions Your friend is very lazy and has not learn how to read
Unix file permissions. You want to help him/her. Write a script called
readWrite.sh which prompts the user for a filename. If the file does not
exist, the program says so. If it is a directory, the program says so. If it is a
file, then it give the read and write permissions.
Cut and Paste and Translate Messy or inconsistent data is often a problem
in computer science. In the first file, the data is in rows and separated by a
space. In the second file, the data is in columns separated by”;”. What cut,
paste, tr commands would you use to get this data into a consistent format.
Try this in stages. You could call this file cleanData.sh. The two files are
below.
FileVertical.txt
John Wood 12 13
Andy Green 66 77
Dave Smith 90 98
FileHorizontal.txt
Mary;Paul;James
Whitehouse;Blyth;Bond
34;256;23
44;55;66
Removing Vowels Use one of the commands from a lecture to remove all
the vowels (aeiou) from a piece of text (just download something from the
web). For example “the quick brown fox jumped over the lazy dogs” will
become “th qck brwn fx jmpd vr th lzy dgs”. (do you know what is special
about this sentence) After you have removed all the vowels, can you still
understand it? Some linguists think that meaning is carried by consonants
(i.e. non-vowels) and feeling or emotion is carried by the vowels. Is it the
same in Chinese, or do tones play a part (I don’t know, maybe you can tell
me – may help your English too ).
Average Write a script called average3.sh which is run with exactly three
arguments. If not exactly three arguments are supplied, the program is
terminated and returns and exit status of 1. Otherwise it prints out a
message giving the average. The shell is only really designed to do integer
arithmetic. This exercise is just about checking the number of parameters.
Try one of the following two (either Prime or Parity). Prime Write a script
called prime.sh which, when given an integer as an argument, tells you if it
is prime. Parity Write a script called parity.sh which prints out if the integer
input is odd or even.
Enter Password Write a script called enterPassword.sh which asks for a
password. The password must be 4 to 8 characters long. Do not echo the
password on the screen and only wait for 10 seconds, after which the system
times out (for security reasons).
Menu Write a script called menu.sh which asks the user to press 1, 2, or 3.
For now just print e.g. “you pressed 2”. Press 0 to quit. If 0-3 is not pressed,
then say “invalid option”. We may use this type of program later and you will
have almost certainly made a program like this in java. Note that you could
used some of the constructs already covered in the lectures, but there might
be more suitable ones (hint think about menus in Java, look at the example
scripts on the shared drive too).
Download