0

I get the following error:

> echo "${$(qstat -a | grep kig):0:7}"
-bash: ${$(qstat -a | grep kig):0:7}: bad substitution

I'm trying to take the number before. of

> qstat -a | grep kig
1192530.perceus-     kigumen     lr_regul pbs.sh            27198     2     16    --  24:00:00 R 00:32:23

and use it as an argument to qdel in openPBS so that I can delete all process that I started with my login kigumen

so ideally, this should work:

qdel ${$(qstat -a | grep kig):0:7}

so far, only this works:

str=$(qstat -a | grep kig); qdel "${str:0:7}"

but I want a clean one-liner without a temporary variable.

kirill_igum
  • 3,953
  • 5
  • 47
  • 73

3 Answers3

1

The shell substring construct you're using (:0:7) only works on variables, not command substitution. If you want to do this in a single operation, you'll need to trim the string as part of the pipeline, something like one of these:

echo "$(qstat -a | grep kig | sed 's/[.].*//')"
echo "$(qstat -a | awk -F. '/kig/ {print $1}')"
echo "$(qstat -a | awk '/kig/ {print substr($0, 1, 7)}')"

(Note that the first two print everything before the first ".", while the last prints the first 7 characters.) I don't know that any of them are particularly cleaner, but they do it without a temp variable...

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • 1
    Not without storing it in a variable first. Shell substitutions aren't nestable like true string expressions, so you basically only get one "operation" per substitution. e.g. you can do a command substitution OR a substring of a variable OR a variable with a prefix pattern removed OR a variable with a suffix pattern removed... but only one of them at a time. – Gordon Davisson Aug 05 '13 at 22:56
1
qstat -u palle | cut -f 1 -d "." | xargs qdel

Kills all my jobs... normally I grep out the jobname(s) before cut'ing...

So I use a small script "idlist":

qstat -u palle | grep -E "*.in" | grep -E "$1" | cut -f 1 -d "." | xargs

To see all my "map_..." jobs:

idlist "map_*"

For killing all my "map_...." jobs:

idlist "map_*" | xargs qdel
pallevillesen
  • 735
  • 6
  • 14
0

yet another ways :

    foreach   m1 in $(qstat -a );do
           if [[ $m1 =~ kig ]];then
               m2=${m1%.kig}
               echo "kig found $m2 "
               break
           fi
    done
michael501
  • 1,452
  • 9
  • 18