在printf中添加循环

时间:2015-02-11 14:34:43

标签: bash loops text printf

我正在使用一些大脚本生成一些新的input.sh文件,该文件由一系列命令组成,这些命令用作某个程序的输入,此处称为pmemd。

在这里,我想集中讨论如何获得此input.sh。使用printf我有以下内容:

 printf "#setenv CUDA_VISIBLE_DEVICES "1"\nmpirun -np 8 pmemd.MPI -O -i ../in/minim.in -o minim.out -p ./protein.parm7 -c  ./protein.inpcrd -r minim.restrt\npmemd.cuda -O -i ../in/equil.in -o equil.out -p ./protein.parm7 -c  ./minim.restrt -r equil.restrt -x equil.nc -ref ./minim.restrt\npmemd.cuda -O -i ../in/equil2.in -o equil2.out -p ./protein.parm7 -c  equil.restrt -r equil2.restrt -x equil2.nc -ref equil.restrt\npmemd.cuda -O -i ../in/equil3.in -o equil3.out -p ./protein.parm7 -c  equil2.restrt -r equil3.restrt -x equil3.nc -ref equil2.restrt\npmemd.cuda -O -i ../in/equil4.in -o equil4.out -p ./protein.parm7 -c  equil3.restrt -r equil4.restrt -x equil4.nc -ref equil3.restrt\npmemd.cuda -O -i ../in/equil5.in -o equil5_1.out -p ./protein.parm7 -c  equil4.restrt -r equil5_1.restrt -x equil5.nc -ref equil4.restrt\npmemd.cuda -O -i ../in/equil5.in -o equil5_2.out -p ./protein.parm7 -c  equil5_1.restrt -r equil5_2.restrt -x equil5.nc -ref equil5_1.restrt\npmemd.cuda -O -i ../in/equil5.in -o equil5_3.out -p ./protein.parm7 -c  equil5_2.restrt -r equil5_3.restrt -x equil5.nc -ref equil5_2.restrt\npmemd.cuda -O -i ../in/equil5.in -o equil5_4.out -p ./protein.parm7 -c  equil5_3.restrt -r equil5_4.restrt -x equil5.nc -ref equil5_3.restrt\npmemd.cuda -O -i ../in/equil5.in -o equil5_5.out -p ./protein.parm7 -c  equil5_4.restrt -r equil5_5.restrt -x equil5.nc -ref equil5_4.restrt\npmemd.cuda -O -i ../in/md.in -o md_${P}_${com_n}.out -p ./protein.parm7 -c  equil5_5.restrt -r md.restrt -x md.nc" > ./input.sh

导致input.sh最终由

组成
#setenv CUDA_VISIBLE_DEVICES 1
mpirun -np 8 pmemd.MPI -O -i ../in/minim.in -o minim.out -p ./protein.parm7 -c  ./protein.inpcrd -r minim.restrt
pmemd.cuda -O -i ../in/equil.in -o equil.out -p ./protein.parm7 -c  ./minim.restrt -r equil.restrt -x equil.nc -ref ./minim.restrt
pmemd.cuda -O -i ../in/equil2.in -o equil2.out -p ./protein.parm7 -c  equil.restrt -r equil2.restrt -x equil2.nc -ref equil.restrt
pmemd.cuda -O -i ../in/equil3.in -o equil3.out -p ./protein.parm7 -c  equil2.restrt -r equil3.restrt -x equil3.nc -ref equil2.restrt
pmemd.cuda -O -i ../in/equil4.in -o equil4.out -p ./protein.parm7 -c  equil3.restrt -r equil4.restrt -x equil4.nc -ref equil3.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_1.out -p ./protein.parm7 -c  equil4.restrt -r equil5_1.restrt -x equil5.nc -ref equil4.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_2.out -p ./protein.parm7 -c  equil5_1.restrt -r equil5_2.restrt -x equil5.nc -ref equil5_1.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_3.out -p ./protein.parm7 -c  equil5_2.restrt -r equil5_3.restrt -x equil5.nc -ref equil5_2.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_4.out -p ./protein.parm7 -c  equil5_3.restrt -r equil5_4.restrt -x equil5.nc -ref equil5_3.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_5.out -p ./protein.parm7 -c  equil5_4.restrt -r equil5_5.restrt -x equil5.nc -ref equil5_4.restrt
pmemd.cuda -O -i ../in/md.in -o md_OR5P3_plus-carvone.out -p ./protein.parm7 -c  equil5_5.restrt -r md.restrt -x md.nc

你可以看到这个方法不太舒服,因为当我需要添加对未来input.sh的一些修改时,在初始脚本中编辑input.sh的容器并不容易。另外正如你注意到文件由片段组成,我需要通过一些循环获得它,因为它包含几个步骤,如5_1,5_2等(这里每个下一步使用前一个作为输入)

pmemd.cuda -O -i ../in/equil5.in -o equil5_1.out -p ./protein.parm7 -c  equil4.restrt -r equil5_1.restrt -x equil5.nc -ref equil4.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_2.out -p ./protein.parm7 -c  equil5_1.restrt -r equil5_2.restrt -x equil5.nc -ref equil5_1.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_3.out -p ./protein.parm7 -c  equil5_2.restrt -r equil5_3.restrt -x equil5.nc -ref equil5_2.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_4.out -p ./protein.parm7 -c  equil5_3.restrt -r equil5_4.restrt -x equil5.nc -ref equil5_3.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_5.out -p ./protein.parm7 -c  equil5_4.restrt -r equil5_5.restrt -x equil5.nc -ref equil5_4.restrt

所以我感谢有人向我提供了如何在这样的printf运算符或其替代方案中添加For或While循环的想法。

感谢您的帮助!!

格列勃

2 个答案:

答案 0 :(得分:2)

这是一个shell脚本,可以生成帖子中的大部分行:

prev=minim
for i in '' $(seq 2 4); do
  cur=equil$i
  printf 'pmemd.cuda -O -i ../in/%s.in -o %s.out -p ./protein.parm7 -c %s.restrt -r %s.restrt -x %s.nc -ref %s.restrt\n' $cur $cur $prev $cur $cur $prev
  prev=$cur
done
for i in $(seq 5); do
  cur=equil5_$i
  printf 'pmemd.cuda -O -i ../in/equil5.in -o %s.out -p ./protein.parm7 -c %s.restrt -r %s.restrt -x equil5.nc -ref %s.restrt\n' $cur $prev $cur $prev
  prev=$cur
done

这会产生:

pmemd.cuda -O -i ../in/equil.in -o equil.out -p ./protein.parm7 -c minim.restrt -r equil.restrt -x equil.nc -ref minim.restrt
pmemd.cuda -O -i ../in/equil2.in -o equil2.out -p ./protein.parm7 -c equil.restrt -r equil2.restrt -x equil2.nc -ref equil.restrt
pmemd.cuda -O -i ../in/equil3.in -o equil3.out -p ./protein.parm7 -c equil2.restrt -r equil3.restrt -x equil3.nc -ref equil2.restrt
pmemd.cuda -O -i ../in/equil4.in -o equil4.out -p ./protein.parm7 -c equil3.restrt -r equil4.restrt -x equil4.nc -ref equil3.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_1.out -p ./protein.parm7 -c equil4.restrt -r equil5_1.restrt -x equil5.nc -ref equil4.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_2.out -p ./protein.parm7 -c equil5_1.restrt -r equil5_2.restrt -x equil5.nc -ref equil5_1.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_3.out -p ./protein.parm7 -c equil5_2.restrt -r equil5_3.restrt -x equil5.nc -ref equil5_2.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_4.out -p ./protein.parm7 -c equil5_3.restrt -r equil5_4.restrt -x equil5.nc -ref equil5_3.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_5.out -p ./protein.parm7 -c equil5_4.restrt -r equil5_5.restrt -x equil5.nc -ref equil5_4.restrt

答案 1 :(得分:2)

我可能会误解你的问题,但似乎你对这样一个巨大的printf行感到不舒服。所以,为什么不把它分开呢。您可以使用>> 追加到文件中。这样,input.sh中的每一行都是单独的printf行:

printf "#setenv CUDA_VISIBLE_DEVICES \"1\"\n" > input.sh
printf "mpirun -np 8 pmemd.MPI -O -i ../in/minim.in -o minim.out -p ./protein.parm7 -c  ./protein.inpcrd -r minim.restrt\n" >> input.sh
printf "pmemd.cuda -O -i ../in/equil.in -o equil.out -p ./protein.parm7 -c  ./minim.restrt -r equil.restrt -x equil.nc -ref ./minim.restrt\n" >> input.sh
printf "pmemd.cuda -O -i ../in/equil2.in -o equil2.out -p ./protein.parm7 -c  equil.restrt -r equil2.restrt -x equil2.nc -ref equil.restrt\n" >> input.sh
printf "pmemd.cuda -O -i ../in/equil3.in -o equil3.out -p ./protein.parm7 -c  equil2.restrt -r equil3.restrt -x equil3.nc -ref equil2.restrt\n" >> input.sh
printf "pmemd.cuda -O -i ../in/equil4.in -o equil4.out -p ./protein.parm7 -c  equil3.restrt -r equil4.restrt -x equil4.nc -ref equil3.restrt\n" >> input.sh
printf "pmemd.cuda -O -i ../in/equil5.in -o equil5_1.out -p ./protein.parm7 -c  equil4.restrt -r equil5_1.restrt -x equil5.nc -ref equil4.restrt\n" >> input.sh
printf "pmemd.cuda -O -i ../in/equil5.in -o equil5_2.out -p ./protein.parm7 -c  equil5_1.restrt -r equil5_2.restrt -x equil5.nc -ref equil5_1.restrt\n >> input.sh
printf "pmemd.cuda -O -i ../in/equil5.in -o equil5_3.out -p ./protein.parm7 -c  equil5_2.restrt -r equil5_3.restrt -x equil5.nc -ref equil5_2.restrt\n" >> input.sh
printf "pmemd.cuda -O -i ../in/equil5.in -o equil5_4.out -p ./protein.parm7 -c  equil5_3.restrt -r equil5_4.restrt -x equil5.nc -ref equil5_3.restrt\n" >> input.sh
printf "pmemd.cuda -O -i ../in/equil5.in -o equil5_5.out -p ./protein.parm7 -c  equil5_4.restrt -r equil5_5.restrt -x equil5.nc -ref equil5_4.restrt\n" >> input.sh
printf "pmemd.cuda -O -i ../in/md.in -o md_${P}_${com_n}.out -p ./protein.parm7 -c  equil5_5.restrt -r md.restrt -x md.nc\n" >> ./input.sh

由于printf最终不会自动包含NL,因此您甚至可以将单个input.sh行分开以获得更好的可读性:

printf " pmemd.cuda -O"         >> input.sh
printf " -i ../in/equil.in"     >> input.sh
printf " -o equil.out"          >> input.sh
printf " -p ./protein.parm7"    >> input.sh
printf " -c ./minim.restrt"     >> input.sh 
printf " -r equil.restrt"       >> input.sh
printf " -x equil.nc"           >> input.sh
printf " -ref ./minim.restrt"   >> input.sh
printf "\n"                     >> input.sh

现在,如果更改了某些内容,您可以快速找到该参数并进行修改。

您还可以将其与Chris Jester-Young建议使用for循环来增加您的值相结合:

for cur in '' $(seq 2 4)
do
    printf ' pmemd.cuda -O '                >> input.sh
    printf ' -i ../in/equil%s.in '    $cur  >> input.sh
    printf ' -o equil%s.out'          $cur  >> input.sh
    printf ' -p ./protein.parm7"            >> input.sh
    printf ' -c equil%s.restrt'       $prev >> input.sh
    prinff ' -r equil%s.restrt'       $cur  >> input.sh
    printf ' -x equil%s.nc'           $cur  >> input.sh
    printf ' -ref equil%s.restrt\n'   $prev >> input.sh
    prev=$cur
done

请注意每行的间距如何使您更容易阅读和理解命令。

最后,如果您不介意写出整个文件,可以使用 here 文档。

<<启动 here 文档。 EOF是在结束时告诉 here 文档的参数。这可能是任何事情。例如,它可能是INPUT_SH。如果我将该参数放在单引号中,shell将不会插入变量。

cat <<'EOF' > input.sh
#setenv CUDA_VISIBLE_DEVICES "1"
mpirun -np 8 pmemd.MPI -O -i ../in/minim.in -o minim.out -p ./protein.parm7 -c  ./protein.inpcrd -r minim.restrt
pmemd.cuda -O -i ../in/equil.in -o equil.out -p ./protein.parm7 -c  ./minim.restrt -r equil.restrt -x equil.nc -ref ./minim.restrt
pmemd.cuda -O -i ../in/equil2.in -o equil2.out -p ./protein.parm7 -c  equil.restrt -r equil2.restrt -x equil2.nc -ref equil.restrt
pmemd.cuda -O -i ../in/equil3.in -o equil3.out -p ./protein.parm7 -c  equil2.restrt -r equil3.restrt -x equil3.nc -ref equil2.restrt
pmemd.cuda -O -i ../in/equil4.in -o equil4.out -p ./protein.parm7 -c  equil3.restrt -r equil4.restrt -x equil4.nc -ref equil3.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_1.out -p ./protein.parm7 -c  equil4.restrt -r equil5_1.restrt -x equil5.nc -ref equil4.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_2.out -p ./protein.parm7 -c  equil5_1.restrt -r equil5_2.restrt -x equil5.nc -ref equil5_1.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_3.out -p ./protein.parm7 -c  equil5_2.restrt -r equil5_3.restrt -x equil5.nc -ref equil5_2.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_4.out -p ./protein.parm7 -c  equil5_3.restrt -r equil5_4.restrt -x equil5.nc -ref equil5_3.restrt
pmemd.cuda -O -i ../in/equil5.in -o equil5_5.out -p ./protein.parm7 -c  equil5_4.restrt -r equil5_5.restrt -x equil5.nc -ref equil5_4.restrt
pmemd.cuda -O -i ../in/md.in -o md_${P}_${com_n}.out -p ./protein.parm7 -c  equil5_5.restrt -r md.restrt -x md.nc
EOF