Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 16 of 16

Thread: bash loop

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11

    Dear Watael,

    One more question: if we wish to display only X=1,2,3,4,8,12,16,20,...4n (n>=2~100) as following, is there any easy way to do? The only way on top of my head is to write several separate loops, which becomes complicated when the number goes up.

    ibrun pmemd.MPI 1.dat prod_v0_11.mdcrd
    ibrun pmemd.MPI 2.dat prod_v0_11.mdcrd prod_v0_12.mdcrd
    ibrun pmemd.MPI 3.dat prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd
    ibrun pmemd.MPI 4.dat prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd prod_v0_14.mdcrd

    # jump to 4X (8,12,16,20....)

    ibrun pmemd.MPI 8.dat prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd prod_v0_14.mdcrd prod_v0_15.mdcrd prod_v0_16.mdcrd prod_v0_17.mdcrd prod_v0_18.mdcrd

    ibrun pmemd.MPI 12.dat prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd prod_v0_14.mdcrd prod_v0_15.mdcrd prod_v0_16.mdcrd prod_v0_17.mdcrd prod_v0_18.mdcrd prod_v0_19.mdcrd prod_v0_110.mdcrd prod_v0_111.mdcrd prod_v0_112.mdcrd

    the same thing for 16, 20, 24, ...4n (n>=2~100) etc.

    Thanks,
    Henry

  2. #12
    I don't understand what 2~100 is

    instead of a for loop you could use a while one including a condition

    for what I undestand:
    Code:
    while test ${i:-0} -le 100
    do
       test ${i:-0} -ge 4 && ((i+=4)) || ((i++))
       #do stuff with $i
    done

  3. #13
    Hi Watael,

    I was trying to give the loop a control so that it only prints out certain ones. For example, only print out when X=1,2,4,7

    ibrun pmemd.MPI 1.dat prod_v0_11.mdcrd
    ibrun pmemd.MPI 2.dat prod_v0_11.mdcrd prod_v0_12.mdcrd
    ibrun pmemd.MPI 4.dat prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd prod_v0_14.mdcrd
    ibrun pmemd.MPI 7.dat prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd prod_v0_14.mdcrd prod_v0_15.mdcrd prod_v0_16.mdcrd prod_v0_17.mdcrd


    I guess this is something I was looking for (how do you insert a code?)

    #!/bin/bash
    var="ibrun"
    for ((X=1; X<=20; X++))
    do
    var1="${X}.dat pemd.MPI"
    var2="$var2 prod_v0_1${X}.mdcrd"
    if [ "$X"== "1" ] || [ "$X"== "2" ] || [ "$X"== "4" ]||[ "$X"== "7" ]; then
    echo "$var $var1 $var2"
    fi
    done > testprint


    Unfortunately, an error "test: line 7: [: 20==: unary operator expected" keeps pop out in the above code. Could you kindly give me some guides? Thanks.

    Henry

  4. $spacer_open
    $spacer_close
  5. #14
    in the tool bar there is a # button to insert code tags.

    error "test: line 7: [: 20==: unary operator expected"
    you need to put more spaces between comands, variables, and operators.
    the equal sign is to test variables lexically, not arithmatically.

  6. #15
    Code:
    #!/bin/bash
    var="ibrun"
    for ((X=1; X<=20; X++))
    do
      var1="${X}.dat pemd.MPI"
      var2="$var2 prod_v0_1${X}.mdcrd"
      if [ "$X" = "1" ] || [ "$X" = "2" ] || [ "$X" = "4" ]||[ "$X" = "7" ]; then
        echo "$var $var1 $var2"
      fi
    done > testprint
    Thanks Watael! It works!

    Henry

  7. #16
    Hi Watael,

    Code:
    #!/bin/bash
    
    
    wd=`pwd`
    pre0='ohyo'
    var="ibrun"
    
    for i in 1 2 
    
    do
    
       for ((X=1; X<=3; X++))
       do
          let "time=X*250"
          #time=$(( X * 25 ))
          var1="MMPBSA.py.MPI -O -i ohla_${i}.in -o ${pre0}_${i}_${time}ps.dat -y"
          var2="$var2 ${pre0}_prod_v0_l${X}.mdcrd"
          if [ "$X" = "1" ] || [ "$X" = "2" ] || [ "$X" = "3" ]; then
          echo "$var $var1 $var2"
          fi
       done >> testprint2
    
    done
    SO the above code will mkae the following output

    Code:
    ibrun MMPBSA.py.MPI -O -i ohla_1.in -o ohyo_1_250ps.dat -y  ohyo_prod_v0_l1.mdcrd
    ibrun MMPBSA.py.MPI -O -i ohla_1.in -o ohyo_1_500ps.dat -y  ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd
    ibrun MMPBSA.py.MPI -O -i ohla_1.in -o ohyo_1_750ps.dat -y  ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd ohyo_prod_v0_l3.mdcrd
    ibrun MMPBSA.py.MPI -O -i ohla_2.in -o ohyo_2_250ps.dat -y  ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd ohyo_prod_v0_l3.mdcrd ohyo_prod_v0_l1.mdcrd
    ibrun MMPBSA.py.MPI -O -i ohla_2.in -o ohyo_2_500ps.dat -y  ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd ohyo_prod_v0_l3.mdcrd ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd
    ibrun MMPBSA.py.MPI -O -i ohla_2.in -o ohyo_2_750ps.dat -y  ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd ohyo_prod_v0_l3.mdcrd ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd ohyo_prod_v0_l3.mdcrd
    But we want the following

    Code:
    ibrun MMPBSA.py.MPI -O -i ohla_1.in -o ohyo_1_250ps.dat -y  ohyo_prod_v0_l1.mdcrd
    ibrun MMPBSA.py.MPI -O -i ohla_1.in -o ohyo_1_500ps.dat -y  ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd
    ibrun MMPBSA.py.MPI -O -i ohla_1.in -o ohyo_1_750ps.dat -y  ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd ohyo_prod_v0_l3.mdcrd
    ibrun MMPBSA.py.MPI -O -i ohla_2.in -o ohyo_2_250ps.dat -y  ohyo_prod_v0_l1.mdcrd
    ibrun MMPBSA.py.MPI -O -i ohla_2.in -o ohyo_2_500ps.dat -y  ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd 
    ibrun MMPBSA.py.MPI -O -i ohla_2.in -o ohyo_2_750ps.dat -y  ohyo_prod_v0_l1.mdcrd ohyo_prod_v0_l2.mdcrd ohyo_prod_v0_l3.mdcrd
    Could you give some hint how to achieve the above output? Thanks!

    Henry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •