Tuesday, 15 May 2012

bash - bach echo variable substitution -



bash - bach echo variable substitution -

i want create query wiht bash code.

##fist extrac table name table.txt file table=$(head -n 1 tables.txt) #in code create substitution on query result=`echo "select 'result '||segment_name||':'||mb||':'||total_mb ( select tseg.segment_name,round(tseg.bytes/1024/1024) mb,l.segment_name lsegment_name,nvl(l.mb,0) lob_mb,nvl(l.mb,0)+round(tseg.bytes/1024/1024) total_mb dba_segments tseg, (select lobs.table_name,round(bseg.bytes/1024/1024) mb,lobs.segment_name dba_lobs lobs,dba_segments bseg lobs.segment_name=bseg.segment_name order bseg.bytes asc ) l tseg.segment_type='table' , tseg.segment_name='$table' , tseg.segment_name=l.table_name(+) order total_mb )where rownum=1;`

my problem on line tseg.segment_name='$table', need table name on format 'table_name'.

this actual ouput table named "aablg":

select 'result '||segment_name||':'||mb||':'||total_mb ( select tseg.segment_name,round(tseg.bytes/1024/1024) mb,l.segment_name lsegment_name,nvl(l.mb,0) lob_mb,nvl(l.mb,0)+round(tseg.bytes/1024/1024) total_mb dba_segments tseg, (select lobs.table_name,round(bseg.bytes/1024/1024) mb,lobs.segment_name dba_lobs lobs,dba_segments bseg lobs.segment_name=bseg.segment_name order bseg.bytes asc ) l tseg.segment_type='table' ' , tseg.segment_name='aablg , tseg.segment_name=l.table_name(+) order total_mb )where rownum=1;

you can see " ' " on first position, , don't know why.

regards. marco.

this much improve done without echo @ all. consider, instance:

ifs=$'\r\n ' read -r table <tables.txt ifs= read -r -d '' result <<eof select 'result '||segment_name||':'||mb||':'||total_mb ( select tseg.segment_name,round(tseg.bytes/1024/1024) mb,l.segment_name lsegment_name,nvl(l.mb,0) lob_mb,nvl(l.mb,0)+round(tseg.bytes/1024/1024) total_mb dba_segments tseg, (select lobs.table_name,round(bseg.bytes/1024/1024) mb,lobs.segment_name dba_lobs lobs,dba_segments bseg lobs.segment_name=bseg.segment_name order bseg.bytes asc ) l tseg.segment_type='table' , tseg.segment_name='$table' , tseg.segment_name=l.table_name(+) order total_mb ) rownum=1; eof

this fixes bug observed in question setting ifs value includes $'\r', carriage-return character found in dos-formatted newlines, , stripping such characters when exist @ end of first line of tables.txt.

bash unix echo

No comments:

Post a Comment