shell 写一个oracle表空间小于100G则退出执行的脚本
时间: 2024-03-08 14:50:12 浏览: 93
可以以下脚本实现:
```shell
#!/bin/bash
# 设置Oracle环境变量
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$PATH:$ORACLE_HOME/bin
# 连接到Oracle数据库
sqlplus -S /nolog <<EOF
connect username/password@database
# 查询表空间使用情况
set feedback off
set heading off
set pagesize 0
set linesize 200
SELECT ROUND(SUM(bytes) / 1024 / 1024 / 1024) as "Total Size (GB)", ROUND(SUM(bytes - blocks * block_size) / 1024 / 1024 / 1024) as "Used Size (GB)", ROUND((SUM(bytes - blocks * block_size) / SUM(bytes)) * 100) as "Used %", tablespace_name FROM dba_data_files GROUP BY tablespace_name;
# 判断表空间大小是否小于100GB
set serveroutput on
declare
ts_name varchar2(30);
ts_size number;
begin
select sum(bytes) / 1024 / 1024 / 1024 into ts_size from dba_data_files;
if ts_size < 100 then
dbms_output.put_line('Tablespace size is less than 100GB, exiting script.');
exit;
end if;
end;
/
EOF
```
脚本中的 `username/password@database` 需要替换为实际的数据库连接信息。执行脚本时,如果表空间大小小于100GB,则会输出提示信息并退出脚本。
阅读全文