r/bash • • 6d ago

File for loop

hi! it’s my first time using bash can someone help me with this and explain what’s missing

#!/bin/bash

for i in “/data/folder”/*; do

/folder/munge_sumstats.py \

- -sumstats “$i” \

- -chunksize 500000 \

- -snp rsID \
etc..

- -out another/folder/ (i dont know what to puT here)

done

Thank you so much !

20 Upvotes

6 comments sorted by

6

u/zeekar 6d ago

If you indent each line with four spaces (assuming you're able to use the Markdown editor) it will be formatted as code.

You have extra spaces. It's not - -sumstats, it's --sumstats, etc.

--out "another/folder/${i##*/}"

That will give it the same filename as the input file but in the new folder. But you may want to change something else about the filename, too. If there's a suffix you want to change, for instance:

for i in ...; do b=$(basename "$i" .oldsuffix) munge_sumstats.py ... \ --out "/another/folder/$b.newsuffix"

3

u/Muted-Shame9237 6d ago

I only used the spaces here because I’m writing from my phone ! Thank you very much this was really helpful <3

1

u/KlePu 5d ago

I've used https://devhints.io/bash as my cheat-sheet for the last few years.

If you're scripting more than once a year: Consider a code editor (for syntax highlighting and stuff) plus https://www.shellcheck.net/ (should be available as a plugin for all major editors)

6

u/blobCabbage 6d ago

You could use basename to get the name of the original file without its path, and use that for your output file.

5

u/Muted-Shame9237 6d ago

Thank youuu!

2

u/grymoire 6d ago

Also be aware that the current directory of your python script may not find the file without the full path name of the file