Search code examples
cshargvcdbackticks

change working directory from csh script


I want to lookup some data of running processes, actually I'm searching for the scratch directory to a corresponding job-ID. I managed to do that manually by the following commands (assuming the job-ID is 12345):

find ~ -name '12345.out'

This finds the output file according to the job:

/home/username/somefolder/12345.out

The scratch directory is written to a file named .scrdir in that folder, so

cd | cat /home/username/somefolder/.scrdir

brings me where I want.

I now want to combine that to a csh script or some alias in my .cshrc.


My atempt so far:

#/bin/csh

set jobfile = `find ~ -name $argv[1].out`
set jobdir = `dirname $jobfile`
set scrdir = `cat $jobdir/.scrdir`
echo $scrdir

where the first argument value is a job-ID, 12345 e.g. This script prints the right scratch directory but I want it to change my actual working directory to the scratch directory after I've called the script. How can I change the working directory from within a csh script?


I would also be grateful for any advice to refine the question / -title.


Solution

  • Since there are no shell functions in csh, the only solution I found is to call the script with source.

    The script:

    #/bin/csh
    
    set jobfile = `find ~ -name $argv[1].out`
    set jobdir = `dirname $jobfile`
    set scrdir = `cat $jobdir/.scrdir`
    cd $scrdir
    

    To call the script:

    source myscript.sh