Date Created: Thu 25-Nov-2010

Related Document Categories:


    Shell script to replace string using korn shell.

    replaceString()
    {
    ###################################################
    # Replace string 1 with string 2.
    # Takes 4 arguments eg:
    # replaceString "Directory" "file" "search_string" "replace_string"
    #
    ###################################################
    if [ $# -ne 4 ]
    then
    print "\nInvalid Entry! Please verify value and try again!\n"
    exit 1
    fi
    cd $1
    if [ $? -ne 0 ]; then
    print "\n\nERROR Could not cd to dir \"${1}\"!!\n\n"
    exit 1
    fi
    if [[ ! -f ${2} ]]; then
    print "\n\nERROR \"${2}\" does not exist in \"${1}\"!!"
    print " - Please re-validate and try again!\n\n"
    exit 1
    fi
    print " Replacing... \"$2\": Replacing \"$3\" with \"$4\" "
    sed -e s!"$3"!"$4"!g $2 > $$_.tmp
    if [ $? -ne 0 ]
    then
    echo "\n\nERROR in sed command during replace substring \"$2\""
    echo " - No changes made.\n\n"
    exit 1;
    fi
    mv $$_.tmp $2

Middleware Mentor - Steven Charles Robinson

About Me

Steve Robinson has been working in IT for over 15 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware consulting. Steve comes from both an administration and development background.

Before moving to JEE, Steve was an accomplished developer and consultant for both IBM Lotus Notes and Microsoft .NET Technologies.

Follow Steve as @stevencrobinson on twitter.

Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Oracle WebLogic Categories

JBoss Categories

Other Categories