2

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:

  1. squeue to see all jobs and their Job_ID,
  2. Pick one of the Job_IDs,
  3. sjobs <Job_ID> to display information,
  4. Search with my eyes for the line that includes the path, copy the path and
  5. 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.

Marcel Sad
  • 23
  • 2
  • 5
    You can't pass arguments to *any* alias; use a function instead. – chepner Jul 04 '18 at 13:01
  • I see, but that does not solve the problem I am facing. A function is placed inside a script. And the script will only change the directory while it is running and after it is completed return to the directory where it started. So even a function will not help. Correct me if I'm wrong – Marcel Sad Jul 04 '18 at 13:51
  • You are half wrong/half right. A function is placed inside a script, right. The script will only change the directory while it is running, false. Try to put that function in your ~/.bashrc file and you will discover that, while the function is in a script, it will be available despite the ~/.bashrc file is processed when starting the shell. – Poshi Jul 04 '18 at 13:54
  • 2
    Whatever you do to define your alias in the current shell will work to define a function in the current shell as well. – chepner Jul 04 '18 at 13:57
  • @chepner, I reckon you should take your two comments and make an answer out of them. (that should be accepted and upvoted) – talz Jul 04 '18 at 14:23

1 Answers1

2

Define a function to call the two commands instead. The argument to the function can be passed to the first command.

go_to () {
    command1 "$1"
    command2
}
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Knowing now the trick, this was also particularly helpful for me as a scripting beginner: https://superuser.com/questions/106272/how-to-call-bash-functions – Marcel Sad Jul 05 '18 at 09:27