Question: Without using the wc command , write a bash script that accepts any number of arguments. Out of these some would be options ( hyphen
Without using the wc command write a bash script that accepts any number of arguments. Out of these some would be optionshyphen plus a character like l or c and the last argument will be a file pathuse $@: to access the last argument, there is a space before Only four options are accepted by your script lwn and s
Assume that file path given will always be for a valid file and we will refer it as file in the next lines. For options,
If no option is supplied to your script do nothing.
If l option is supplied, print the number of lines in the file.
If w option is supplied, print the number of words in the file. Assume that any string between spaces is a word. ie if using awk count the number of fields in each line to get the word count.
If n option is supplied, print the number of lines having only digitsno alphabets or spaces in the file.
option s also accepts an argument say str In this case print the number of lines containing the string str If no argument is specified with s option print
The above options can be supplied together or more than once. Print the required count for each appearance of the option on a new line. For eg
if l and w are both supplied together in the sequence print count of lines and count of words each on separate lines.
If ln and l options are supplied in the sequence then print number of lines, number of lines containing only digits and finally again number of lines in the file each on separate line.
Hints:
Use while getopts style code.
Use awk to find the count. Or a combination of egrep and awk for counting lines which matches some pattern. Or pattern block of awk.
Note: Do not use single quotes in your script. Either replace each single quote with double quotesif you are not using any double quotes in your awk script or replace each single quote with
Sample
Suppose your bash script is named as myCount.sh In the below sample the argument to s option is "say" so this should count all the lines containing the string "say".
For the public test case all the commands given in the below sample are executed inthe same sequence one by one on the input file.
$ cat somefile.txt
This is a sample file
this is not end justsay start
that contains say
some number
say like
or
or
or say
and now it ends.
$ bash myCount.sh l somefile.txt
$ bash myCount.sh w somefile.txt
$ bash myCount.sh n somefile.txt
$ bash myCount.sh s say somefile.txt
$ bash myCount.sh l n somefile.txt
$ bash myCount.sh l s say l n somefile.txt
$ bash myCount.sh
$ bash myCount.sh somefile.txt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
