无法通过使用 getopts

问题描述

我正在尝试通过使用 getopts 方法将凭据作为参数传递来连接 sqlplus-

代码

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 当我运行这个脚本时 -

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

它给了我这样的输出-

***
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
***

并且无法连接sqlplus。有人可以帮我解决这个问题吗?!

提前致谢!

解决方法

我测试了你的脚本,它运行良好。 我发现您的密码包含 $,应该用反斜杠转义。

您可以使用下一个解决方案来转义密码:

#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 

演示 online 演示如何转义密码。