Find and replace a string on remote server

Main script command

ssh  user@server.XYZ.net export "'$ProjectTarget'" < /webdata/ecif/etl/scripts/dbchange.sh

dbchange.sh

 
#!/bin/bash
for file in find /devl/bfx/ecif/etl/$ProjectTarget/parameterfiles/ -exec grep -l CNTCT{} \;
do ls -l $file
sed -e 's/CNTCT/CNTCTST/' $file > $file.tmp 
mv $file.tmp $file 
done 
for file in find /devl/bfx/ecif/etl/$ProjectTarget/parameterfiles/ -exec grep -l DBCIBNR{} \;
do ls -l $file
sed  's/DBCIBNR/DBCIBC4/g' $file > $file.tmp 
mv $file.tmp $file 
done 

This script runs but do not replace the string. Can someone tell me what i am missing here. My local system os is linux and remote system is solaris.

I'm not sure what that syntax is, but I think this may be closer to what you want:

scp -pq /webdata/ecif/etl/scripts/dbchange.sh  user@server.XYZ.net 
ssh  user@server.XYZ.net  "export ProjectTarget=some_real_name && ./dbchange.sh"

Assumptions - ProejctTarget is a directory on the remote node and the shell script has to live on that node to work. Either that or you have to nfs mount your remote directory on let a local copy of dbchange.sh see it as local.

Strongly advise that you copy the script to the remote server and test it thoroughly there from the Solaris command prompt (without the bash shebang unless you have bash on the Solaris box) but with the shebang to match your Solaris account (e.g. /bin/ksh). The script contains numerous syntax errors and fundamental errors and it needs serious debugging. When debugged I would recommend that you execute the remote script from ssh.
As far as I can see the script actually does nothing useful - mainly because the "find" statements never execute. Unless this is a mispaste, the script should be giving syntax errors on the "for" and "do" lines.

My usual comment. With faulty code we need a written description of what the script should do complete with sample input and sample output.

My guess for some corrections (assuming that you run the script on the Solaris box). Totally untested and without warranty and intended as an exercise in correcting Shell syntax:

#!/bin/ksh
#
Project_Target="MYVALUE"      # This variable needs a value. Perhaps as $1 ?
#
find /devl/bfx/ecif/etl/"${ProjectTarget}"/parameterfiles/ -exec grep -l "CNTCT" {} \; | while read file
do
     ls -l "${file}"
     sed -e 's/CNTCT/CNTCTST/g' "${file}" > "${file}.tmp"
     mv "${file}.tmp" "${file}" 
done
# 
find /devl/bfx/ecif/etl/"${ProjectTarget}"/parameterfiles/ -exec grep -l "DBCIBNR" {} \; | while read file
do
     ls -l "${file}"
     sed -e 's/DBCIBNR/DBCIBC4/g' "${file}" > "${file}.tmp" 
     mv "${file}.tmp" "${file}"
done

Notice the subtle addition of space characters on the "grep" lines.

Because the script now has the potential to change live parameter files, please test on a test system having backed-up the original files.
A script of this nature should backup the original files before making changes. It's good working practice and it creates an Audit Trail.

I copied the script to the remote server and tested and it worked fine code is

#!/bin/ksh
find /devl/bfx/ecif/etl/visibility/parameterfiles/ -exec grep -l CNTCT {} \; | while read file
do ls -l $file
sed -e 's/CNTCT/CNTCTST/' $file> $file.tmp
mv $file.tmp $file
done
find /devl/bfx/ecif/etl/visibility/parameterfiles/ -exec grep -l DBCIBNR {} \; | while read file
do ls -l $file
sed  's/DBCIBNR/DBCIBC4/g' $file> $file.tmp
mv $file.tmp $file
done

when i tried the same from local i could not get the desired output.
main Code

#!/bin/bash
echo "Today is $(date)"
echo " This script will copy Prod Unix code into Breakfix Env."
echo "Choose Project: (1-18)"
echo " 1- CAM Extracts"
echo " 2- CASNCOA Extracts"
echo " 3- CDC Extracts"
echo " 4- CTMNZ Extracts"
echo " 5- CYBERLIFE  Extracts"
echo " 6- DW_ASCII_Conversion"
echo " 7- ECIF2CIFLOW"
echo " 8- ECIFNZ Extracts"
echo " 9- KEYNOTE Extracts"
echo " 10- NRS Extracts "
echo " 11- VANTAGE Extracts"
echo " 12- Prospects Extracts"
echo " 13- Visibility Extracts"
echo " 14- DW Extracts"
echo " 15- CASS MASN"
echo " 16- NWBank"
echo " 17- EASE"
echo " 18- Email Harvest"
echo " 19- CIM Stats Extract"
echo " 20- Change Identifier"
echo " 21- OFAC Extract"
echo " 22- Common Objects"
echo " 23- PLB Prefix Extract"
echo " 24- EDelivery Extract"
echo " 25- Farm Bureau Extract"
echo " 26- CNT Harvest"
echo " 27- AKM2ECIF"
echo " 28- CTM Claim Account Load"
echo " 29- Batch Processor"
echo " 30- Unload2MF"
echo " 31- ECIF_BATCHSTAT"
echo " 32- ALSEXTRACT"
echo " 33- SUPPORT"

echo "  ************************************"
echo ""
read Project
export Project1=$Project
case $Project in
        1) export Project=CAM_Extracts
	export ProjectTarget=camextract
        ;;
        2) export Project=casncoaext
	export ProjectTarget=casncoaext
        ;;
        3) export Project=cdcextract
	export ProjectTarget=cdcextract
        ;;
        4) export Project=ctmnzextract
	export ProjectTarget=ctmnzext
        ;;
        5) export Project=CYBERLIFE_EXTRACT
	export ProjectTarget=cyberlife
        ;;
        6) export Project=DW_ASCII_Conversion
	export ProjectTarget=dwascconv
        ;;
        7) export Project=ECIF2CIFLOW
	export ProjectTarget=ecif2ci
        ;;
        8) export Project=ecifnzext
	export ProjectTarget=ecifnzext
        ;;
        9) export Project=KEYNOTE_EXTRACT
	export ProjectTarget=keynote
        ;;
        10) export Project=NRS_EXTRACT
	export ProjectTarget=nrsextract
        ;;
        11) export Project=VANTAGE_EXTRACT
	export ProjectTarget=vantage
        ;;
        12) export Project=PROSPECTS_EXTRACT
	export ProjectTarget=prospects
        ;;
        13) export Project=visibility
	export ProjectTarget=visibility
        ;;
        14) export Project=dwextract
	export ProjectTarget=dwextract
        ;;
        15) export Project=cassmasn
	export ProjectTarget=cassmasn
        ;;
        16) export Project=nwbank
	export ProjectTarget=nwbank
        ;;
        17) export Project=ease
	export ProjectTarget=ease
        ;;
        18) export Project=emailharvest
	export ProjectTarget=emailharvest
        ;;
        19) export Project=cimstatsextract
	export ProjectTarget=cimstatsextract
        ;;
        20) export Project=changeid
	export ProjectTarget=changeid
        ;;
        21) export Project=ofacextract
	export ProjectTarget=ofacextract
        ;;
        22) export Project=common_objects
	export ProjectTarget=common_objects
        ;;
        23) export Project=plb_prefix_extract
	export ProjectTarget=plbextract
        ;;
        24) export Project=edelivery_extract
	export ProjectTarget=edelextrct
        ;;
        25) export Project=farm_bureau_extract
	export ProjectTarget=farm_bureau_extract
        ;;
        26) export Project=cntharvest
	export ProjectTarget=cntharvest
        ;;
        27) export Project=akm2ecif
	export ProjectTarget=akm2ecif
        ;;
        28) export Project=ctmclactld
	export ProjectTarget=ctmclactld
        ;;
        29) export Project=batchproc
	export ProjectTarget=batchproc
        ;;
        30) export Project=unload2mf
	export ProjectTarget=unload2mf
        ;;
	31) export Project=ECIF_BATCHSTAT
	export ProjectTarget=batchstats	
        ;;
	32) export Project=ALSEXTRACT
	export ProjectTarget=alsextract 
	;;
	33) export Project=support
	export ProjectTarget=support
        ;;
esac
case $Project1 in
        1) export Project=CAM_Extracts
	export ProjectTarget=camextract
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/bin/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/bin/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/scriptfiles/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/scriptfiles/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/parameterfiles/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/parameterfiles/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/sql/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/sql/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/maestro/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/maestro/
	scp  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/syncsort/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/syncsort/
	ssh  ecifetls@boom.nwie.net export "'$ProjectTarget'" < /webdata/ecif/etl/scripts/dbchange.sh ;;
esac

dbchange.sh

#!/bin/ksh

find /devl/bfx/ecif/etl/"${ProjectTarget}"/parameterfiles/ -exec grep -l "CNTCT" {} \; | while read file
do
     ls -l "${file}"
     sed -e 's/CNTCT/CNTCTST/g' "${file}" > "${file}.tmp"
     mv "${file}.tmp" "${file}" 
done
# 
find /devl/bfx/ecif/etl/"${ProjectTarget}"/parameterfiles/ -exec grep -l "DBCIBNR" {} \; | while read file
do
     ls -l "${file}"
     sed -e 's/DBCIBNR/DBCIBC4/g' "${file}" > "${file}.tmp" 
     mv "${file}.tmp" "${file}"
done

Here i m trying to copy file from Server1 Dir-1 to Server1 Dir-2 from Server2 and finding and replacing some string in parameter dir-2 of server1 .

Do anyone has any suggestions ????

Any suggestions???

1) Bumping up posts or double posting is not permitted in these forums.
2) You say you did mot get desired output, fine, what did you get then?
3) Why are you mixing scripts written in bash and ksh? knowing that linux will not understand by default ksh and solaris bash...

  1. My browser had a issue and i could not realize its going to be double post.
  2. Script ran without replacing the string mentioned in dbchange.sh script
  3. Bash script will run on Linux server and Ksh will run on Solaris this was suggested in previous post.
    Thanks

Sorry, but the above sentence makes absolutely no sense in English. Please re-phrase the statement.

Not clear which process takes place on which computer or why you do not copy the script to the remote computer first, then execute that script on the remote computer.

i m trying to find and replace a string on remote server (SOLARIS) from Local server (Linux).
dbchange.sh is the script which gets executed on Solaris server to find and replace "String" it has a variable $ProjectTarget .
I am not suppose to keep script on Remote server.

Some of the files you are copying from Server1 to Server2 appear to be the same files that you wish to edit on Server2.

If ALL the files which need editing exist on Server1 and will be copied to Server2, why not edit them (to temporary names) locally and then copy the edited versions to Server2 ?

Addendum: It would be much easier to run the whole process from Server2.

Thats correct Methyl but server1 is a production server , we should not be editing anything on that server. The ID which is used here doesn't have write access to those files.
I wish this could have been easy.. Can you guide me on how we can do this from remote. Thanks

The Development server only needs read access to the Live server files. I would suggest running the whole process on the Development server and copying the files from the Live server to the Development server and then correcting the parameters.

I have written the code just waiting for testing

scp -r  ecifetls@boom.nwie.net:/vol/ecif/etl/$ProjectTarget/parameterfiles/*.* /bfx/$ProjectTarget/parameterfiles/
	find /bfx/$ProjectTarget/parameterfiles/ -type f -exec sed -i 's/CNTCT/CNTCTST/' {} \;
	find /bfx/$ProjectTarget/parameterfiles/ -type f -exec sed -i 's/DBCIBNR/DBCIBC4/g'{} \;
	scp -r  /bfx/$ProjectTarget/parameterfiles/*.* ecifetls@boom.nwie.net:/devl/bfx/ecif/etl/$ProjectTarget/parameterfiles/	

I am having problem in copying files from local to remote, Can some one help. The last line of the code mentioned in above post.

Hi I am new to UNIX/Linux. Can someone place help me with this question? Write a one line statement to substitute the string on Server1 with Server2 in the config.xml