从shell脚本调用.sql脚本时出错

时间:2017-11-16 08:50:16

标签: bash oracle shell sqlplus

民间,

我正在尝试使用shell脚本中的1个参数调用sql脚本。我的脚本如下

tab.sql

set lines 500
col file_name for a80

select file_name,bytes/1024/1024,maxbytes/1024/1024
  from dba_data_files
 where tablespace_name=upper('&TAB_NAME');
/

我的shell脚本是test.sh

#!/bin/bash

LOC=`pwd`

echo -n "Enter the name of the Tablespace: "
read -r TAB_NAME

sqlplus "/ as sysdba" <<- EOF
@$LOC/tab.sql $TAB_NAME
EOF

exit

当我执行脚本时,这就是我得到的

sh test.sh
Enter the name of the Tablespace: users

SQL*Plus: Release 11.2.0.4.0 Production on Thu Nov 16 14:17:45 2017

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected.
SQL> Enter value for tab_name:
SP2-0546: User requested Interrupt or EOF detected.
Enter value for tab_name:
SP2-0546: User requested Interrupt or EOF detected.
SQL> Disconnected

谁能告诉我这里有什么问题?我在网上搜索了这个SP2-0546,但没有解决我的问题。我在OLE-6上使用11.2.0.4 db

由于

1 个答案:

答案 0 :(得分:1)

你混淆了namedpositional替换变量,并且可能会将它们与shell变量混淆。您可以在位置上引用在SQL * Plus命令行上传递的参数:

h1 {
  font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
  font-size: 20px;
  padding: 12px 12px;
  text-align: center;
  text-transform: uppercase;
  text-rendering: optimizeLegibility;
}
h1.deepshadow {
  color: #e0dfdc;
  background-color: #333;
  letter-spacing: .1em;
  text-shadow: 0 -1px 0 #fff, 0 1px 0 #2e2e2e, 0 1px 0 #2c2c2c, 0 2px 0 #2a2a2a, 0 2px 0 #282828, 0 2px 0 #262626, 0 2px 0 #242424, 0 2px 0 #222, 0 4px 0 #202020, 0 4px 0 #1e1e1e, 0 10px 0 #1c1c1c, 0 11px 0 #1a1a1a, 0 12px 0 #181818, 0 13px 0 #161616, 0 14px 0 #141414, 0 15px 0 #121212, 0 22px 30px rgba(0, 0, 0, 0.9);
}

table {
  border-collapse: collapse;
  table-layout: fixed;
  width: 100%;
  background: white;
}

td, th {
  border: 1px solid lightgray;
  padding: 2px;
  text-align: left;
}

td:first-child {
  width: 75px;
}
td:nth-child(2) {
  width: 75px;
}
td:last-child {
  text-align: right;
  width: 120px;
}

th:first-child {
  width: 75px;
}
th:nth-child(2) {
  width: 75px;
}

th:last-child {
  text-align: right;
  width: 135px;
}

th {
  background: #333;
  color: white;
  text-transform: uppercase;
  font-weight: normal;
  height:30px;
}

table tr:first-child td {
  border-top: none;
}

.gridContainer {
  background: #ccc;
  width: 100%;
  padding: 4px; 
  box-sizing: border-box;
}

.tableWrapper {
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.tableWrapper__head{


}
.tableWrapper__body {
  max-height :300px;
  overflow-y: scroll;
}

/* media queries */
@media screen and (max-width: 1000px) {
    .gridContainer {
        max-width: 1000px;
        margin: 0 auto;
    }

    table.responsiveTableLayout {
        width: 100%;

    }

        table.responsiveTableLayout thead {
            display: none;
        }

        table.responsiveTableLayout tbody {
            width: 100%;
        }

        table.responsiveTableLayout tr, table.responsiveTableLayout th, table.responsiveTableLayout td {
            display: block;
            padding: 0;
        }
        table.responsiveTableLayout th:nth-child(n), table.responsiveTableLayout td:nth-child(n) {
            width:auto;
        }

        table.responsiveTableLayout tr {
            border-bottom: none;
            margin: 0 0 10px 0;
            /*padding: 1px;*/
        }

        table.responsiveTableLayout td {
            /*padding: 4px 0 4px 0;*/
            border-bottom: 1px dotted #ccc;
            text-align: right;
        }

            table.responsiveTableLayout td[data-title]:before {
                content: attr(data-title);
                font-weight: bold;
                display: inline-block;
                content: attr(data-title);
                float: left;
              color:teal;
                /*margin-right: 0.5em;
                font-size: 0.95em;*/
            }

            table.responsiveTableLayout td:last-child {
                padding-right: 0;
              padding-bottom:5px;
                border-bottom: 2px solid #ccc;

            }

            table.responsiveTableLayout td:empty {
                display: none;
            }
}

如果您愿意,可以定义命名的替换变量;这里可能有些过分,但对于更大的脚本清晰可能有用:

select file_name,bytes/1024/1024,maxbytes/1024/1024
  from dba_data_files
 where tablespace_name=upper('&1');
/

或简化以后的引用:

define TAB_NAME=&1

select file_name,bytes/1024/1024,maxbytes/1024/1024
  from dba_data_files
 where tablespace_name=upper('&TAB_NAME');
/

请注意,在此版本中,对define TAB_NAME=upper('&1') select file_name,bytes/1024/1024,maxbytes/1024/1024 from dba_data_files where tablespace_name=&TAB_NAME; / 的引用用单引号括起来,因为它们在替换期间应用。

我可以在测试/调试时对&TAB_NAME有所帮助,然后set verify on真正用于隐藏噪音。您可能还会发现在set verify off调用中包含-s标记以隐藏横幅文本很有用。在这种情况下,您甚至不需要heredoc,您也可以直接从命令行运行脚本:

sqlplus

(连接为sqlplus -s "/ as sysdba" @$LOC/tab.sql $TAB_NAME 用于例行查询并不理想;最好让普通用户具有查看您需要查询的数据字典表的权限,但这是一个不同的主题......)

相关问题