您的位置:首页 > 运维架构 > Shell

Shell脚本传递带有空格的参数

2014-10-15 23:40 766 查看
在另一博文《Shell脚本实现DB2数据库表导出到文件》中实现了通过脚本实现将DB2数据库导出到文件,需要传入七个参数,最后一个是一个带有空格字符串,所以传入的时候有点问题,会自动识别空格,默认会将空格前的当作第7个参数,以下是传入的参数:

MD duanwf 23742 CDR_CALL_YYYYMMDD /home/duanwf/asiainfo/export/T141015001_20141014.avl & fetch first 100000 rows only
最后的“fetch first 100000 rows only”为第七个参数,但是识别的时候只获取到fetch,日志如下:
2014-10-15 17:51:59 [Thread-9] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:70 - Run Command:   /home/duanwf/workspace/shell2.sh MD duanwf 23742 CDR_CALL_YYYYMMDD /home/duanwf/asiainfo/export/T141015001_20141014.avl & fetch first 100000 rows only
2014-10-15 17:51:59 [Thread-9] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - execute sql .................
2014-10-15 17:51:59 [Thread-9] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - Begin to export the data:
2014-10-15 17:51:59 [Thread-9] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-17:51:59 ====================connect to MD=======================
2014-10-15 17:51:59 [Thread-9] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-17:51:59 db2 connect to MD user duanwf
2014-10-15 17:52:01 [Thread-9] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-17:52:01 Succeed connect to MD
2014-10-15 17:52:01 [Thread-9] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-17:52:01 export to /home/duanwf/asiainfo/export/T141015001_20141014.avl of del modified by nochardel codepage=1208 COLDEL&  select * from CDR_CALL_YYYYMMDD fetch:


那要怎么处理呢?
之前问了有人说可以用双引号把他引起来,最后结果还是一样,无法拿到,只能获取到
"fetch
还是会自动在空格前自动断开。

可以通过$@命令来处理,即将$7换成echo ${@:7},这样将自动识别到的第7个开始,全部获取到作为最后第7个参数,参数获取改为:
#!/bin/bash

DBSCHEMA=$1
DBUSER=$2
DBPASSWORD=$3
TABLENAME=$4
FILEPATH=$5
DELIMITER=$6
EXPORTLIMIT=`echo ${@:7}`

再次运行结果:

2014-10-15 23:36:05 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:57 - Export Parameters: MD duanwf 23742 ST_ZGD_SCOPE_RSFR_GR_DM_201409 /home/duanwf/asiainfo/export/M141015003_201409.avl & fetch first 100000 rows only
2014-10-15 23:36:05 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:70 - Run Command:   /home/duanwf/workspace/shell/db2.sh MD duanwf 23742 ST_ZGD_SCOPE_RSFR_GR_DM_201409 /home/duanwf/asiainfo/export/M141015003_201409.avl & fetch first 100000 rows only
2014-10-15 23:36:05 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - execute sql .................
2014-10-15 23:36:05 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - Begin to export the data:
2014-10-15 23:36:05 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-23:36:05 ====================connect to MD=======================
2014-10-15 23:36:05 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-23:36:05 db2 connect to MD user duanwf
2014-10-15 23:36:08 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-23:36:08 Succeed connect to MD
2014-10-15 23:36:08 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - \n2014/10/15-23:36:08 export to /home/duanwf/asiainfo/export/M141015003_201409.avl of del modified by nochardel codepage=1208 COLDEL&  select * from ST_ZGD_SCOPE_RSFR_GR_DM_201409 fetch first 100000 rows only:
2014-10-15 23:36:09 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - SQL3104N  EXPORT 实用程序 正在开始将数据导出至文件
2014-10-15 23:36:09 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - "/home/duanwf/asiainfo/export/M141015003_201409.avl"。
2014-10-15 23:36:09 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 -
2014-10-15 23:36:09 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - SQL3105N  Export 实用程序已经完成导出 "12742" 行。
2014-10-15 23:36:09 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 -
2014-10-15 23:36:09 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 -
2014-10-15 23:36:09 [Thread-2] INFO  com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData:78 - 导出的行数:12742
问题解决!!

<--------------------------------- 我是华丽的分割线 --------------------------------->

补充对Shell函数参数的说明:

来源:http://www.w3cschool.cc/linux/linux-shell-func.html

在Shell中,调用函数时可以向其传递参数。在函数体内部,通过 $n 的形式来获取参数的值,例如,$1表示第一个参数,$2表示第二个参数...带参数的函数示例:
#!/bin/bash
funWithParam(){
echo "The value of the first parameter is $1 !"
echo "The value of the second parameter is $2 !"
echo "The value of the tenth parameter is $10 !"
echo "The value of the tenth parameter is ${10} !"
echo "The value of the eleventh parameter is ${11} !"
echo "The amount of the parameters is $# !"
echo "The string of the parameters is $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
输出结果:
The value of the first parameter is 1 !
The value of the second parameter is 2 !
The value of the tenth parameter is 10 !
The value of the tenth parameter is 34 !
The value of the eleventh parameter is 73 !
The amount of the parameters is 12 !
The string of the parameters is 1 2 3 4 5 6 7 8 9 34 73 !"

注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。另外,还有几个特殊字符用来处理参数:
参数处理说明
$#传递到脚本的参数个数
$*以一个单字符串显示所有向脚本传递的参数
$$脚本运行的当前进程ID号
$!后台运行的最后一个进程的ID号
$@与$#相同,但是使用时加引号,并在引号中返回每个参数。
$-显示Shell使用的当前选项,与set命令功能相同。
$?显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
本文出自 “Forever Love” 博客,请务必保留此出处http://dwf07223.blog.51cto.com/8712758/1564600
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: