[20201109]here-doc(EOF) in bash.txt
--//在bash shell使用her-doc个人习惯使用EPF作为结束符,自己工作中经常犯一些小错误,做一些例子:
1.方法一:
sqlplus -s -l scott/book <<EOF
select * from dept;
quit
EOF
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
--//这是最常用的方式,这样的方式最大问题就是最后EOF必须顶到头,前面不能有tab或者空格。
2.方法二:
sqlplus -s -l scott/book <<-EOF
select * from dept;
quit
EOF
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO:
40 OPERATIONS BOSTON
--//如果结束EOF前面有空格,前面必须使用-EOF.
--//并且如果手工输入命令,它不会自动执行,必须在结尾加入ctrl+D,例子:
$ sqlplus -s -l scott/book <<-EOF
> select count(*) from dept;
> quit
> EOF
>
>
COUNT(*)
----------
4
--//结尾EOF输入不会执行,必须按ctrl+D,不建议采用这样的书写模式。并且结束前最好加入quit退出命令对于sqlplus。
sqlplus -s -l scott/book <<-EOF
select * from dept;
EOF
$ sqlplus -s -l scott/book <<-EOF
> select * from dept;
> EOF
>
>
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SP2-0042: unknown command "EOF" - rest of line ignored.
--//报SP2-0042错误。
sqlplus -s -l scott/book <<-EOF
select * from dept;
EOF
$ sqlplus -s -l scott/book <<-EOF
> select * from dept;
> EOF
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
--//不会报错。
3.方法3:
--//转义问题:
sqlplus -s scott/book <<EOF
$(seq 1 3| xargs -i{} echo select sysdate from dual \; )
quit
EOF
--//分号必须转义。
$ sqlplus -s -l scott/book <<EOF
> set numw 12
> select current_scn from v$database ;
> EOF
select current_scn from v
*
ERROR at line 1:
ORA-04044: procedure, function, package, or type is not allowed here
--//$database被当作变量。给EOF加入引号就可以避免这个错误,不然也要使用\转义.
$ sqlplus -s -l scott/book << 'EOF'
> set numw 12
> select current_scn from v$database ;
> quit
> EOF
CURRENT_SCN
------------
13304164054
$ sqlplus -s -l scott/book << EOF
> set numw 12
> select current_scn from v\$database ;
> quit
> EOF
CURRENT_SCN
------------
13304164077
4.总结:
--//实际上这些都是一些很细节的问题,工作中有时候遇到。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2732891/ ,如需转载,请注明出处,否则将追究法律责任。