Shell脚本传递带有空格的参数
在另一博文《Shell脚本实现DB2数据库表导出到文件》中实现了通过脚本实现将DB2数据库导出到文件,需要传入七个参数,最后一个是一个带有空格字符串,所以传入的时候有点问题,会自动识别空格,默认会将空格前的当作第7个参数,以下是传入的参数:
MD duanwf CDR_CALL_YYYYMMDD /home/duanwf/asiainfo/export/T141015001_20141014.avl & fetch first rows only
最后的“fetch first 100000 rows only”为第七个参数,但是识别的时候只获取到fetch,日志如下:
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - Run Command: /home/duanwf/workspace/shell2.sh MD duanwf CDR_CALL_YYYYMMDD /home/duanwf/asiainfo/export/T141015001_20141014.avl & fetch first rows only
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - execute sql .................
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - Begin to export the data:
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - \n2014//-:: ====================connect to MD=======================
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - \n2014//-:: db2 connect to MD user duanwf
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - \n2014//-:: Succeed connect to MD
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - \n2014//-:: export to /home/duanwf/asiainfo/export/T141015001_20141014.avl of del modified by nochardel codepage= COLDEL& select * from CDR_CALL_YYYYMMDD fetch:
那要怎么处理呢?
之前问了有人说可以用双引号把他引起来,最后结果还是一样,无法拿到,只能获取到
"fetch
还是会自动在空格前自动断开。
可以通过$@命令来处理,即将$7换成echo ${@:7},这样将自动识别到的第7个开始,全部获取到作为最后第7个参数,参数获取改为:
#!/bin/bash DBSCHEMA=$
DBUSER=$
DBPASSWORD=$
TABLENAME=$
FILEPATH=$
DELIMITER=$
EXPORTLIMIT=`echo ${@:}`
再次运行结果:
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - Export Parameters: MD duanwf ST_ZGD_SCOPE_RSFR_GR_DM_201409 /home/duanwf/asiainfo/export/M141015003_201409.avl & fetch first rows only
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - Run Command: /home/duanwf/workspace/shell/db2.sh MD duanwf ST_ZGD_SCOPE_RSFR_GR_DM_201409 /home/duanwf/asiainfo/export/M141015003_201409.avl & fetch first rows only
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - execute sql .................
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - Begin to export the data:
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - \n2014//-:: ====================connect to MD=======================
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - \n2014//-:: db2 connect to MD user duanwf
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - \n2014//-:: Succeed connect to MD
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - \n2014//-:: export to /home/duanwf/asiainfo/export/M141015003_201409.avl of del modified by nochardel codepage= COLDEL& select * from ST_ZGD_SCOPE_RSFR_GR_DM_201409 fetch first rows only:
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - SQL3104N EXPORT 实用程序 正在开始将数据导出至文件
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - "/home/duanwf/asiainfo/export/M141015003_201409.avl"。
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: -
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - SQL3105N Export 实用程序已经完成导出 "" 行。
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: -
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: -
-- :: [Thread-] INFO com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: - 导出的行数:
问题解决!!
<--------------------------------- 我是华丽的分割线 --------------------------------->
补充对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
输出结果:
The value of the first parameter is !
The value of the second parameter is !
The value of the tenth parameter is !
The value of the tenth parameter is !
The value of the eleventh parameter is !
The amount of the parameters is !
The string of the parameters is !"
注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。
另外,还有几个特殊字符用来处理参数:
| 参数处理 | 说明 |
|---|---|
| $# | 传递到脚本的参数个数 |
| $* | 以一个单字符串显示所有向脚本传递的参数 |
| $$ | 脚本运行的当前进程ID号 |
| $! | 后台运行的最后一个进程的ID号 |
| $@ | 与$#相同,但是使用时加引号,并在引号中返回每个参数。 |
| $- | 显示Shell使用的当前选项,与set命令功能相同。 |
| $? | 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 |
Shell脚本传递带有空格的参数的更多相关文章
- Shell脚本传递带有空格的参数[摘录自网络]
参数处理 说明 $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$#相同,但是使用时加引号,并在 ...
- shell脚本传递带有空格的参数的解决方法
如下例子所示: #!/bin/sh dt= rdms_presql='select * from dm_general_stat where dimcode = "day" and ...
- shell函数传递带空格的参数
shell中的参数以空格为分割符,经常会碰到需要传递带空格的参数,例如传递带空格的文件名. 方法很简单:给参数加双引号. 但是实际效果要看你的函数内容,一种可能的情况是: 其实你真的传递进去了带空格的 ...
- JAVA传递带有空格的参数
String s="b2 + b1"; Process child = Runtime.getRuntime().exec("C:\\eclipse-workspace\ ...
- 解决shell脚本参数传递含有空格的问题
有这样一个py文件,需要传一个字典作为参数: import json import sys def parse_params(data): json_data = json.loads(data[1] ...
- Javascript函数中传递带空格的参数
通常在页面中要让某些内容点击后产 生点击事件(非页面跳转)都会使用onclick,但是这样不适于需要传递参数的情况,于是写成直接调用Javascript函数的方式:<a href=javascr ...
- Uncaught SyntaxError : Unexpected token ILLEGAL js传递带空格的参数
通常在页面中要让某些内容点击后产生点击事件(非页面跳转)都会使用onclick,但是这样不适于需要传递参数的情况,于是写成直接调用JavaScript函数的方式:<a href=javascri ...
- Shell脚本中判断输入变量或者参数是否为空的方法
shell判断一个变量是否为空方法总结 https://www.jb51.net/article/154835.htm 1.判断变量 复制代码代码如下: read -p "input a w ...
- postgresql shell脚本传递参数并执行sql脚本并
参考: https://stackoverflow.com/questions/7389416/postgresql-how-to-pass-parameters-from-command-line ...
随机推荐
- 洛谷P3957 跳房子 题解 二分答案/DP/RMQ
题目链接:https://www.luogu.org/problem/P3957 这道题目我用到了如下算法: 线段树求区间最大值: 二分答案: DP求每一次枚举答案g时是否能够找到 \(\ge k\) ...
- springboot2多数据源完整示例
springboot2 + mybatis + mysql + oracle + sqlserver多数据源的配置 相信很多朋友在开发的时候,可能会碰到需要一个项目,配置多个数据源的需求,可能是同一种 ...
- JPA多对一单向关联
在实际开发过程中,JPA多对一单向关联是使用最多的关联方式. 下面是订单与订单项的配置关系. 订单(Order):一的一方,不进行任何配置 @Entity @Table(name="orde ...
- Python--day32--复习:https和http的区别;黏包;黏包问题的解决方式;
1,https和http的区别: https比较安全,传输的时候先对内容进行加密,收到后再进行解密:它的传输内容不容易拦截,就算拦截下来了,也是加密的,看不懂.但是要买证书,一年要好几万,小公司承担不 ...
- 如何通过命令行 msbuild 编译项目
本文告诉大家如何通过 msbuild 编译一个项目,通过命令行编译可以输出更多的编译信息,可以用来调试自己写的编译相关方法,可以看到是哪个文件编译失败 在开始菜单可以找到 VisualStudio 的 ...
- git 常用操作命令(Common operation)
win10清除已登录账号密码方法 打开控制面板(Control Panel): 选择用户账户(User Accounts): 选择管理你的凭据(Credential Manager): 管理windo ...
- tab选项卡平滑滚动vue
<html lang="en"> <head> <meta charset="UTF-8"> <title>Ti ...
- 2018-2-13-win10-uwp-从Type使用构造
title author date CreateTime categories win10 uwp 从Type使用构造 lindexi 2018-2-13 17:23:3 +0800 2018-2-1 ...
- CSS兼容性问题的解决方式(更新中···)
1.清除浮动的兼容性(低版本的浏览器不兼容问题) .clearfix:after{ content:""; clear:both; display:block; visibilit ...
- 关于MySQL中查询大数据量的情况下分页limit的性能优化
https://blog.csdn.net/weixin_37848710/article/details/80772725