简体   繁体   中英

Bash for loop syntax error with pipe

I'm trying this command

for x in qstat -u '*' | grep Eqw | awk {'print $1'}; do qmod -cj $x; done

and end up with this error:

-bash: syntax error near unexpected token `|'

I've tried wrapping my qstat command in various quotes and brackets to no avail, what am I doing wrong?

由于您似乎想遍历结果,因此将其更改为:

for x in `qstat -u '*' | grep Eqw | awk {'print $1'}`; do qmod -cj $x; done

An optimised version will be :

for x in $(qstat -u '*' | awk '/Eqw/{print $1}'); do qmod -cj "$x"; done
  • the backquote (`) is used in the old-style command substitution. The foo=$(command) syntax is recommended instead. Backslash handling inside $() is less surprising, and $() is easier to nest. See http://mywiki.wooledge.org/BashFAQ/082
  • awk can grep hitself

Use this sript instead:

for x in $(qstat -u * | awk '/Eqw/ {print $1}')
do
    qmod -cj $x
done

它应该是:

for x in $(qstat -u '*' | grep Eqw | awk '{print $1}'); do qmod -cj $x; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM