In this lab, you’ll have the opportunity to write some shell scripts. You should put each of your solutions into a separate script for each problem. You do not need to turn in any of these!
Write a shells cript named one.sh
.
This script will find all of the lines in a file that contain numbers, and it will print the first and last of these lines to standard output.
It will take one argument, which is the name of the file to proces.
For example, running this script on a file with these contents:
this is a sentence
one
1
this is another sentence
3
three
2
two
Would produce:
1
2
write a shells cript named two.sh
.
this program will take two command-line arguments.
the first is a single number n, and the second is the name of a file.
this program should find all words in the file that have length n.
it should print out all of these words to standard output.
for example, say that content.txt
has exactly these contents:
this is a sentence
one
1
this is another sentence
3
three
2
two
here are a few example runs of this script:
$ bash two.sh 4 content.txt
this
this
$ bash two.sh 5 content.txt
three
$ bash two.sh 7 content.txt
another
Write a shells cript named three.sh
.
This program will take four command-line arguments.
The first and second should be either a y
or an n
, which will enable or diable two options.
The third should be a word to search for.
The fouth should be a file name to search in.
The first y/n will either enable/disable a case-insitive search (you can set this with the -i
option using grep
).
The second y/n will either enable/disable showing line numbers with the search results (you can set this with the -n
option using grep
).
For example, say that content.txt
has exactly these contents:
THIS IS UPPER
this is lower
ThiS Is MIxed cAsE
Here are a few example runs of this script:
$ bash three.sh n n this content.txt
this
$ bash three.sh y n this content.txt
THIS
this
ThiS
$ bash three.sh n y this content.txt
2:this
$ bash three.sh y y this content.txt
1:THIS
2:this
3:ThiS
Write a shells cript named four.sh
.
This program will take one command-line argument - the name of a file.
This program should search for the numbers 0-9 in the file, and report how many times each appears.
For example, say that content.txt
contains this text:
The score was 2 to 4, but then in half 2 a player scores 3 points.
Why did 6 fear 7 ?
Because 7 8 9 . . .
Here is an example script execution:
$ bash four.sh content.txt
0 appears 0 times
1 appears 0 times
2 appears 2 times
3 appears 1 times
4 appears 1 times
5 appears 0 times
6 appears 1 times
7 appears 2 times
8 appears 1 times
9 appears 1 times