Created
February 25, 2010 19:16
-
-
Save sms420/314926 to your computer and use it in GitHub Desktop.
Show_Oracle_Users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr//bin/env ruby | |
################################################################################ | |
# | |
# Program Name: Show_Oracle_Users.rb | |
# Author: Sean Stephenson | |
# Course: CS151B | |
# Date: 2010-02-07 | |
# Adapted From: oracle_in_ruby.rb AND ShowTables (both by Abbas) | |
# | |
# ShowUsers.rb will do the followings: | |
# 1. running ShowUsers without argument will display all users in sorted order | |
# 2. with -n and a regular expression as an argument, it will display total | |
# number of users matched with the regular expression. | |
# 3. with a regular expression as an argument, will display all users matched | |
# 4. with -n as an argument, it will display the total number of users | |
# | |
################################################################################# | |
#*********************************do_oracle(sql)********************************* | |
def do_oracle(sql) | |
sql.chop! if sql[-1] == ';' | |
cmd = <<-EOJ | |
sqlplus -S scott/tiger <<-EOF | |
set echo off head off pagesize 0 | |
#{sql}; | |
set echo on head on pagesize 24 | |
quit | |
EOF | |
EOJ | |
done = system(cmd) | |
abort("access to Oracle failed\n") unless done | |
end | |
#**********************************numUsers************************************** | |
def numUsers | |
print "total number of users: " | |
do_oracle("select count(username) from all_users") | |
end | |
#*********************************regExDisplay(match)**************************** | |
def regExDisplay(match) | |
puts "users matching regexp: " | |
str = "select distinct username from all_users | |
where regexp_like(username, '#{match}', 'i') | |
order by username" | |
do_oracle(str) | |
end | |
#*********************************regExCount(match)****************************** | |
def regExCount(match) | |
print "total users matching regexp: " | |
str = "select count(username) from all_users | |
where regexp_like(username, '#{match}', 'i')" | |
do_oracle(str) | |
end | |
# displays all users in sorted order | |
if ARGV[0] == nil | |
do_oracle("select distinct username from all_users order by username") | |
end | |
# displays total number of users matched with regular expression | |
if ARGV[0] == '-n' && ARGV[1] =~ /\w/ | |
regExCount(ARGV[1]) | |
exit 0 #needed so we don't also display total users below | |
end | |
# displays all users matched by regular expression | |
if ARGV[0] =~ /\w/ && ARGV[0] != "-n" | |
regExDisplay(ARGV[0]) | |
end | |
# displays the total number of users | |
if ARGV[0] == "-n" | |
numUsers | |
end | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment