I am beginner in programming/scripting and stuck with the following problem. I searched a lot on stack overflow and the net but could not resolve the issue. The detailed situation I face is described below in case somebody has a completely different approach to solve the problem.
Is there a way to use an alias
(that I call go_to
) with multiple commands but where I can pass an argument
to only the first command1
?
I want to execute goto argument
from something like alias go_to='command1 ; command2'
.
command1
should evaluate a path
based on the argument
and command2
should cd
to there.
Situation in detail
I'm executing calculations on a computer facility that uses the slurm batch system to queue and start jobs. Using the command squeue
slurm shows all running and pending jobs including their Job_ID. Using sjobs <Job_ID>
a bunch of information are displayed, including the path where the calculation was started. My goal is to go to that directory.
Of course I can do that:
squeue
to see all jobs and their Job_ID,- Pick one of the Job_IDs,
sjobs <Job_ID>
to display information,- Search with my eyes for the line that includes the path, copy the path and
cd path
to arrive where I want to go.
That is a lengthy procedure if you want to check multiple calculations. Therefore, I want to use a alias and/or a bash or python script (let's call it go_to
) so that I simply need to type go_to <Job_ID>
to arrive at the directory.
So far, using a python script I achieved to do python script.py <Job_ID>
to call sjobs
and extract the path
from it (but piping sjobs to grep and sed would also be possible).
I've already read
Why doesn't "cd" work in a shell script? and Location of cd executable and understood that os.chdir(path)
or subprocess.call(['cd',path])
will only change directories inside the python subshell and that as soon as the script is finished I will end up in the same directory where I started the script.
From what I understand only an alias
can bring me to path
. Therefore, my idea was to output the path
from the python script into a file, e.g. Path.txt
to be usable by an alias. Using the alias
alias go_to='cd $(head -1 /absolute/path/to/Path.txt)'
it is possible to change to the desired path
. But that involves letting the python script run beforehand.
The actual problem for me now is to do that in one step as I need to pass the <Job_ID>
to evaluation of the path
first.
As I said, I am quite new to scripting, so any alternative ways are welcome.