在shell指令碼中呼叫sql語句

2022-08-25 13:21:18 字數 3383 閱讀 1258

-s:靜默登入

[oracle@localhost shells]$ cat shell1.sh 

#!/bin/bash

#查詢員工資訊

sqlplus -s /nolog 《在sqlplus的eof中,

單引號中的取變數符號和外面不同

它可以取到變數值

[oracle@localhost shells]$ cat shell4.sh

#!/bin/bash

if [ $# -lt 1 ];then

echo 請輸入部門編號

exit

fisqlplus -s scott/scott《傳入乙個部門編號,查詢除部門的員工人數

並將sqlplus的結果傳到shell指令碼的變數中

[oracle@localhost shells]$ cat shell5.sh

#!/bin/bash

if [ $# -lt 1 ];then

echo 請輸入部門編號

exit

fidno=$1

num=`sqlplus -s scott/scott《傳入部門編號,查詢部門下的員工編號和姓名

[oracle@localhost shells]$ cat shell8.sh

#!/bin/bash

if [ $# -lt 1 ];then

echo 請輸入部門編號

exit

fidno=$1

sqlplus -s scott/scott > emp.txt< $file《將emp表中的所有列的資料,匯出到檔案中,

列和列之間用逗號隔開

[oracle@localhost shells]$ cat shell9.sh

#!/bin/bash

file=emp.txt

sqlplus -s scott/scott > $file《將匯出檔案放到備份伺服器或者目標伺服器

[oracle@localhost ~]$ cat shell11.sh

#!/bin/bash

exp system/"oracle"@192.168.0.33:1521/orcl file='/home/oracle/data/exp.dump' log='/home/oracle/data/exp.log' owner=scott indexes=n

scp -r /home/oracle/data/exp.dump [email protected]:/home/oracle/dmp/

[oracle@localhost ~]$ bash shell11.sh 

[email protected]'s password:

[oracle@localhost ~]$ tree

.├── data

│   ├── exp.dump

│   └── exp.log

├── dmp

│   └── exp.dump

└── shell11.sh

優化上面的語句

[oracle@localhost ~]$ cat shell12.sh 

#!/bin/bash

#匯出配置

dbuser=system

passwd=oracle

dbip=192.168.0.33

port=1521

sid=orcl

dumppath=/home/oracle/data

dt=`date "+%y%m%d%h%m%s"`

logpath=/home/oracle/data

schema=scott

dumpfile=exp_$.dump

logfile=exp_$.log

#備份伺服器配置

desuser=oracle

desip=oracle

desip=192.168.0.33

dir=/home/oracle/dmp

#將資料庫中的資料匯出

exp $dbuser/"$passwd"@$dbip:$port/$sid file=$dumppath/$dumpfile log=$dumppath/$logfile owner=$schema indexes=n

#將匯出檔案放到備份伺服器或者目標伺服器

scp -r $dumppath/$dumpfile $desuser@$desip:$dir/

[oracle@localhost ~]$ bash shell12.sh 

[email protected]'s password:

[oracle@localhost ~]$ tree

.├── data

│   ├── exp_20200307030903.dump

│   ├── exp_20200307030903.log

│   ├── exp.dump

│   └── exp.log

├── dmp

│   ├── exp_20200307030903.dump

│   └── exp.dump

├── shell11.sh

└── shell12.sh

匯出到dept.txt

sqlplus -s scott/scott > dept.txt《在oracle中新建一張表

sql> create table dept_bak as select * from dept where 1=0;

執行匯入指令碼

[oracle@localhost ~]$ cat impdept.sh 

#!/bin/bash

while read line

do if [[ -z $line ]];then

continue

fidno=`echo $line | cut -f 1 -d '|'`

name=`echo $line | cut -f 2 -d '|'`

l=`echo $line | cut -f 3 -d '|'`

sqlplus -s scott/scott > /dev/null< select * from dept_bak;

deptno dname loc

---------- -------------- -------------

10 accounting new york

20 research dallas

30 sales chicago

40 operations boston

shell指令碼中呼叫其他指令碼

目前來說有三種方法 1.指令碼絕對路徑 這個方式是最普通的,底層呼叫的是fork實現,執行的時候開乙個子shell執行呼叫的指令碼,子shell執行的時候,父shell還在 子shell執行完畢後返回父shell,子shell從父shell繼承環境變數,但是子shell中的環境變數不會帶回父shel...

在shell指令碼中呼叫sftp免密碼的配置

需要編寫乙個定時任務指令碼向ecs上傳輸檔案,使用sftp命令時會有互動輸入密碼,可以通過下列步驟配置,執行指令碼時不用輸入密碼 前提 本地機器 local代號a 遠端機器 阿里雲ecs代號b b機器上的 etc ssh sshd config中 rsaauthentication yes pubk...

Python 怎樣在python中呼叫C語言

1.寫c語言函式c.c include int add int int a,int b float add float float a,float b 2.編譯為so檔案 windows下用gcc將c檔案編譯成so檔案 gcc shared fpic o c.so c.clinux下用gcc將c檔案...