在echo bash-script中写一个循环

时间:2022-09-30 16:21:18

I used bash script to generate input files for a program. The bash script generates the list of random numbers followed by the writing the input file. My bash script has two sections where first section generates the list of random numbers and second section use a random number to build the output file contents.

我使用bash脚本为程序生成输入文件。 bash脚本生成随机数列表,然后写入输入文件。我的bash脚本有两个部分,第一部分生成随机数列表,第二部分使用随机数生成输出文件内容。

   # 1- Generate Random Numbers
    for i in {1..200}
    do
    ARRAY+=($((RANDOM%885+0)))
    done
    echo "
    ${ARRAY[*]}
    "> random_numbers.txt
# 2- construction of output file    
echo "
# Packmol input file
filetype pdb
output peg_$(($npeg)).pdb
# selecting the structure according random number
structure peg${ARRAY[0]}.pdb
  number 1
  inside cube -$d. -$d. -$d. $d.
end structure
structure peg${ARRAY[1]}.pdb
  number 1
  inside cube -$d. -$d. -$d. $d.
end structure
structure peg${ARRAY[2]}.pdb
  number 1
  inside cube -$d. -$d. -$d. $d.
end structure
structure peg${ARRAY[3]}.pdb
  number 1
  inside cube -$d. -$d. -$d. $d.
end structure
structure peg${ARRAY[4]}.pdb
  number 1
  inside cube -$d. -$d. -$d. $d.
end structure
continued *****
structure peg${ARRAY[24]}.pdb
  number 1
  inside cube -$d. -$d. -$d. $d.
end structure

"> output.inp

Is there a way if I can run a loop of different range (25, 50, 75 ...) over the repeating lines given below by changing the Array number?

如果我可以通过更改数组编号在下面给出的重复行上运行不同范围(25,50,75 ...)的循环,有没有办法?

structure peg${ARRAY[*]}.pdb
  number 1
  inside cube -$d. -$d. -$d. $d.
end structure

1 个解决方案

#1


{
  # write header
  printf '%s\n' '# Packmol input file' 'filetype pdb' "output peg_$((npeg)).pdb"

  # iterate over array contents
  for value in "${ARRAY[@]}"; do
    printf '%s\n' \
      "structure peg${value}" \
      '  number 1' \
      '  inside cube -$d. -$d. -$d. $d.' \
      'end structure'
  done
} >output.inp

...to take only every 25th item, modify the inner loop as follows:

...只取每25个项目,修改内循环如下:

  # iterate over array contents
  for ((i=0; i<${#ARRAY[@]}; i+=25)); do
    printf '%s\n' \
      "structure peg${ARRAY[$i]}" \
      '  number 1' \
      '  inside cube -$d. -$d. -$d. $d.' \
      'end structure'
  done

Points to note:

注意事项:

  • We're only doing a redirection once, for the whole block. Putting >>output.inp on a bunch of separate commands re-opens the file once each time, making it very inefficient.
  • 我们只对整个块进行一次重定向。将>> output.inp放在一堆单独的命令上每次重新打开一次文件,使其效率非常低。

  • {start..end..count} syntax is quite limit (cannot use variables), so C-style for loop syntax is strongly preferable.
  • {start..end..count}语法是非常有限的(不能使用变量),因此C语言for循环语法是非常可取的。

#1


{
  # write header
  printf '%s\n' '# Packmol input file' 'filetype pdb' "output peg_$((npeg)).pdb"

  # iterate over array contents
  for value in "${ARRAY[@]}"; do
    printf '%s\n' \
      "structure peg${value}" \
      '  number 1' \
      '  inside cube -$d. -$d. -$d. $d.' \
      'end structure'
  done
} >output.inp

...to take only every 25th item, modify the inner loop as follows:

...只取每25个项目,修改内循环如下:

  # iterate over array contents
  for ((i=0; i<${#ARRAY[@]}; i+=25)); do
    printf '%s\n' \
      "structure peg${ARRAY[$i]}" \
      '  number 1' \
      '  inside cube -$d. -$d. -$d. $d.' \
      'end structure'
  done

Points to note:

注意事项:

  • We're only doing a redirection once, for the whole block. Putting >>output.inp on a bunch of separate commands re-opens the file once each time, making it very inefficient.
  • 我们只对整个块进行一次重定向。将>> output.inp放在一堆单独的命令上每次重新打开一次文件,使其效率非常低。

  • {start..end..count} syntax is quite limit (cannot use variables), so C-style for loop syntax is strongly preferable.
  • {start..end..count}语法是非常有限的(不能使用变量),因此C语言for循环语法是非常可取的。