Question

In: Computer Science

Simple shell scripting Write a Bash shell script called strcount.sh that counts the number of occurances...

Simple shell scripting

Write a Bash shell script called strcount.sh that counts the number of occurances of a given string within a file. The scripts takes two arguments: the name of the file to check and the string to count occurances of. If the file is not found (or not readable), the script should print the error message Cannot access file and exit with code -1. Otherwise, the script should print the number of occurances of the given string and exit with code 0. If the user fails to specify two arguments, the script should print the error message Usage: strcount.sh <file> <string> and exit with code -1.

Thus, if I have the following file penguins.txt:

  Galapagos penguin
  Erect-crested penguin
  Subantarctic gentoo penguin
  Royal penguin
  Humboldt penguin
  Eastern rockhopper penguin
  Fiordland penguin
  Allied king penguin
  Ellsworth's gentoo penguin
  White-flippered penguin
  Cook Strait little penguin
  Chatham Island little penguin

Then the output should be the following:

  ekrell@ekrell-desktop:~$ ./strcount.sh penguins.txt little
  2

Or if the file does not exist:

  ekrell@ekrell-desktop:~$ ./strcount.sh badfile.txt little
  Cannot access file

Or if the user fails to specify two arguments:

  ekrell@ekrell-desktop:~$ ./strcount.sh 
  Usage: strcount.sh <file> <string>

Write your script here

  <- ... ->
  <- ... ->
  <- ... ->
  <- Add as many lines as you need ->

Credit

Material adopted from http://faculty.smu.edu/hdeshon/

Solutions

Expert Solution

check out the solution and do COMMENT if any queries.

-------------------------------------------------------------------------------------

#!/bin/bash

# check for 2 arguments along with strcount.sh

# if there no arguments then print appropriate message

if [ "$#" -ne 2 ]; then

        echo "Usage : strcount.sh <file> <string>"

        exit -1

else

        # if arguments are present then read them in variables

        filename=$1

        str=$2

        # check if file exists

        if [ ! -f "$filename" ]; then

                echo "Cannot access file"

                exit -1

        else

                # grep - searches for the given string in given file

                # -wc : word count

                count=$(grep -wc "$str" "$filename")

                # print the count

                echo "$count"

                exit 0

        fi

fi

# program ends

---------------------------------------------------

---------------------------------------------

OUTPUT ::

------------------------------------

contents of penguins.txt


Related Solutions

this is bash scripting. a. Write a shell script that adds an extension “.deb” to all...
this is bash scripting. a. Write a shell script that adds an extension “.deb” to all the files in a directory. b. Write a command sequence or a script to insert a line “GHIJKLM” at every 10th line of a file? c. Write a bash command or script to find all the files modified in less than 5 days and print the record count of each.
Part 2 – Scripting Goals:  Write a bash script  Use linux shell commands within...
Part 2 – Scripting Goals:  Write a bash script  Use linux shell commands within your script  Provide user feedback  Use loops  Use conditionals Remember to use chmod +x to make your file executable! Each script is 5 points in the Specifications portion of the rubric. Don’t forget to maintain good standards and comments. Script 1 – Echo-back some information Write a script name hello.sh that will take the user’s first name as a command line...
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Write a BASH shell script (see the Important Notes section) that acts as a DOS command...
Write a BASH shell script (see the Important Notes section) that acts as a DOS command interpreter (the user enters the DOS command and the script executes the corresponding Linux command). The script should loop continuously until the user enters the QUIT command Prior to accepting a command, display a prompt containing your first name and the greater than symbol (>) Example prompt: linux> The DOS command, and any arguments, should be stored in variables $command – the DOS command...
Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the...
Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the files from two named directories into a new third directory. Timestamps must be preserved while copying files from one directory into another. Task Requirements Three directory names must be supplied as arguments to your script when it is executed by the user. In the beginning of your script you need to check that the first two directory names provided (e.g. dir1 and dir2) exist...
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
creating a bash script called guessingGame.sh The guessingGame.sh should generate a random number that the user...
creating a bash script called guessingGame.sh The guessingGame.sh should generate a random number that the user has to guess. Each time after a guess input the program will let the user know if their guess is too high or too low until they guess the right number. The script should keep a count of how many guesses have currently been made. If the user the has made five guesses without guessing correctly the program should give a hint and let...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string...
Linux Script Convert String (Part 1) Write a simple shell script that takes a permission string expressed as -rwxrwxrwx and prints out whether or not theobject is a directory, file or link. The string is read in from standard input. Convert String (Part 2) Modify the script so its able to print out the octal permission which the string represents along with the file type. This is in addition to part 1. Convert String (Part 3) For the script in...
Examine the following shell script and describe its function line-by-line: #! /bin/bash #This script backs up...
Examine the following shell script and describe its function line-by-line: #! /bin/bash #This script backs up the Oracle DB rm -f /SAN/backup-oracle* if tar -zcvf /SAN/backup-oracle- 'date +%F'.tar.gz/oracledb/* then echo "Oracle backup completed on 'date'" >>/var/log/oraclelog else echo "Oracle backup failed on 'date'" >>/var/log/oraclelog mail -s ALERT jason.eckert@trios.com </var/log/oraclelog fi
Please write a shell script called "myfilechecking" to determine whether a file belongs to one of...
Please write a shell script called "myfilechecking" to determine whether a file belongs to one of the following categories: A directory; A scrip file (readable and executable); An executable file (not readable but executable, such as a.out); A regular ascii file (only readable, such as a C source code file); The file cannot be recognized. Your script should display the corresponding information based on the category of the file; The file name should be provided along with your script invocation,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT