Last active
December 20, 2017 20:32
-
-
Save ezarko/74c14ad6b3ff628a91cb35186d6e3cfe to your computer and use it in GitHub Desktop.
A convenience function which turns the complex sqlplus syntax into getopt style arguments. Also supports using sqlplus in a docker image/container.
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
function sqlplus { | |
local USER=SYS | |
local PASS=<default_password> | |
local NET | |
local HOST=localhost | |
local PORT | |
local SID=<default_sid> | |
local ROLE=SYSDBA | |
local EDITION | |
local DEFAULT_DOCKER_IMAGE=oracle/database:12.2.0.1-ee | |
local DOCKER_IMAGE | |
local DOCKER_CONTAINER | |
# read the options | |
local TEMP=`getopt -o 'u:p:n:h:P:s:r::e:d:D:' --long 'user:,pass:,net:,host:,port:,sid:,role::,edition:,docker-run:,docker-exec:' -n 'sqlplus' -- "$@"` | |
eval set -- "$TEMP" | |
# extract options and their arguments into variables. | |
while true ; do | |
case "$1" in | |
-u|--user) | |
case "$2" in | |
"") shift 2 ;; | |
*) USER=$2 ; shift 2 ;; | |
esac ;; | |
-p|--pass) | |
case "$2" in | |
"") shift 2 ;; | |
*) PASS=$2 ; shift 2 ;; | |
esac ;; | |
-n|--net) | |
case "$2" in | |
"") shift 2 ;; | |
*) NET=$2 ; shift 2 ;; | |
esac ;; | |
-h|--host) | |
case "$2" in | |
"") shift 2 ;; | |
*) HOST=$2 ; shift 2 ;; | |
esac ;; | |
-P|--port) | |
case "$2" in | |
"") shift 2 ;; | |
*) PORT=$2 ; shift 2 ;; | |
esac ;; | |
-s|--sid) | |
case "$2" in | |
"") shift 2 ;; | |
*) SID=$2 ; shift 2 ;; | |
esac ;; | |
-r|--role) | |
case "$2" in | |
"") shift 2 ;; | |
SYSDBA|SYSOPER|SYSASM) ROLE=$2 ; shift 2 ;; | |
*) echo "role must be blank, or one of SYSDBA, SYSOPER, or SYSASM" ; exit 1 ;; | |
esac ;; | |
-e|--edition) | |
case "$2" in | |
"") shift 2 ;; | |
*) EDITION=$2 ; shift 2 ;; | |
esac ;; | |
-d|--docker-run) | |
if [ -n "$DOCKER_CONTAINER" ]; then | |
echo "only one of docker-run or docker-exec can be specified" ; exit 1 | |
fi | |
case "$2" in | |
"") DOCKER_IMAGE=$DEFAULT_DOCKER_IMAGE ; shift 2 ;; | |
*) DOCKER_IMAGE=$2 ; shift 2 ;; | |
esac ;; | |
-D|--docker-exec) | |
if [ -n "$DOCKER_IMAGE" ]; then | |
echo "only one of docker-run or docker-exec can be specified" ; exit 1 | |
fi | |
case "$2" in | |
"") echo "no container name specified, ignoring $1" ; shift 2 ;; | |
*) DOCKER_CONTAINER=$2 ; shift 2 ;; | |
esac ;; | |
--) shift ; break ;; | |
*) echo "Internal error!" ; exit 1 ;; | |
esac | |
done | |
local ARGS=/ | |
if [ -n "$USER" ]; then | |
ARGS=$USER | |
if [ -n "$PASS" ]; then | |
ARGS=$ARGS/$PASS | |
fi | |
if [ -n "$NET" ]; then | |
ARGS=$ARGS@$NET | |
else | |
if [ -n "$HOST" ]; then | |
ARGS=$ARGS@$HOST | |
if [ -n "$PORT" ]; then | |
ARGS=$ARGS:$PORT | |
fi | |
if [ -n "$SID" ]; then | |
ARGS=$ARGS/$SID | |
fi | |
fi | |
fi | |
fi | |
if [ -n "$ROLE" ]; then | |
ARGS="$ARGS AS $ROLE" | |
fi | |
if [ -n "$EDITION" ]; then | |
ARGS="$ARGS EDITION=$EDITION" | |
fi | |
ARGS="$ARGS $@" | |
if [ -n "$DOCKER_IMAGE" ]; then | |
echo "/bin/docker run --rm -it $DOCKER_IMAGE sqlplus $ARGS" | |
rlwrap /bin/docker run --rm -it $DOCKER_IMAGE sqlplus $ARGS | |
elif [ -n "$DOCKER_CONTAINER" ]; then | |
echo "/bin/docker exec -it $DOCKER_CONTAINER sqlplus $ARGS" | |
rlwrap /bin/docker exec -it $DOCKER_CONTAINER sqlplus $ARGS | |
else | |
echo "sqlplus $ARGS" | |
rlwrap $ORACLE_HOME/bin/sqlplus $ARGS | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment