Help with sed in Oracle DB backup script.

I have a script like this ..

#!/bin/ksh
database=$(echo $@ | sed 's/.*-S \([a-zA-Z0-9]*\).*/\1/')
instance=$(grep "$database" /var/opt/oracle/oratab | awk -F : '{print $1}')
command=$(echo $@ | sed 's/"$database"/"$instance"/')
echo  $command

when I tried to execute the script like this ...

ksh -x rman_wrap.sh rman_backup.pl -S IRMD51 -io 
+ echo /opt/dba/oraadmin/temp/rman_backup.pl -S IRMD51 -io
+ sed 's/.*-S \([a-zA-Z0-9]*\).*/\1/'
+ database=IRMD51
+ grep IRMD51 /var/opt/oracle/oratab
+ awk -F : '{print $1}'
+ instance=IRMD511
+ echo /opt/dba/oraadmin/temp/rman_backup.pl -S IRMD51 -io
+ sed 's/"$database"/"$instance"/'
+ command='/opt/dba/oraadmin/temp/rman_backup.pl -S IRMD51 -io'
+ echo /opt/dba/oraadmin/temp/rman_backup.pl -S IRMD51 -io
/opt/dba/oraadmin/temp/rman_backup.pl -S IRMD51 -io

it's failing on sed command . syntax is correct . Can anyone suggest what's wrong ?

Thanks

sed 's/'"$database"'/'"$instance"'/'

Modify the line as :

command=$(echo $@ | sed  "s/$database/$instance/")

I am executing this script ( via AUTOSYS) from another host . And it's still giving me this error ....

sed: -e expression #1, char 8: unknown option to `s'

Well it really depends on what's in those variables you're feeding into sed there.

Perhaps this may be of use, some awk to change the first occurence of string s with replacement text r in a way that isn't susceptible to regular expression metacharacters nor string escape sequences:

awk -v s="$database" -v r="$instance" 'i=index($0,s){$0=substr($0,1,i-1) r substr($0,i+length(s))}1'