How can I find the location of the tcsh shell script I'm executing?

后端 未结 4 1267
攒了一身酷
攒了一身酷 2021-02-20 05:10

Say I put an executable tcsh file in /path/to/my_script.csh

and my current directory is anywhere, for example I\'m in /path

So I type to/my_script.csh

I

4条回答
  •  醉酒成梦
    2021-02-20 05:35

    If you want an absolute path then this should help you out:

    #!/bin/tcsh -f 
    set called=($_)
    
    if ( "$called" != "" ) then  ### called by source 
       echo "branch 1"
       set script_fn=`readlink -f $called[2]`
    else                         ### called by direct execution of the script
       echo "branch 2"
       set script_fn=`readlink -f $0`
    endif
    
    echo "A:$0"
    echo "B:$called"
    set script_dir=`dirname $script_fn`
    
    echo "script file name=$script_fn"
    echo "script dir=$script_dir"
    

    Source: http://tipsarea.com/2013/04/11/how-to-get-the-script-path-name-in-cshtcsh/

提交回复
热议问题