0

I am trying to connect sqlplus by passing credentials as a parameter using getopts method-

Code:

while getopts ":o:s:t::i::p::f::" opt; do
  case "$opt" in

    o) uname=$OPTARG ;;
    s) sname=$OPTARG ;;
    t) password=$OPTARG ;;
    i) ip=$OPTARG ;;
    p) port=$OPTARG;;
    f) sid=$OPTARG;;
    ?) echo "I Don't Know What $OPTARG it is"
  esac
done
sqlplus -silent $uname/$password@$ip:$port/$sid

N when I am running this script as -

../a.sh -o otv4 -s OTV4 -t Password12356/$ -i 10.10.98.6 -p 1521 -f ortf

Its giving me Output something like this-

***
SQL*Plus: Release 18.0.0.0.0 - Production Version 18.3.0.0.0

Copyright (c) 1982, 2018, Oracle.  All rights reserved.

Use SQL*Plus to execute SQL, PL/SQL and SQL*Plus statements.

Usage 1: sqlplus -H | -V

    -H             Displays the SQL*Plus version and the
                   usage help.
    -V             Displays the SQL*Plus version.

Usage 2: sqlplus [ [<option>] [{logon | /nolog}] [<start>] ]

  <option> is: [-AC] [-C <version>] [-F] [-L] [-M "<options>"] [-NOLOGINTIME]
               [-R <level>] [-S]

    -AC            Enable Application Continuity.
    -C <version>   Sets the compatibility of affected commands to the
                   version specified by <version>.  The version has
                   the form "x.y[.z]".  For example, -C 10.2.0
    -F             This option improves performance in general. It changes
                   the default values settings.
                   See SQL*Plus User's Guide for the detailed settings.
    -L             Attempts to log on just once, instead of
                   reprompting on error.
    -M "<options>" Sets automatic HTML or CSV markup of output.  The options
***

And not able to connect Sqlplus. Can someone please help me with this?!

Thanks in advance!

saurabh704
  • 63
  • 6
  • 1
    If there's actually a slash in your password, you probably want to put quotes around it - `"$password"` – kfinity Feb 08 '21 at 15:54
  • Try echoing your sqlplus command instead of running it, then copy/paste/run it manually to see if you get the same error. – kfinity Feb 08 '21 at 15:56
  • Thank you @kfinity for your reply. When I am trying echo statement, like to check it is taking same thing whatever we are passing or not. And its giving right output. But when I am trying to run sqlplus, its not working dont know why?! – saurabh704 Feb 08 '21 at 16:14
  • Just an observation that this is a *very, very* insecure way to connect to the database. Your username and password will be visible to literally *anyone* else with access to your client machine. Also note that special characters in passwords almost always cause trouble. The only special character that seems to work universally on any OS and application UI is the underscore "_" . Almost anything else in the command line will trip you up sooner or later. – pmdba Feb 08 '21 at 22:11

1 Answers1

0

I test your script and it's working fine. I find your password contain $, and it should be escaped with a backslash.

you can use the next solution to escape the password:

#escape password, store value in password_esc
printf -v password_esc "%q" "$password"
sqlplus -silent $uname/$password_esc@$ip:$port/$sid 
#solution2 (more simpler)
password2=${password@Q}
sqlplus -silent $uname/$password2@$ip:$port/$sid 

A demo online to show how password is escaped.

M.Hassan
  • 10,282
  • 5
  • 65
  • 84