korn shell: dynamic use of the EOF execution method (349 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 4 (349 Views)
Accepted Solution

korn shell: dynamic use of the EOF execution method

Hello,

I have following issue :

I use in a shell script an oracle command ( RMAN ). i use the RMAN command by using the EOF execution method like below.
but the part of the channels should by dynamic. how can i create the EOF part dynamic ? with a temporary file ?

the input should be the number of channels and depend of the nummer i should create dynamic channels in the EOF part


NOW:
rman <<-EOF
connect catalog rman/${RMAN_CONNECT}@${RMAN_SID}
connect target  /
run {
   allocate channel dev_1 type 'SBT_TAPE';
...
...
   release channel dev_1;
}
EOF

NEW:
nr_channels=3  # so 3 channels should be exist in the EOF part

# create the dynamic part of channels
rman <<-EOF
connect catalog rman/${RMAN_CONNECT}@${RMAN_SID}
connect target  /
run {
   allocate channel dev_1 type 'SBT_TAPE';
   allocate channel dev_2 type 'SBT_TAPE';
   allocate channel dev_3 type 'SBT_TAPE';
...
...
   release channel dev_3;
   release channel dev_2;
   release channel dev_1;
}
EOF

regards

Acclaimed Contributor
Dennis Handly
Posts: 25,294
Registered: ‎03-06-2006
Message 2 of 4 (342 Views)

Re: korn shell: dynamic use of here docs

>i use the RMAN command by using the EOF execution method

 

(These are called here documents or here docs.)

 

>how can i create the EOF part dynamic? with a temporary file?

 

Whatever is easiest and can be understood and maintained.

 

A separate file with echo and cat may be easiest.  But you can put scripts into the here doc:

rman <<-EOF
connect catalog rman/${RMAN_CONNECT}@${RMAN_SID}
connect target  /
run {

$(

for i in dev_1 dev_2 dev_3; do

   echo "   allocate channel $i type 'SBT_TAPE';"

done

)

...

$(

for i in dev_3 dev_2 dev_1; do

   echo "   release channel $i;"
done

)

}
EOF

 

But it may be easier to just create a temp file:

tmp_file=/var/tmp/tf.$$

{

cat <<-EOF1

connect catalog rman/${RMAN_CONNECT}@${RMAN_SID}
connect target  /
run {

EOF1

 

for i in dev_1 dev_2 dev_3; do

   echo "   allocate channel $i type 'SBT_TAPE';"

done

 

cat <<-EOF2

...

EOF2

 

for i in dev_3 dev_2 dev_1; do

   echo "   release channel $i;"
done


cat <<-EOF3

}

EOF3

} > $tmp_file

 

rman < $tmp_file

 

rm -f $tmp_file  # cleanup

Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 3 of 4 (322 Views)

Re: korn shell: dynamic use of here docs

hello,

 

i prefer option 1, but option 2 is also an alternative.

 

here my solution :

 

NR_CHANNELS=3  # define number of channels, starts with "0"
i=0
(( ii = NR_CHANNELS - 1 ))

rman <<-EOF
..
#  allocate channel : dynamic part
#  allocate channel dev_0 type 'SBT_TAPE';
$( 
while [ ${i} -lt ${NR_CHANNELS} ]; do
  echo "allocate channel dev_${i} type 'SBT_TAPE';"
  (( i += 1 ))
done
)
#  release channel : dynamic part
#  release channel dev_0;
$( 
until [ ${ii} -lt 0 ]; do
  echo "release channel dev_${ii};"
  (( ii -= 1 ))
done
)
}
EOF

 

last question :

 

is there a difference between :

 

cat <<-EOF

 

and

 

cat <<EOF  ( without "-" )

 

regards

Acclaimed Contributor
Dennis Handly
Posts: 25,294
Registered: ‎03-06-2006
Message 4 of 4 (317 Views)

Re: korn shell: dynamic use of here docs

>is there a difference between: cat <<-EOF

 

As documented, that "-" causes all leading tabs to be removed

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.