Shell scripts pass parameters with spaces

Source: Internet
Author: User


In another blog "shell script implementation DB2 database table export to file " implemented through the implementation of the script to export the DB2 database to a file, need to pass in seven parameters, the last is a string with a space, so when there is a problem in the incoming, will automatically recognize the space, The default is to take the space before the 7th parameter, the following is the parameters passed in:

MD DUANWF 23742 CDR_CALL_YYYYMMDD/HOME/DUANWF/ASIAINFO/EXPORT/T141015001_20141014.AVL & Fetch first 100000 rows Only

The final "Fetch first 100000 rows only" is the seventh parameter, but only fetch when recognized, the log is as follows:

 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 only2014-10-15  17:51:59 [thread-9] info  com.asiainfo.dmp.proc.exportdataservicedb2.exportdata:78  - EXECUTE SQL&NBSP, ........... 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:


How do you deal with that?

Before asked someone said can use double quotation marks to bring him, the final result is still the same, unable to get, can only get to

The Fetch

Automatically disconnects before a space.


Can be processed by the [email protected] command, the $7 will be replaced by Echo ${@:7}, so that the 7th start is automatically recognized, all obtained as the last 7th parameter, the parameter gets changed to:

#!/bin/bashdbschema=$1dbuser=$2dbpassword=$3tablename=$4filepath=$5delimiter=$6exportlimit= ' echo ${@:7} '

Run the results again:

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 only2014-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 only2014-10-15 23:36:05 [thread-2] info   COM.ASIAINFO.DMP.PROC.EXPORTDATASERVICEDB2.EXPORTDATA:78 - EXECUTE SQL&NBSP, ........... 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  utilities   Starting to export data to a file 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  The  Export  utility has finished exporting   "12742"   rows. 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 -  number of rows exported: 12742

problem Solving!!


<---------------------------------I'm gorgeous split-line--------------------------------->


Add a description of the Shell function parameters:

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


In the shell, arguments can be passed to a function when it is called. Inside the function body, the value of the parameter is obtained in the form of a $n, for example, $ $ for the first argument, and $ = for the second argument ...

example of a function with parameters:

#!/bin/bashfunwithparam () {    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 

Output Result:

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! "

Note that the $ $ cannot get the tenth parameter, and the tenth parameter requires ${10}. When n>=10, you need to use ${n} to get the parameters.

In addition, there are several special characters for handling parameters:

parameter Handling Description
$# The number of arguments passed to the script
$* Displays all parameters passed to the script in a single string
$$ The current process ID number for the script to run
$! ID number of the last process running in the background
[Email protected] With $ #相同, but use quotation marks and return each parameter in quotation marks.
$- Displays the current options used by the shell, the same as the SET command function.
$? Displays the exit status of the last command. 0 means there is no error, and any other value indicates an error.


This article is from the "Forever Love" blog, be sure to keep this source http://dwf07223.blog.51cto.com/8712758/1564600

Shell scripts pass parameters with spaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.