Solved

bash script that replaces string in files to be used in cygwin

Posted on 2004-04-22
4
5,711 Views
Last Modified: 2013-12-26
I just installed cygwin on my windows XP.  I want to write a bash script to find this "<!--<~~" string in all the jsp files and replace it with "<%" string.  Below is my attempt on the bash script and the errors I got. Note, it even complains about the mv command.  Can anyone help me? If you have the solution, please give me some explanation as well since I am new to shell programming. Thanks.

#!/bin/bash
# Filename: srTags.sh
# Find all jsp file
# Search for <!--<~~ tag and replace with <% tag in all jsp files found
# Search for ~~>--> tag and replace with %> tag

  OLDTAG1="<!--<~~"
  NEWTAG1="<%"
  OLDTAG2="~~>-->"
  NEWTAG2="%>"
 
  for FILE in 'find . -type f -name *.jsp -print';
  do
     sed -e 's/$OLDTAG1/$NEWTAG1/g' -e 's/$OLDTAG2/$NEWTAG2/g' $FILE > temp
     mv temp $FILE
  done
exit 0
#-------------END BASH ------------------------

Here are the errors:

[jvsaha] site > ./srTags.sh
sed: invalid option -- t
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  -i[suffix], --in-place[=suffix]
                 edit files in place (makes backup if extension supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
  -V, --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

E-mail bug reports to: <email address removed>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
mv: invalid option -- t
Try `mv --help' for more information.
[jvsaha] site >


0
Comment
Question by:jvsaha
4 Comments
 
LVL 20

Assisted Solution

by:tfewster
tfewster earned 25 total points
Comment Utility
It looks like you have single quotes (') round the find statement instead of backticks (`);  And "*.jsp" should be enclosed in double-quotes, as shown, so the shell doesn't expand it.

Apart from that, try putting "echo $FILE" in just before the `sed...` line, to make sure the `find` isn't returning a dodgy filename, eg -- t
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 100 total points
Comment Utility
Hi jvsaha,
> for FILE in 'find . -type f -name *.jsp -print';
>   do
>      sed -e 's/$OLDTAG1/$NEWTAG1/g' -e 's/$OLDTAG2/$NEWTAG2/g' $FILE > temp
>      mv temp $FILE
>   done
> exit 0

You have a couple of glitches there:

> for FILE in 'find . -type f -name *.jsp -print';
You'll get 'find . -type f -name *.jsp -print' as a loop argument, and you need to quote your wildcards in find:
Use $( find . -type f -name "*.jsp" -print ) instead.

>sed -e 's/$OLDTAG1/$NEWTAG1/g' -e 's/$OLDTAG2/$NEWTAG2/g' $FILE > temp

The single quote mode does not expand shell variables. Use double quotes instead:
sed -e "s/$OLDTAG1/$NEWTAG1/g" -e "s/$OLDTAG2/$NEWTAG2/g" $FILE > temp

Also be careful with file name variables which may contain spaces. You can get undesired side effects, as a file name can be split in two.
You're on the safe side when you always use "$FILE" instead of $FILE.

Cheers,
Stefan
0
 

Author Comment

by:jvsaha
Comment Utility
Tried what Stefan said & it worked.  However for those directories with space in their name, it doesn't work.  Is there a way to take care of this problem?

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now