Hi folks,
I have the following program:
#! /bin/ksh
set -xv
export grepDataSource="</data-sources>"
export DB_HOST=tornado
export SCHEMA_NAME=IAS10G
export dataSource=data-sources.xml
sourceString="<data-source class=\"oracle.jdbc.pool.OracleConnectionCacheImpl\" name=\"RiGHTvDS\" location=\"jdbc/RiGHTvociDS\" xa-location=\"jdbc/xa/RiGHTvXADS\" ejb-location=\"jdbc/RiGHTvDS\" connection-driver=\"oracle.jdbc.driver.OracleDriver\" username=\"${SCHEMA_NAME}\" password=\"${SCHEMA_PASS}\" url=\"jdbc:oracle:thin:@${DB_HOST}:1521:${ORACLE_SID}\" inactivity-timeout=\"30\" max-connections=\"20\" min-connections=\"20\"/>"
lineNum=`grep -n ${grepDataSource} ${dataSource} | awk -F: '{print $1}'`
sed ${lineNum}i'\${sourceString}\' ${dataSource}
In this program I need to insert the string $sourceString before line </data-sources>.
My problem is with the parameter $sourceString:
latform:/tmp> ./update_data_source.ksh
export grepDataSource="</data-sources>"
+ export grepDataSource=</data-sources>
export DB_HOST=tornado
+ export DB_HOST=tornado
export SCHEMA_NAME=IAS10G
+ export SCHEMA_NAME=IAS10G
export dataSource=data-sources.xml
+ export dataSource=data-sources.xml
sourceString="<data-source class=\"oracle.jdbc.pool.OracleConnectionCacheImpl\" name=\"RiGHTvDS\" location=\"jdbc/RiGHTvociDS\" xa-location=\"jdbc/xa/RiGHTvXADS\" ejb-location=\"jdbc/RiGHTvDS\" connection-driver=\"oracle.jdbc.driver.OracleDriver\" username=\"${SCHEMA_NAME}\" password=\"${SCHEMA_PASS}\" url=\"jdbc:oracle:thin:@${DB_HOST}:1521:${ORACLE_SID}\" inactivity-timeout=\"30\" max-connections=\"20\" min-connections=\"20\"/>"
+ sourceString=<data-source class="oracle.jdbc.pool.OracleConnectionCacheImpl" name="RiGHTvDS" location="jdbc/RiGHTvociDS" xa-location="jdbc/xa/RiGHTvXADS" ejb-location="jdbc/RiGHTvDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="IAS10G" password="" url="jdbc:oracle:thin:@tornado:1521:DB10g" inactivity-timeout="30" max-connections="20" min-connections="20"/>
lineNum=`grep -n ${grepDataSource} ${dataSource} | awk -F: '{print $1}'`
+ grep -n </data-sources> data-sources.xml
+ awk -F: {print $1}
+ lineNum=30
sed ${lineNum}i'\${sourceString}\' ${dataSource}
+ sed 30i\${sourceString}\ data-sources.xml
<?xml version="1.0" standalone='yes'?>
<!DOCTYPE data-sources PUBLIC "Orion data-sources" "http://xmlns.oracle.com/ias/dtds/data-sources-9_04.dtd">
<data-sources>
<!--
An example/default DataSource that uses
Oracle JDBC-driver to create the connections.
This tag creates all the needed kinds
of data-sources, transactional, pooled and EJB-aware sources.
The source generally used in application code is the "EJB"
one - it provides transactional safety and connection
pooling. Oracle thin driver could be used as well,
like below.
url="jdbc:oracle:thin:@host:port:sid"
-->
<data-source
class="com.evermind.sql.DriverManagerDataSource"
name="OracleDS"
location="jdbc/OracleCoreDS"
xa-location="jdbc/xa/OracleXADS"
ejb-location="jdbc/OracleDS"
connection-driver="oracle.jdbc.driver.OracleDriver"
username="scott"
password="tiger"
url="jdbc:oracle:thin:@//localhost:1521/oracle.regress.rdbms.dev.us.oracle.com"
inactivity-timeout="30"
/>
${sourceString}
</data-sources>
As you can see,The parameter ${sourceString} was not converted.
I tried to add quotation marks before the parameter in the sed command:
sed ${lineNum}i'\"'"${sourceString}"'"\' ${dataSource}
And the results are:
......
"<data-source class="oracle.jdbc.pool.OracleConnectionCacheImpl" name="RiGHTvDS" location="jdbc/RiGHTvociDS" xa-location="jdbc/xa/RiGHTvXADS" ejb-location="jdbc/RiGHTvDS" connection-driver="oracle.jdbc.driver.OracleDriver" username="IAS10G" password="" url="jdbc:oracle:thin:@tornado:1521:DB10g" inactivity-timeout="30" max-connections="20" min-connections="20"/>"
</data-sources>
The problem in this case is that the sed added quotation marks (") in the beginning and in the end of the line.
Which marks and where should I put around the parameter in the sed command?
Thanks in advance,
Nir