FAQ
I'm writing a script to allow the sysadmin's to kill any active sessions for a particular database user.

If the script is called as root, I re-invoke it as oracle. However, 'dirname' doesn't seem to be working as expect and I have no idea why or how to get around it. A little bit of googl'ing didn't help either so I turn to old faithful. :)

#!/bin/sh

export ORAENV_ASK=NO
export ORACLE_SID=xxxxxxx

export SCRIPT=`basename $0`
export SCRIPT_DIR=`dirname $0`

echo "${SCRIPT_DIR} is the script dir."
# Re-invoke this script as oracle if invoked as root
CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`
if [ "$CUSER" = "root" ]
then

su - oracle -c "${SCRIPT_DIR}/${SCRIPT}"
exit $?

fi

The script output is:

". is the script dir."

The script is located in /home/oracle/scripts/sql/topsecret

Anybody know what the deal is?

I'm guessing it has something to do with sub-shells or something but I don't know how to work around it. I guess I can always hardcode the path but I would prefer not to.

Thanks for any help!

chris

CONFIDENTIALITY NOTICE:

This message is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this message is not the intended recipient or the employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by email reply.

Search Discussions

  • David Fitzjarrell at May 27, 2011 at 6:51 pm
    Check the path for '.' as that allows running scripts/programs from the current
    directory, which is where you are when you run  the script.  If you remove that
    entry from the path then the 'proper' directory name should appear.
    Â
    David Fitzjarrell

    ________________________________
    From: "Stephens, Chris"
    To: "'oracle-l@freelists.org' (oracle-l_at_freelists.org)"
    Sent: Fri, May 27, 2011 11:42:28 AM
    Subject: basic bash help

    I’m writing a script to allow the sysadmin’s to kill any active sessions for a
    particular database user.Â

    Â
    If the script is called as root, I re-invoke it as oracle.  However, ‘dirname’
    doesn’t seem to be working as expect and I have no idea why or how to get around
    it.  A little bit of googl’ing didn’t help either so I turn to old faithful. J
    Â
    #!/bin/sh
    Â
    export ORAENV_ASK=NO
    export ORACLE_SID=xxxxxxx
    Â
    export SCRIPT=`basename $0`
    export SCRIPT_DIR=`dirname $0`
    Â
    echo "${SCRIPT_DIR} is the script dir."
    # Re-invoke this script as oracle if invoked as root
    CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`
    if [ "$CUSER" = "root" ]
    then
            su - oracle -c "${SCRIPT_DIR}/${SCRIPT}"
            exit $?
    fi
    Â

    Â
    The script output is:
    Â
      “. is the script dir.”
    Â
    The script is located in /home/oracle/scripts/sql/topsecret
    Â
    Anybody know what the deal is?
    Â
    I’m guessing it has something to do with sub-shells or something but I don’t
    know how to work around it.  I guess I can always hardcode the path but I would
    prefer not to.
    Â
    Thanks for any help!
    Â
    chris
    Â
    CONFIDENTIALITY NOTICE:
    This message is intended for the use of the individual or entity to which it is
    addressed and may contain information that is privileged, confidential and
    exempt from disclosure under applicable law. If the reader of this message is
    not the intended recipient or the employee or agent responsible for delivering
    this message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please notify us
    immediately by email reply.
  • David Fitzjarrell at May 27, 2011 at 7:10 pm
    And you are correct, setting '.' in the path doesn't return '.' as the
    directory name.  I said that with the untested assumption that it would.  I
    won't do that again.  :)

    Calling the script in this manner does return '.' as the directory name:

    ./

    $ envtest.sh
    /export/home/dfitzjarrell-admin/bin is the script dir.

    $  ./envtest.sh
    . is the script dir.
    Â
    David Fitzjarrell

    ________________________________
    From: "Stephens, Chris"
    To: David Fitzjarrell
    Sent: Fri, May 27, 2011 12:00:24 PM
    Subject: RE: basic bash help

    Thanks, but I don’t think that’s it:
    Â
    [root_at_050orareddb vmware]# cat kill_vm.sh
    #!/bin/sh
    Â
    export ORAENV_ASK=NO
    export ORACLE_SID=yyyyy
    Â
    export SCRIPT=`basename $0`
    export SCRIPT_DIR=`dirname $0`
    Â
    echo "${SCRIPT_DIR} is the script dir."
    # Re-invoke this script as oracle if invoked as root
    CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`
    if [ "$CUSER" = "root" ]
    then
            su - oracle -c "${SCRIPT_DIR}/${SCRIPT}"
            exit $?
    fi
    Â

    Â
    Â
    [root_at_xxxxxxx ]#./kill_vm.sh
    . is the script dir.
    PATH:
    /bin:/jdk/jre/lib/i386/server:/rdbms/lib:/lib:/usr/lib:/usr/X11R6/lib:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/sbin:/usr/sbin:/root/bin:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin:/home/oracle/bin:/u01/app/oracle/product/11.2.0/dbhome_1/bin

    -bash: ./kill_vm.sh: No such file or directory
    [root_at_xxxxxxx ]#
    Â
    From:David Fitzjarrell
    Sent: Friday, May 27, 2011 1:52 PM
    To: Stephens, Chris; oracle-l_at_freelists.org
    Subject: Re: basic bash help
    Â
    Check the path for '.' as that allows running scripts/programs from the current
    directory, which is where you are when you run  the script.  If you remove that
    entry from the path then the 'proper' directory name should appear.
    Â
    David Fitzjarrell
    Â
    Â
    Â

    ________________________________

    From:"Stephens, Chris"
    To: "'oracle-l_at_freelists.org' (oracle-l_at_freelists.org)"
    Sent: Fri, May 27, 2011 11:42:28 AM
    Subject: basic bash help
    I’m writing a script to allow the sysadmin’s to kill any active sessions for a
    particular database user.Â

    Â
    If the script is called as root, I re-invoke it as oracle.  However, ‘dirname’
    doesn’t seem to be working as expect and I have no idea why or how to get around
    it.  A little bit of googl’ing didn’t help either so I turn to old faithful. J
    Â
    #!/bin/sh
    Â
    export ORAENV_ASK=NO
    export ORACLE_SID=xxxxxxx
    Â
    export SCRIPT=`basename $0`
    export SCRIPT_DIR=`dirname $0`
    Â
    echo "${SCRIPT_DIR} is the script dir."
    # Re-invoke this script as oracle if invoked as root
    CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`
    if [ "$CUSER" = "root" ]
    then
            su - oracle -c "${SCRIPT_DIR}/${SCRIPT}"
            exit $?
    fi
    Â

    Â
    The script output is:
    Â
      “. is the script dir.”
    Â
    The script is located in /home/oracle/scripts/sql/topsecret
    Â
    Anybody know what the deal is?
    Â
    I’m guessing it has something to do with sub-shells or something but I don’t
    know how to work around it.  I guess I can always hardcode the path but I would
    prefer not to.
    Â
    Thanks for any help!
    Â
    chris
    Â

    CONFIDENTIALITY NOTICE:
    This message is intended for the use of the individual or entity to which it is
    addressed and may contain information that is privileged, confidential and
    exempt from disclosure under applicable law. If the reader of this message is
    not the intended recipient or the employee or agent responsible for delivering
    this message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please notify us
    immediately by email reply.

    CONFIDENTIALITY NOTICE:
    This message is intended for the use of the individual or entity to which it is
    addressed and may contain information that is privileged, confidential and
    exempt from disclosure under applicable law. If the reader of this message is
    not the intended recipient or the employee or agent responsible for delivering
    this message to the intended recipient, you are hereby notified that any
    dissemination, distribution or copying of this communication is strictly
    prohibited. If you have received this communication in error, please notify us
    immediately by email reply.
  • Radoulov, Dimitre at May 27, 2011 at 7:26 pm

    On 27/05/2011 20:42, Stephens, Chris wrote:
    I'm writing a script to allow the sysadmin's to kill any active
    sessions for a particular database user.

    If the script is called as root, I re-invoke it as oracle. However,
    'dirname' doesn't seem to be working as expect and I have no idea why
    or how to get around it. A little bit of googl'ing didn't help either
    so I turn to old faithful. J
    [...]

    You could use something like this (assuming a *POSIX* shell):

    SCRIPT_DIR=$(

    cd -P -- "$(dirname -- "$0")" && pwd -P
    )

    It won't work if you invoke the script as:

    sh script_name

    and the script is not in the current directory.

    In that case you could use something like this:

    SCRIPT_DIR=$(

    _script=$0
    case $_script in

    ( */* );;
    ( * ) [ -e "$_script" ] || _script=$(command -v -- "$0")

    esac
    cd -P -- "$(dirname -- "$_script")" && pwd -P
    )

    It won't work in all situations, but it may be sufficient :)

    Regards
    Dimitre
  • Maxim Demenko at May 27, 2011 at 7:49 pm
    On linux it can be done relatively simple this way:
    cat whatami
    #!/bin/bash
    echo $(readlink -f $0)
    ./whatami
    /home/oracle/whatami

    Best regards

    Maxim
    On 27.05.2011 21:26, Radoulov, Dimitre wrote:
    On 27/05/2011 20:42, Stephens, Chris wrote:


    I'm writing a script to allow the sysadmin's to kill any active
    sessions for a particular database user.

    If the script is called as root, I re-invoke it as oracle. However,
    'dirname' doesn't seem to be working as expect and I have no idea why
    or how to get around it. A little bit of googl'ing didn't help
    either so I turn to old faithful. J
    [...] >
    You could use something like this (assuming a *POSIX* shell):


    SCRIPT_DIR=$(
    cd -P -- "$(dirname -- "$0")" && pwd -P
    )


    It won't work if you invoke the script as: >
    sh script_name >
    and the script is not in the current directory. >
    In that case you could use something like this:


    SCRIPT_DIR=$(
    _script=$0
    case $_script in
    ( */* );;
    ( * ) [ -e "$_script" ] || _script=$(command -v -- "$0")
    esac
    cd -P -- "$(dirname -- "$_script")" && pwd -P
    )

    It won't work in all situations, but it may be sufficient :)
  • Radoulov, Dimitre at May 27, 2011 at 7:53 pm

    On 27/05/2011 21:49, Maxim Demenko wrote:
    On linux it can be done relatively simple this way:
    cat whatami
    #!/bin/bash
    echo $(readlink -f $0)
    ./whatami
    /home/oracle/whatami
    ... assuming readlink is available :)

    Regards
    Dimitre
  • Radoulov, Dimitre at May 27, 2011 at 7:58 pm

    On 27/05/2011 21:53, Radoulov, Dimitre wrote:
    On 27/05/2011 21:49, Maxim Demenko wrote:
    On linux it can be done relatively simple this way:
    cat whatami
    #!/bin/bash
    echo $(readlink -f $0)
    ./whatami
    /home/oracle/whatami
    ... assuming readlink is available :)
    Oops, sorry!
    I just missed the first line (On linux ... ).

    Regards
    Dimitre
  • Fowler, Kenneth R at May 27, 2011 at 7:32 pm
    I tend to use the following code fragment in many of my scripts...



    $ cat test.sh
    #!/bin/sh
    case $0 in
    /*)

    FILE_NAME=$0

    ;;
    *)

    FILE_NAME=`printf "%s/%s\n" $PWD $0 | sed -e s'!/\./!/!'g`
    ;;
    esac
    #
    echo $FILE_NAME



    It will give desired results no matter how it is called, full pathname
    or relative etc...



    $ pwd
    /orabackup/ocdba/Ken/stuff
    $ test.sh
    /orabackup/ocdba/Ken/stuff/test.sh
    $ ./test.sh
    /orabackup/ocdba/Ken/stuff/test.sh
    $ /orabackup/ocdba/Ken/stuff/test.sh
    /orabackup/ocdba/Ken/stuff/test.sh
    $ cd ..
    $ stuff/test.sh
    /orabackup/ocdba/Ken/stuff/test.sh
    $ ./stuff/test.sh
    /orabackup/ocdba/Ken/stuff/test.sh
    $ /orabackup/ocdba/Ken/stuff/test.sh
    /orabackup/ocdba/Ken/stuff/test.sh



    Regards,
    Ken.

    From: oracle-l-bounce_at_freelists.org
    On Behalf Of Stephens, Chris
    Sent: Friday, May 27, 2011 2:42 PM
    To: 'oracle-l@freelists.org' (oracle-l@freelists.org)
    Subject: basic bash help

    I'm writing a script to allow the sysadmin's to kill any active sessions
    for a particular database user.



    If the script is called as root, I re-invoke it as oracle. However,
    'dirname' doesn't seem to be working as expect and I have no idea why or
    how to get around it. A little bit of googl'ing didn't help either so I
    turn to old faithful. J



    #!/bin/sh



    export ORAENV_ASK=NO

    export ORACLE_SID=xxxxxxx



    export SCRIPT=`basename $0`

    export SCRIPT_DIR=`dirname $0`



    echo "${SCRIPT_DIR} is the script dir."

    # Re-invoke this script as oracle if invoked as root

    CUSER=`id |cut -d"(" -f2 | cut -d ")" -f1`

    if [ "$CUSER" = "root" ]

    then

    su - oracle -c "${SCRIPT_DIR}/${SCRIPT}"

    exit $?

    fi





    The script output is:



    ". is the script dir."



    The script is located in /home/oracle/scripts/sql/topsecret



    Anybody know what the deal is?



    I'm guessing it has something to do with sub-shells or something but I
    don't know how to work around it. I guess I can always hardcode the
    path but I would prefer not to.



    Thanks for any help!



    chris



    CONFIDENTIALITY NOTICE:

    This message is intended for the use of the individual or entity to
    which it is addressed and may contain information that is privileged,
    confidential and exempt from disclosure under applicable law. If the
    reader of this message is not the intended recipient or the employee or
    agent responsible for delivering this message to the intended recipient,
    you are hereby notified that any dissemination, distribution or copying
    of this communication is strictly prohibited. If you have received this
    communication in error, please notify us immediately by email reply.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouporacle-l @
categoriesoracle
postedMay 27, '11 at 6:42p
activeMay 27, '11 at 7:58p
posts8
users5
websiteoracle.com

People

Translate

site design / logo © 2022 Grokbase