RE: basic bash help

From: Fowler, Kenneth R <Kenneth.R.Fowler_at_pfizer.com>
Date: Fri, 27 May 2011 15:32:02 -0400
Message-ID: <BBB354D922057B4BA2D84BA6801DB92306B6ED2C_at_ndhamrexm66.amer.pfizer.com>



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
[mailto:oracle-l-bounce_at_freelists.org] On Behalf Of Stephens, Chris Sent: Friday, May 27, 2011 2:42 PM
To: 'oracle-l_at_freelists.org' (oracle-l_at_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  

<rest of script>  

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.

--
http://www.freelists.org/webpage/oracle-l
Received on Fri May 27 2011 - 14:32:02 CDT

Original text of this message