A Bash script for simplified execution of SQL commands and SQL scripts in Oracle databases. Supports both SYSDBA and user-based connections.
- 🚀 Quick SQL execution without complex sqlplus syntax
- 👥 Support for SYSDBA and regular users
- 📂 SQL file execution
- 🎯 Automatic SID detection
- 🔒 Secure password prompting
- 🌈 Colored output for better readability
- Oracle Database Client (sqlplus)
- Bash Shell
- Properly configured Oracle environment (/etc/oratab)
- Sufficient Oracle access permissions
- Download the script:
curl -O https://raw.githubusercontent.com/yourusername/ora-sql/main/ora-sql.sh- Make it executable:
chmod +x ora-sql.sh- Optional: Move to PATH directory:
sudo mv ora-sql.sh /usr/local/bin/ora-sql./ora-sql.sh [-u user] [SID] [command|file.sql]-u user: Oracle user (optional, default: sysdba)SID: Oracle SID (optional, will be detected automatically)command|file.sql: SQL command or path to SQL file
- Check database status (as SYSDBA):
./ora-sql.sh- Check specific database:
./ora-sql.sh mydb- Execute SQL command:
./ora-sql.sh "select sysdate from dual"- Execute as specific user:
./ora-sql.sh -u system "select * from dba_users"- Execute SQL file:
./ora-sql.sh myscript.sql- Execute SQL file on specific database as user:
./ora-sql.sh -u scott mydb myscript.sqlSQL files must have the .sql extension and can contain multiple SQL commands:
-- Example myscript.sql
select sysdate from dual;
select * from v$version;
-- Comments are ignored
select username from all_users;- Detects available SIDs from /etc/oratab
- Automatically selects single SID if only one is available
- Provides selection menu for multiple SIDs
- SYSDBA access without password (default)
- Secure password prompting for regular users
- Supports all Oracle users
- Direct command execution
- Script execution
- Automatic comment removal
- Optional semicolon at end of commands
- 0: Success
- 1: Error (wrong arguments, connection error, SQL error)
The script automatically sets:
ORACLE_SID: Based on selection/inputORACLE_HOME: From /etc/oratabPATH: Extended with $ORACLE_HOME/bin
-
"No SIDs found"
- Check /etc/oratab
- Ensure Oracle is properly installed
-
"Connection error"
- Check Oracle listener
- Verify user/password
-
"Insufficient privileges"
- Check user permissions
- Use SYSDBA if needed
The repository includes a test suite:
./testsql.shTests verify:
- Basic functionality
- User connections
- Error handling
- SQL execution
- File processing
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create Pull Request
GNU
mrAibo - Aleksej Voronin
- Initial release
- SYSDBA and user support
- SQL file support
- Colored output
- Automatic SID detection
# Check database version
./ora-sql.sh "select * from v\$version"
# List all users
./ora-sql.sh -u system "select username, created from dba_users"
# Check tablespace usage
./ora-sql.sh "select tablespace_name, used_percent from dba_tablespace_usage_metrics"# Create maintenance script
cat > maintenance.sql << EOF
-- Check invalid objects
select owner, object_type, object_name from dba_objects where status = 'INVALID';
-- Check tablespace usage
select tablespace_name, used_percent from dba_tablespace_usage_metrics;
-- Check active sessions
select username, sid, serial#, status from v\$session where type != 'BACKGROUND';
EOF
# Execute maintenance script
./ora-sql.sh maintenance.sql# Create user management script
cat > user_mgmt.sql << EOF
-- List locked accounts
select username, account_status, lock_date
from dba_users
where account_status like '%LOCKED%';
-- Check password expiry
select username, account_status, expiry_date
from dba_users
where expiry_date is not null;
EOF
# Execute as system
./ora-sql.sh -u system user_mgmt.sql- The script uses secure password prompting
- No passwords are stored or logged
- SYSDBA access requires appropriate OS permissions
- Connection details are not exposed in process list
- Use appropriate indexes for queries
- Include WHERE clauses to limit results
- Use specific column names instead of SELECT *
- Consider using bind variables for repeated queries
- Oracle Instant Client or full Oracle Client
- Bash 4.0 or higher
- Standard Unix utilities (awk, grep, sed)
- Oracle sqlplus executable in PATH
Please report bugs and feature requests through the GitHub issue tracker.