Mike F Mike F - 4 months ago 7
Bash Question

creating bash script to automate task for analyzing multiple files

I don't have a lot of experience with scripting.

I have a directory that contains, among many other files, a set of

*.phylip
files I need to analyze with a program. I would like to automate this task. I think a loop bash shell script would be appropriate, although I could be wrong.

If I was to perform the analysis manually on one
.phylip
file, I would use the following command in terminal:

./raxmlHPC-SSE3 -m GTRCAT -y -s uce-5.phylip --print-identical-sequences -p 12345 -n uce-5_result


For the bash shell script, I think it would be close to:

#!/bin/sh
for i in $( ls ); do
./raxmlHPC-SSE3 -m GTRCAT -y -s uce-5.phylip --print-identical-sequences -p 12345 -n test_5 $i
done


The issue I'm aware of, but don't know how to fix, is the -s option, which specifies the input phylip file. Any suggestions on how to modify the script to do what I need done?

Answer

Try the below code:

#!/bin/bash
for i in *.phylip
do
    ./raxmlHPC-SSE3 -m GTRCAT -y -s "$i" --print-identical-sequences -p 12345 -n ${i%.phylip}_result
done

-s option will be passed $i which has the file name with .phylip extension in the current directory.

${i%.phylip}_result replaces the .phylip extension with _result which i guess is what you expect. (Ref: Parameter Substitution)

Comments