Re: basic bash help

  • From: "Radoulov, Dimitre" <cichomitiko@xxxxxxxxx>
  • To: Chris.Stephens@xxxxxxx
  • Date: Fri, 27 May 2011 21:26:54 +0200

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


Other related posts: