Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7561331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:08:10+00:00 2026-05-30T13:08:10+00:00

I’m trying to replace some annotations in ‘jsp’ pages via a bash script. My

  • 0

I’m trying to replace some annotations in ‘jsp’ pages via a bash script.

My issue is that, randomly, some files are wiped while others have the header properly replaced.

The script:

echo "Processed Files:" > processedFiles.txt

for f in $(find . -name "*.jsp")
do

    #Check if the file contains the doctype
    htmlPage=`cat $f | grep "<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Strict\/\/EN\">"` 
    oldHtmlPage=`cat $f | grep "<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 3.2\/\/EN\">"`
    if [ ${#htmlPage} -gt 0 ] || [ ${#oldHtmlPage} -gt 0 ]
    then
        echo processing $f

        # if read only, allow us to write on it
        rdonly=0
        if [ ! -w "$f" ] 
        then
            chmod +w "$f"
            rdonly=1
        fi

            # replace headers
        if [ ${#htmlPage} -gt 0 ]
        then
            cat "$f" | sed 's/<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 4.0 Strict\/\/EN">/<!DOCTYPE html>/g' > "$f"      
        fi

        if [ ${#oldHtmlPage} -gt 0 ]
        then
            cat "$f" | sed 's/<!DOCTYPE HTML PUBLIC "-\/\/W3C\/\/DTD HTML 3.2\/\/EN\">/ /g' > "$f"      
        fi

        # add file to list of processed
        echo $f'\n' >> processedFiles.txt

        # restore read only
        if [ $rdonly -eq 1 ]
        then
            chmod -w $f
        fi
    else
        # jsp without html doctype declaration
        echo ignoring $f 
    fi

done

When executing the sed command in some cases the content is properly replaced, but in others the full content is wiped, so the file to which I’m injecting the code becomes empty.

I assume that, due to the if condition, only files that contain the header to be replaced go into that logic path, and I saw no differences between the wiped files and the files that behaved as expected.

I assume that if a file would not match the sed expression the file would remain untouched (in any case the wiped files seem to contain that header)

Any idea on what may the issue be would be greatly appreciated 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:08:11+00:00Added an answer on May 30, 2026 at 1:08 pm

    This little snippet:

    cat "$f" | sed 'something' > "$f"
    

    is not really a good idea (reading and writing to the same file at the same time). It’s possible that the writing of data to the file will interfere with the reading.

    It’s probably safer to use sed -i (in-place editing).

    If your sed doesn’t have in-place editing, find one that does. Or use something like:

    cat "${f}" | sed 'something' > "${f}-next"
    mv -f "${f}-next" "${f}"
    

    And, please, if you value your sanity, make backups of the entire directory tree before you run these sort of scripts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.