Masthan
Masthan

Reputation: 695

Get script path when executing it in C Shell

I have script suppose env.csh . I can source it from any where irrespective of its location

      File Path :  /tmp/dump/env.csh 

      Run Path : /usr  

      Command :  source /tmp/dump/env.csh

How to get file path when I am sourcing env.csh from / folder usr .

I used this script

  set SCRIPT=`readlink -f "$0"`
  set SCRIPTPATH=`dirname "$SCRIPT"`
  echo $SCRIPTPATH

But It is gives path as /usr

Any way to get file path

Upvotes: 2

Views: 2573

Answers (2)

Snowrabbit
Snowrabbit

Reputation: 152

If your file is named env.csh

set scriptpath=`find /proc/$$/fd -type l -lname '*env.csh' -printf '%l' + | xargs dirname`
echo $scriptpath

See also here and here.

Upvotes: 0

Masthan
Masthan

Reputation: 695

 set called=($_)
 set script_fn=`readlink -f $called[2]`
 echo $script_fn
 set script_dir = `dirname "$script_fn"`
 echo $script_dir

Upvotes: 3

Related Questions