標籤:串連數 $1 today time file 最簡 bsp nbsp 輸入
[[email protected] shell_test]$ cat echo_time #!/bin/sh一.最簡單的調用sqlplussqlplus -S "sys/unimas as sysdba" << !select to_char(sysdate,‘yyyy-mm-dd‘) today from dual;exit;![[email protected] shell_test]$ ./echo_time
運行結果:
TODAY----------2011-03-21
-S 是silent mode,不輸出類似“SQL>”,串連資料庫,關閉資料庫之類的資訊。
EOF
eof可以是任何字串 比如"laldf"那麼當你輸入單獨一行laldf時"shell認為輸入結束,但是必須表示塊開始必須使用<<;
開始和結束要匹配這個符號“<<”後面的內容
舉例子:
[[email protected] shell_test]$ sqlplus -s "sys/unimas as sysdba" << abc> select to_char(sysdate,‘yyyy-mm-dd‘) today from dual;> exit;> abc
運行結果
TODAY----------2011-03-21
二.sqlplus的結果傳遞給shell的方法一
[[email protected] shell_test]$ cat test2.sh #!/bin/bashVALUE=`sqlplus -S "test/unimas" << !set heading offset feedback offset pagesize 0set verify offset echo offselect to_char(sysdate,‘yyyy-mm-dd‘) today from dual;exit!`echo $VALUEif [ -n "$VALUE" ]; thenecho "The rows is $VALUE"exit 0elseecho "There is no row"fi
三.sqlplus的結果傳遞給shell的方法二
[[email protected] shell_test]$ cat test1.sh #!/bin/bashsqlplus -S "test/unimas" << !set heading offset feedback offset pagesize 0set verify offset echo offcol coun new_value v_counselect count(*) coun from lesson;exit v_coun!VALUE="$?"echo "show row:$VALUE"
col coun new_value v_coun v_coun為number類型。因為exit 只能返回數實值型別。
四.把shell參數傳遞給sqlplu
#!/bin/basht_id="$1"sqlplus -S "test/unimas" << !set heading offset feedback offset pagesize 0set verify offset echo offselect teachername from teacher where id=$t_id;exit!
五.sqlplus的結果儲存在檔案中
#!/bin/shsqlplus -S "test/unimas"<<EOFset heading offset feedback offset pagesize 0set verify offset echo offspool spool_fileSELECT * from teacher;spool offexit;EOF
shell調用sqlplus查詢oracle