Shell scripts pass parameters with spaces

Source: Internet
Author: User
Tags db2 db2 connect db2 connect to

In another blog "Shell script implementation DB2 database table export to a 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, it will automatically recognize the space, the default will be a space before the 7th parameter , the following parameters are passed in:

23742 100000 rows only

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

 the-Ten- the  -:Wuyi: -[thread-9] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: --Run Command:/home/duanwf/workspace/shell2.SHMD DUANWF23742CDR_CALL_YYYYMMDD/HOME/DUANWF/ASIAINFO/EXPORT/T141015001_20141014.AVL & Fetch First100000rows only the-Ten- the  -:Wuyi: -[thread-9] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-Execute SQL ... ..... ..... the-Ten- the  -:Wuyi: -[thread-9] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-Begin to export the data: the-Ten- the  -:Wuyi: -[thread-9] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-\n2014/Ten/ the- -:Wuyi: -====================connect to md======================= the-Ten- the  -:Wuyi: -[thread-9] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-\n2014/Ten/ the- -:Wuyi: -DB2 connect to MD user DUANWF the-Ten- the  -: the: on[thread-9] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-\n2014/Ten/ the- -: the: onsucceed Connect to MD the-Ten- the  -: the: on[thread-9] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-\n2014/Ten/ the- -: the: onExport To/home/duanwf/asiainfo/export/t141015001_20141014.avl of del Modified by Nochardel codepage=1208coldel&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

" 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/bash DBSCHEMA=$1DBUSER=$2dbpassword=$3  TABLENAME=$4FILEPATH=$5DELIMITER=$6Exportlimit = 'echo ${@:7} '

Run the results again:

 the-Ten- the  at: $: to[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: $-Export PARAMETERS:MD DUANWF23742ST_ZGD_SCOPE_RSFR_GR_DM_201409/HOME/DUANWF/ASIAINFO/EXPORT/M141015003_201409.AVL & Fetch First100000rows only the-Ten- the  at: $: to[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: --Run Command:/HOME/DUANWF/WORKSPACE/SHELL/DB2.SHMD DUANWF23742ST_ZGD_SCOPE_RSFR_GR_DM_201409/HOME/DUANWF/ASIAINFO/EXPORT/M141015003_201409.AVL & Fetch First100000rows only the-Ten- the  at: $: to[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-Execute SQL ... ..... ..... the-Ten- the  at: $: to[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-Begin to export the data: the-Ten- the  at: $: to[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-\n2014/Ten/ the- at: $: to====================connect to md======================= the-Ten- the  at: $: to[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-\n2014/Ten/ the- at: $: toDB2 connect to MD user DUANWF the-Ten- the  at: $: ,[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-\n2014/Ten/ the- at: $: ,succeed Connect to MD the-Ten- the  at: $: ,[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-\n2014/Ten/ the- at: $: ,Export To/home/duanwf/asiainfo/export/m141015003_201409.avl of del Modified by Nochardel codepage=1208coldel&Select* FROM st_zgd_scope_rsfr_gr_dm_201409 Fetch first100000rows only: the-Ten- the  at: $: the[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-sql3104n Export utility is beginning exporting data to a file the-Ten- the  at: $: the[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-"/home/duanwf/asiainfo/export/m141015003_201409.avl".  the-Ten- the  at: $: the[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +- the-Ten- the  at: $: the[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-sql3105n Export utility has finished exporting"12742"line.  the-Ten- the  at: $: the[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +- the-Ten- the  at: $: the[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +- the-Ten- the  at: $: the[thread-2] INFO Com.asiainfo.dmp.proc.ExportDataServiceDB2.ExportData: +-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 ...

Examples of functions with parameters:

#!/bin/Bashfunwithparam () {Echo "The value of the first parameter is $ !"    Echo "The value of the second parameter is $!"    Echo "The value of the tenth parameter is $!"    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 $*!"}funwithparam1 2 3 4 5 6 7 8 9  the  the

Output Result:

The value of the first parameter is1!The value of the second parameter is2!The value of the tenth parameter isTen!The value of the tenth parameter is the!The value of the eleventh parameter is the!The amount of the parameters is A! thestringof the parameters is1 2 3 4 5 6 7 8 9  the  the!"

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.

Shell scripts pass parameters with spaces

Related Article

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.