Google Answers Logo
View Question
Q: Bash assignment help ( Answered,   0 Comments )
Subject: Bash assignment help
Category: Computers > Programming
Asked by: derekwtp-ga
List Price: $25.00
Posted: 28 Apr 2003 08:01 PDT
Expires: 28 May 2003 08:01 PDT
Question ID: 196522
help deciphering and contructing

Write a shell script, change-lines, which will substitute a string for
a replacement string for each occurance of the string in files
specified.  The original file will be saved, with the same filename
with the .keep extension unless the -n option is present.  You may use
a temporary file for this script. Remember the search string and or
replacement string can have special characters in them. Use proper
quoting techniques.

The script will use

   * the option -s "string" for the search string
   * the option -r "string" for the replacement string
   * the -n option to prevent a backup file from being created
   * the -h option to print a usage statement and exit

The script will exit with an error status and print  an error message:

   * if the -s option is not present.
   * if the -r option is not present.
   * if the string is not specified with the -s option
   * if the string is not specified with the -r option
   * if there are any other options specified
   * if there are no input files specified
   * if the input file cannot be read by the current process
   * if the input file cannot be written by the current process
   * if you cannot create the backup file.

Here is the usage statement for the script:

     change-lines [-n] -s search string -r replace string files ...

          -n    do not backup the original file
          -s    search string the search for this string
          -r    replace string replace the search string with
          this string
          -h    print this message


     prompt> change-lines -s foobar -r FooBar *.c
     This script will edit all the files *.c, and each occurrence
     of foobar it will be replaced by FooBar it will be replaced by
     FooBar. Each file will be backed up.

     prompt> change-lines -s "SHELL IS NOT FUN" -r "SHELL PROGRAMING
IS A LOT OF FUN"  /tmp/file1.txt
     This script will edit all the file /tmp/file1.txt , and each
     occurrence of SHELL IS NOT FUN will replaced by the text, SHELL
     PROGRAMMING IS A LOT OF FUN. A backup file /tmp/file1.txt.keep
     will be created.

You may use temporary files in this project.

Request for Question Clarification by dogbite-ga on 28 Apr 2003 11:22 PDT
Hey derekwtp-ga,

  Sure, I can write that script for
  you and explain what it does.  I
  will put the solution here:

  by 5:00 tonight.  Sound good?


Clarification of Question by derekwtp-ga on 28 Apr 2003 12:20 PDT
sounds great. Thanks dog.
Subject: Re: Bash assignment help
Answered By: dogbite-ga on 28 Apr 2003 12:47 PDT
Hey derekwtp-ga, 

  Okay, I put the script in the zip file linked
  in my request for clarification.

  Let me know how it works out for you.

There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy