views:

40

answers:

4

I work in a research group and we use the PBS queuing system. I'm no PBS master, but I wanted to script a search for if a job was running. To do this I first grab a string of all the jobs by using the results of a qstat call as my argument to qstat -f and then taking the detailed list of all jobs and searching it for the submitted file path. The current kludge stands as follows

dump=`qstat -f `qstat``
if grep -q \
          "/${compounds[$i]}/D0_${j}_z_$((k*30))/scripts/jobscript_minim" \
      <<<$dump; then
      echo "Minimize is running!"
fi

Suggestions for improvement?

Also, I've been told that $() is cleaner than ``. But when I try:

dump="$(qstat -f "$(qstat)")"

...my program fails. Why is this? Am I misunderstanding how to nest shell calls with $()?? Or is it something to do with how I'm passing the list of queue jobs from qstat to qstat -f? Should I be using awk or something to grab the jobs from the qstat command and then somehow pass them as args to qstat -f?

Also should I be using recursive grep? Some people tell me its "saner" but I'm not sure what that means. Is it more portable? Is it faster? Does it need less trips to the therapist?

What is the reason you should use it?

A: 

Try without the quotes:

dump=$(qstat -f $(qstat))
Amber
Didn't work... I get:'usage: qstat [-f] [-1] [-W site_specific] [ job_identifier... | destination... ]qstat [-a|-i|-r|-e] [-u user] [-n [-1]] [-s] [-G|-M] [-R] [job_id... | destination...]qstat -Q [-f [-1]] [-W site_specific] [ destination... ]qstat -q [-G|-M] [ destination... ]qstat -B [-f [-1]] [-W site_specific] [ server_name... ]For BC0/D0_1_z_30:Minimize failed on error'
Jason R. Mick
*Note before I was getting good results, so obviously there's some problem with that version of the statement. That's basically the same thing I tried (unsuccessfully) above, sans quotes...
Jason R. Mick
A: 

dump=`qstat -f `qstat`` is equivalent to dump=$(qstat -f )qstat$() which is equivalent to dump="$(qstat -f)qstat".

qstat -f "$(qstat)" calls qstat with two arguments: the option -f, and the output from qstat lumped together as a single word. dump="$(qstat -f "$(qstat)")" sets dump to the output of the outer qstat command.

qstat -f $(qstat) calls qstat with any number of arguments starting from 1, depending on the output from qstat: first the output of qstat is split into separate words at each whitespace sequence, then each word that looks like a glob pattern (i.e. contains *, ? or [) that matches at least one file is replaced by the list of matching file names. All these words and file names become individual arguments to the outer qstat.

Gilles
A: 

Alright... managed to come up with a clean solution...

search_dir="${compounds[${i}]}/D0_${j}_z_$[30*k]"
if [ ! -z "$(qstat -f $(qstat | grep -F jmick | awk '{print $1}')|\
      grep -F "$search_dir"|head -n 1)" ]
      then

...since the directory I'm searching for is kind of long I assign it to a variable. I run the inner command substitution to get only the jobs with my user name, then run the outer command substitution to print full details on those jobs and then grep through those details for my directory. In case it finds it early I included a head to try to short circuit the command.

The question of what's the point of recursive grep, though, still stands.

Jason R. Mick
A: 

A recursive grep will search multiple files in all the subdirectories. Without using recursion it will search a file or files only in the current (or specified) directory. I can't see how one would be any "saner" than the other. They each have their particular applications.

By the way, you should really split your questions into specific issues rather than posting them together - even if they have something in common. This site works better when you do it that way.

Dennis Williamson
Fair enough. I've used recursive grep before, but I recall reading elsewhere that the recursive grep was "saner" than the average grep and wondered what that meant. I guess it maybe was a misnomer?
Jason R. Mick