我的循环问题。我正在使用bash版本4.1.2。
我的代码如下:
### List of files to process from the File .txt defined before ###
g=`cat ${AA}.txt | wc -l`
### Number of time the loop has to make calculated with the number of using cores ###
let h=($g/$nproc)
### Loop using $h ###
for i in (( i = 1; i <= ${h}; i++ )); do
### Loop to asign every core a process ###
for j in (( j = 1; j <= ${nproc}; j++ )); do
command lines to process $file
done
### Wait for every started process to finish ###
wait
done
我得到的语法错误是:
/var/spool/torque/mom_priv/jobs/1796866.SC: line 57: syntax error near unexpected token `('
/var/spool/torque/mom_priv/jobs/1796866.SC: line 57: `for i in (( i = 1; i <= h; i++ )); do'
有没有办法为i和j编写循环以使其语法有效?感谢
答案 0 :(得分:2)
语法是(例如):
for (( i = 1; i <= $h; i++ )); do
虽然大多数人会这样写:
for (( i = 0; i < $h; i++ )); do
虽然我们是关于它的:
g=`cat ${AA}.txt | wc -l`
可能写得更好:
g=$(wc -l < "$AA.txt")
$( )
符号比`
我们保存子进程
始终引用包含文件名的变量,它们可能包含嵌入的空格。