Help with shell script, syntax?

This is some code I have. Basically, it's a simple script to do a particular task 100 times on each of 100 events.

#!/bin/bash

EVENT=0

while [ $EVENT -lt 100 ]; do

COUNTER=0

while [ $COUNTER -lt 100 ]; do

SEED=$RANDOM

lalinference_mcmc --IFO [H1,L1,V1] --cache [LALLIGO,LALLIGO,LALVirgo] --PSDstart 1000000000 --PSDlength 256 --srate 2048 --seglen 8 --trigtime 1000000000 --Niter 2 --Neff 1000 --approx SpinTaylorT4threePointFivePN --inj /home/karla.guardado/public_html/injections/injection2.xml --event $EVENT --dataseed $SEED

let COUNTER=COUNTER+1

done

mv PTMCMC.output.* public_html/injections/BLANK

let EVENT=EVENT+1

done

What I need help with is that after I run 100 tasks on one event, I would to move all the files I created to a specific folder in the directory public_html/injections/BLANK. I am using BLANK as a place holder though for whT I actually want to do so I can explain it better. I want BLANK to be event1, event2, event3, so it updates with each event as the loop runs. So that eventually, I will have 100 files in event0, event1, ...., event99. But what is the syntax for doing this?

I am new to shell programming but I hope I made myself clear.

Thanks.

1 Answer

Relevance
  • Merc
    Lv 6
    7 years ago
    Favorite Answer

    replace the following line:

    mv PTMCMC.output.* public_html/injections/BLANK

    with:

    T=public_html/injections/event$EVENT

    mkdir -p $T

    mv PTMCMC.output.* $T

    (It'd be nicer to name that variable "TARGET" instead of "T". Yahoo doesn't let me write long lines here so I can't...)

    ==

    Alternatively:

    Right now you're cluttering your home directory with those PTMCMC.output.* files. Instead, you can generate them in the T folder right from the start. Add the following (instead of the above) before your long command:

    T=~/public_html/injections/event$EVENT

    mkdir -p $T

    cd $T

    ==

    BTW, you can replace the following construct:

    COUNTER=0

    while [ $COUNTER -lt 100 ]; do

     ...whatever...

     let COUNTER=COUNTER+1

    done

    with:

    for COUNTER in {1..99}; do

     ...whatever...

    done

    It looks neater. (If you're using an old verison of Bash, you'd have to do `seq 1 99` instead of {1..99}).

    ==

    Another tip:

    Right now the folders would be named event1, ..., event99. This is problematic because they don't sort well ("event10" comes before "event2" in the file manager). If you want them named event01, ..., event99 (which would fix the problem), replace the T=... line with:

    T=$(printf "public_html/injections/event%02d" $EVENT)

    ==

    A related tip:

    If you're using a relatively new version of Bash, compare the output of these commands:

    for i in {1..10}; do echo $i; done

    for i in {01..10}; do echo $i; done

    for i in {0001..0010}; do echo $i; done

    To support older Bash you'd do:

    for i in `seq -w 1 10`; do echo $i; done

    (But be careful if you want to use this zero-padding feature: Right now you do "lalinference_mcmc ... --event 2". You'd need to check whether this program recognizes "--event 02" as synonymous with "--event 2".)

    ==

    Good luck.

    • Login to reply the answers
Still have questions? Get your answers by asking now.