Question: Shell Scripting! Part 1 Requirements The shell script srchfile.sh must accept exactly two arguments. o The script shall first print the name of the file

Shell Scripting!

Part 1

Requirements

The shell script srchfile.sh must accept exactly two arguments.

o The script shall first print the name of the file being searched.

o If there are one more instances of the string in the file, the script shall print

each line that contains the matching string, along with the line number of

the file. You may use the UNIX command grep in your script to do the

actual search. In fact, grep can even print the line number if used with

the appropriate option (see man page for grep).

If the user enters more or less than the required number of arguments, the script

shall produce an error message and stop.

The first argument shall represent the string to be searched.

The second argument represents the name of the file to be searched. If the

second argument is not a file, the script shall produce an error message and

stop.

The script must be able to execute on cs.franklin.edu

The script file name must be: srchfile.sh.

The script must be located in $HOME/itec400/homework

Make sure the permissions on the your itec400 directory are 705

Make sure the permissions on your script are 705

Sample Output

Sunny Day scenario, searching for string if in file ex0210 in examples

directory:

> srchfile.sh if /export/home/dandrear/public_html/itec400/examples/ex0210

------ File = /export/home/dandrear/public_html/itec400/examples/ex0210 ------

6:if [ "$LOGNAME" = "dandrear" ]

10:if [ "`ls | wc -l`" -ne 0 ]

14:if [ -w $PWD ]

Rainy Day scenario, script is invoked with only one argument:

> srchfile.sh if

error: must provide 2 arguments.

usage: srchfile.sh search-pattern

file

Rainy Day scenario, searching for if in a directory:

> srchfile.sh if /etc

error: second argument must be a file.

usage: srchfile.sh search-pattern file

Part 2

Requirements:

The shell script srch.sh must accept exactly two arguments. The first

argument shall represent the string to be searched.

o The second argument represents either the name of the file to be searched

or a directory. If a directory is entered, then all files in the directory shall

be searched.

o The script srch.sh shall use the script srchfile.sh to search for the

string in a given file. If the second argument is a directory, then srch.sh

will invoke srchfile.sh repeatedly in a loop.

If the user enters more or less than the required number of arguments, the script

shall produce an error message and stop.

The script must be able to execute on cs.franklin.edu

The script file name must be: srch.sh

The script must be located in $HOME/itec400/homework

Make sure the permissions on the your itec400 directory are 705

Make sure the permissions on your script are 705

Sample Output

Sunny Day scenario, searching for string then in file ex0210 in examples

directory:

> srch.sh then

/export/home/dandrear/public_html/itec400/examples/ex0210

------ File = /export/home/dandrear/public_html/itec400/examples/ex0210 ------

7:then

11:then

15:then

Sunny Day scenario, searching for string then in examples which is a

directory (search all files):

> srch.sh then /export/home/dandrear/public_html/itec400/examples

------ File = /export/home/dandrear/public_html/itec400/examples/ex0010 ------

------ File = /export/home/dandrear/public_html/itec400/examples/ex0020 ------

------ File = /export/home/dandrear/public_html/itec400/examples/ex0030 ------

------ File = /export/home/dandrear/public_html/itec400/examples/ex0040 ------

------ File = /export/home/dandrear/public_html/itec400/examples/ex0050 ------

------ File = /export/home/dandrear/public_html/itec400/examples/ex0205 ------

------ File = /export/home/dandrear/public_html/itec400/examples/ex0210 ------

7:then

11:then

15:then

.

.

.

Rainy Day scenario, script is invoked with only one argument:

> srch.sh then

error: must provide 2 arguments.

usage: srchfile.sh search-pattern

file

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!