Script to check Oracle tablespace

I'm new to unix script, and I need to check the tablespaces daily, here is the script(quite simple), but it does not work. Did I missed something ?
Please guide me, Many thanks !

#!/bin/sh

echo "ORA TABLEASPACE:"

sqlplus system/oracle as sysdba;

set pagesize 9999;
set linesize 132;

SELECT /* + RULE */  df.tablespace_name "Tablespace",
       df.bytes / (1024 * 1024) "Size (MB)",
       SUM(fs.bytes) / (1024 * 1024) "Free (MB)",
       Nvl(Round(SUM(fs.bytes) * 100 / df.bytes),1) "% Free",
       Round((df.bytes - SUM(fs.bytes)) * 100 / df.bytes) "% Used"
  FROM dba_free_space fs,
       (SELECT tablespace_name,SUM(bytes) bytes
          FROM dba_data_files
         GROUP BY tablespace_name) df
 WHERE fs.tablespace_name (+)  = df.tablespace_name
 GROUP BY df.tablespace_name,df.bytes
UNION ALL
SELECT /* + RULE */ df.tablespace_name tspace,
       fs.bytes / (1024 * 1024),
       SUM(df.bytes_free) / (1024 * 1024),
       Nvl(Round((SUM(fs.bytes) - df.bytes_used) * 100 / fs.bytes), 1),
       Round((SUM(fs.bytes) - df.bytes_free) * 100 / fs.bytes)
  FROM dba_temp_files fs,
       (SELECT tablespace_name,bytes_free,bytes_used
          FROM v$temp_space_header
         GROUP BY tablespace_name,bytes_free,bytes_used) df
 WHERE fs.tablespace_name (+)  = df.tablespace_name
 GROUP BY df.tablespace_name,fs.bytes,df.bytes_free,df.bytes_used

EOF
exit
 
sqlplus system/oracle as sysdba <<EOF
1 Like

thanks, but I got this error, any ideas ?

table spaces:
./test2.sh: sqlplus: not found

Well, you need the environment variable $PATH to include the dir where sqlplus is installed, and $ORACLE_HOME correctly set, too. I assume SQLPLUS is installed. Can I interest you in a nice portable JDBC GUI called SQuirreL, or a nice command line JDBC tool from xigole called JISQL?, or can you get an SQLPLUS installation, or TOAD?

1 Like

now its work, but I got the problem with $ sign, is there anyway to fix it ?

I never liked << for most literal to stdin, preferring "echo ' ... '| ", as single quotes are pretty much all literal. You can escape $ with \, if you like << that much.

1 Like