Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16
Dear Linux/Bash guru, We are trying to write a concise script to make the print out file ,run.pbs.A, like the following: #!/bin/bash ibrun pmemd.MPI prod_v0_11.mdcrd ibrun pmemd.MPI prod_v0_11.mdcrd prod_v0_12.mdcrd ibrun ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24

    bash loop


    Dear Linux/Bash guru,

    We are trying to write a concise script to make the print out file ,run.pbs.A, like the following:

    #!/bin/bash

    ibrun pmemd.MPI prod_v0_11.mdcrd

    ibrun pmemd.MPI prod_v0_11.mdcrd prod_v0_12.mdcrd

    ibrun pmemd.MPI prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd

    ibrun pmemd.MPI prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd prod_v0_14.mdcrd

    ########################################

    Here is the script which doesn't meet the request

    #!/bin/bash

    for ((X=1; X<=10; X++))

    do

    cat << EOF >> run.pbs.A
    #!/bin/bash

    ibrun pmemd.MPI prod_v0_1${X}.mdcrd


    EOF

    done

    ###########
    The other way to finish this task is to write it manually, which might make the script quite lengthy

    cat << EOF >> run.pbs.A
    #!/bin/bash

    ibrun pmemd.MPI prod_v0_11.mdcrd

    ibrun pmemd.MPI prod_v0_11.mdcrd prod_v0_12.mdcrd

    ibrun pmemd.MPI prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd

    ibrun pmemd.MPI prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd prod_v0_14.mdcrd

    EOF
    ###########################

    Could any guru kindly offer some examples/guides for a concise way? Sorry for the beginner's ignorance, if any. Thanks!

    Henry

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    you need a basic variable where you store basic command line (ibrun pmemd.MPI) to which you'll add (concatenate) inside the for loop the incremented filename (prod_v0_1${x}.mdcrd).
    the for loop's output will then be redirected to the script you want to write.

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24

    bash incremental concatenate

    Hi Watael!

    I have tried this and it doesn't seem to work.

    #!/bin/bash

    for ((X=1; X<=10; X++))

    do

    cat << EOF >> run.pbs.A
    #!/bin/bash

    ibrun (pmemd.MPI prod_v0_1${X}.mdcrd)


    EOF

    done

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    Code:
    $ var="ibrun pmemd.MPI"
    $ for ((X=1; X<=10; X++))
    > do
    >    var="$var prod_v0_1${X}.mdcrd"
    >    echo "$var"
    > done >destFile

  5. #5
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24
    Dear Watael,

    It works perfectly! The "done > "filename"" is really new to me. I think this is the key. Thank you so much!

    Henry

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by psu4 View Post
    It works perfectly!
    Glad you got it sorted. You can mark this thread as Solved using the Thread Tools link at the top of the page.

  7. #7
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24
    One more question:

    If I try to make something like this:

    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

    I have tried this:

    Y="$X"
    $ var="ibrun ${Y}.dat pmemd.MPI"

    $ for ((X=1; X<=10; X++))
    do
    var="$var prod_v0_1${X}.mdcrd"
    echo "$var"
    > done >destFile

    Output:
    ibrun pmemd.MPI .dat prod_v0_11.mdcrd
    ibrun pmemd.MPI .dat prod_v0_11.mdcrd prod_v0_12.mdcrd
    ibrun pmemd.MPI .dat prod_v0_11.mdcrd prod_v0_12.mdcrd prod_v0_13.mdcrd


    ANd this



    $ for ((X=1; X<=10; X++))
    do
    $ var="ibrun ${Y}.dat pmemd.MPI"
    Y="$X"
    var="$var prod_v0_1${X}.mdcrd"
    echo "$var"
    > done >destFile

    output:

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

    None of these works. I have tried some more complicated stuffs, such as eval, which do not work and make tens of lines. (Therefore I didn't post them out to make the reply brief. ) Could any guru make some solution? Thanks!

  8. #8
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    Code:
    $ var="ibrun"
    $ for ((x=1; x<10; x++))
    > do
    >    var1="${X}.dat pemd.MPI"
    >    var2="prod_v0_1${x}.mdcrd"
    >    echo "$var $var1 $var2"
    > done
    ibrun 1.dat pemd.MPI prod_v0_11.mdcrd
    ibrun 2.dat pemd.MPI prod_v0_12.mdcrd
    ibrun 3.dat pemd.MPI prod_v0_13.mdcrd
    ibrun 4.dat pemd.MPI prod_v0_14.mdcrd
    ibrun 5.dat pemd.MPI prod_v0_15.mdcrd
    ibrun 6.dat pemd.MPI prod_v0_16.mdcrd
    ibrun 7.dat pemd.MPI prod_v0_17.mdcrd
    ibrun 8.dat pemd.MPI prod_v0_18.mdcrd
    ibrun 9.dat pemd.MPI prod_v0_19.mdcrd
    KISS (Keep It Simple Stupid)

  9. #9
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24
    Hi Watael,

    THanks for the reply. What if we try to make the following output?

    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

    Thanks.

  10. #10
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    oops!
    Code:
    var2="$var2 prod_v0_1${x}.mdcrd"

Page 1 of 2 1 2 LastLast

Posting Permissions

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