Score:0

bash script to loop through the folders and files

ps flag

I have folders naming ouput00035023031 to output000035023035. I have written the bash script to iterate through each folder and enter the folder. After entering the folder I want to run a program 'barycorr'. Which asks for three inputs. The inputs are filenames *cl.evt. The code is blelow :

for i in $(seq -f "%03g" 1 5); do
    cd output0003502303$i
    echo -e "sw00092413002xpcw3po_cl.evt 
    sw00092413002xpcw3po_cl_bary4.evt 
    sw00092413002sao.fits.gz" | barycorr ra=253.467570 dec=39.760169
    cd ..
done

I need to give first input the filenames sw*pcw3po_cl.evt, secondly the filename I want to save like sw*xpcw3po_cl_bary4.evt and third the path where sw*sao.fits.gz file is located. The * changes according to which folder I am in. How to write the script now to run this program inside the each folder.

au flag
Will there always be exactly one match to each of those patterns in each folder? That is, is it possible a folder might have `sw1pcw3po_cl.evt` and also `sw2pcw3po_cl.evt`?
unicorn avatar
ps flag
Yes, the `sw00035023032xpcw3po_cl.evt` changes according to output folder number like output00035023032 and aloing with this there might be file name `sw00035023032xwtw2po_cl.evt` i.e pcw3 may changed to 'wtw2'.
au flag
Is the first part always the same as the folder name? That is, are the files `<foldername>xpcw3po_cl.evt`, `<foldername>xpcw3po_cl_bary4.evt`, and `<foldername>sao.fits.gz`? Also, is it really necessary to `cd` into the folders, or would it work to pass paths (e.g. `<foldername>/<foldername>xpcw3po_cl.evt`) to the `barycorr` command? (The reason for the second question is that `cd` in scripts tends to be confusing, and can cause chain-reaction failures, so I prefer to avoid them.)
unicorn avatar
ps flag
Yes, we can give path . We can make list of this command by changing the numerical value only like `00035023030' to '00035023035'. : ` echo -e "/home/dinesh/Test/output00035023030/sw00035023030xwtw2po_cl.evt /home/dinesh/Test/output00035023030/sw00035023030xwtw2po_cl_bary.evt /home/dinesh/Test/00035023030/auxil/sw00035023030sao.fits.gz" | barycorr ra=253.467570 dec=39.760169 ` . but if one number is missed like say ..31 then it program throws error. I need to bypass the error too.
Score:0
au flag

If I understand right, you should be able to do something like this:

for i in $(seq -f "%03g" 1 5); do
    printf '%s\n' "output0003502303${i}/sw00092413${i}"{xpcw3po_cl.evt,xpcw3po_cl_bary4.evt,sao.fits.gz} |
        barycorr ra=253.467570 dec=39.760169
done

Explanation: the printf '%s\n' will print each of the following arguments (the %s part) followed by newlines (the \n) -- that is, each on a separate line. printf is much more controllable for things like this than echo.

As for those arguments, each one consists of the folder name and filename prefixes, and then the {xpcw3po_cl.evt,xpcw3po_cl_bary4.evt,sao.fits.gz} part expands to each of those suffixes. Essentially, it's a shorthand for:

"output0003502303${i}/sw00092413${i}"xpcw3po_cl.evt
"output0003502303${i}/sw00092413${i}"xpcw3po_cl_bary4.evt
"output0003502303${i}/sw00092413${i}"sao.fits.gz

Is this what's needed? If not, please clarify.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.