Skip to content

Instantly share code, notes, and snippets.

@lululau
Created January 22, 2013 15:48
Show Gist options
  • Save lululau/4595697 to your computer and use it in GitHub Desktop.
Save lululau/4595697 to your computer and use it in GitHub Desktop.
test
~ $ cat test.sh
#!/bin/bash
function hello() {
echo "ARG1: $1"
echo "ARG2: $2"
echo "ARG3: $3"
}
echo "ARG_1: $1"
echo "ARG_2: $2"
echo "ARG_3: $3"
echo "ARG_4: $4"
shift # 截掉第一个参数
hello "$@"
~ $ ./test.sh a b c 'd e' f
ARG_1: a
ARG_2: b
ARG_3: c
ARG_4: d e
ARG1: b
ARG2: c
ARG3: d e
@tousinn
Copy link

tousinn commented Jan 23, 2013

恩,看来其实传到函数里面的时候并引号并没有被解释。
我现在是在function里面用了一个here document来执行数据库查询,function的参数其实是查询数据库的bind variable,因为函数的参数个数不是固定的,所以我没有像你在例子里面一样把每个参数都分别取出来放到变量里去。
而是通过shift 和$@来把所有参数传递给查询数据库的sql文。
oracle的sqlplus可以按照下面的格式来执行SQL

这种下面这种写法是可以通过的

@${SQLFilePath} ${bind variable 1} ${bind variable 2} ..........

但是下面这种写法是不行的。这个时候如果$@里面有空格的话,参数就会被分成多个

@${SQLFilePath} $@

下面这种写法也是不行的。这个时候$@会被解释成为一个参数

@${SQLFilePath} "$@"

@tousinn
Copy link

tousinn commented Jan 23, 2013

我找到解决方法了。
在shell执行的时候参数的写法像这样:

单引号双引号参数值双引号单引号

./test.sh a b c '"d e"' f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment