Bash 巴赫回波变量代换
我想用这个bash代码进行查询Bash 巴赫回波变量代换,bash,unix,echo,Bash,Unix,Echo,我想用这个bash代码进行查询 ##fist I will extrac the table name from table.txt file table=$(head -n 1 tables.txt) #In this code is where I will make the substitution on the query result=`echo "select 'result '||segment_name||':'||MB||':'||TOTAL_MB from ( sel
##fist I will extrac the table name from table.txt file
table=$(head -n 1 tables.txt)
#In this code is where I will make the substitution on the query
result=`echo "select 'result '||segment_name||':'||MB||':'||TOTAL_MB from (
select TSEG.segment_name,round(TSEG.bytes/1024/1024) MB,l.segment_name as LSEGMENT_NAME,nvl(l.MB,0) as LOB_MB,nvl(l.MB,0)+round(TSEG.bytes/1024/1024) as TOTAL_MB
from dba_segments tseg, (select LOBS.table_name,round(bseg.bytes/1024/1024) MB,lobs.SEGMENT_NAME
from dba_lobs lobs,dba_segments bseg
where LOBS.SEGMENT_NAME=bseg.segment_name
order by bseg.bytes asc
) l
where TSEG.segment_type='TABLE'
and TSEG.segment_name='$table'
and TSEG.SEGMENT_NAME=l.table_name(+)
order by TOTAL_MB
)where rownum=1;`
我的问题是在行TSEG.segment_name='$table',我需要格式为'table_name'的表名
这是我的实际输出,表名为“AABLG”:
你可以看到“'”在第一个位置,我不知道为什么
问候。
马可。如果根本没有回声,这会更好。例如,考虑:
IFS=$'\r\n ' read -r table <tables.txt
IFS= read -r -d '' result <<EOF
select 'result '||segment_name||':'||MB||':'||TOTAL_MB from (
select TSEG.segment_name,round(TSEG.bytes/1024/1024) MB,l.segment_name as LSEGMENT_NAME,nvl(l.MB,0) as LOB_MB,nvl(l.MB,0)+round(TSEG.bytes/1024/1024) as TOTAL_MB
from dba_segments tseg, (select LOBS.table_name,round(bseg.bytes/1024/1024) MB,lobs.SEGMENT_NAME
from dba_lobs lobs,dba_segments bseg
where LOBS.SEGMENT_NAME=bseg.segment_name
order by bseg.bytes asc
) l
where TSEG.segment_type='TABLE'
and TSEG.segment_name='$table'
and TSEG.SEGMENT_NAME=l.table_name(+)
order by TOTAL_MB
) where rownum=1;
EOF
IFS=$”\r\n“read-r表有一点需要澄清:echo
根本不做替换——echo只是将其参数与空格组合起来,打印到stdout,并在末尾添加一个换行符。所有替换都发生在调用echo
之前,其方式与任何非echo
命令完全相同。无论如何,换行可能是由于tables.txt
是DOS格式的文本文件,因此在每行末尾都有一个游离的\r
。使用诸如dos2unix
之类的工具对其进行转换,或者按照我的代码示例所做的操作,将“IFS”设置为一个将对其进行隐式修剪的值。感谢您的回复。您回答我的问题是回车字符,tables.txt文件是在windows上编辑的。
IFS=$'\r\n ' read -r table <tables.txt
IFS= read -r -d '' result <<EOF
select 'result '||segment_name||':'||MB||':'||TOTAL_MB from (
select TSEG.segment_name,round(TSEG.bytes/1024/1024) MB,l.segment_name as LSEGMENT_NAME,nvl(l.MB,0) as LOB_MB,nvl(l.MB,0)+round(TSEG.bytes/1024/1024) as TOTAL_MB
from dba_segments tseg, (select LOBS.table_name,round(bseg.bytes/1024/1024) MB,lobs.SEGMENT_NAME
from dba_lobs lobs,dba_segments bseg
where LOBS.SEGMENT_NAME=bseg.segment_name
order by bseg.bytes asc
) l
where TSEG.segment_type='TABLE'
and TSEG.segment_name='$table'
and TSEG.SEGMENT_NAME=l.table_name(+)
order by TOTAL_MB
) where rownum=1;
EOF