Is Recursive Grep Really Better?; How to Improve PBS-based Bash Script?; and other Questions
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?
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.
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.
Try without the quotes:
dump=$(qstat -f $(qstat))
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
.
精彩评论