bash 的初学者,我已经编写了一个小脚本,循环遍历所有 *.txt 文件并将 *.txt 作为 perl 脚本的输入进行处理。
单个文件作为输入
#!/bin/bash
set -e
for i in *.txt
do
SAMPLE=$(echo ${i} | sed "s/.txt//")
echo ${SAMPLE}.txt
time /home/sunn/data/softwares/evaluation/msa/pal2nal.v14/pal2nal.pl ${SAMPLE}.txt -output paml > ${SAMPLE}.paml.txt
done
运行 perl 脚本的实际命令(2 个文件作为输入)
pal2nal.pl OG0012884_out.fa OG0012884_out.txt -output paml > OG0012884_paml.txt
两个文件作为输入?我被击中了..
#!/bin/bash
set -e
for i in *.txt
do
SAMPLE=$(echo ${i} | sed "s/.txt//" | "s/.fa//")
echo ${SAMPLE}.txt
time /home/sunn/data/softwares/evaluation/msa/pal2nal.v14/pal2nal.pl ${SAMPLE}.fa ${SAMPLE}.txt -output paml > ${SAMPLE}.paml.txt
done
答案1
只需多次使用该变量即可。
您不需要调用sed
,参数扩展也可以删除扩展名。
for txt in *_out.txt ; do
sample=${txt%.txt}
out=${sample%_out}
pal2nal.pl "$sample".fa "$sample".txt -output paml > "$out"_paml.txt
done
您可以添加检查其他文件是否存在:
if [[ ! -e $sample.fa ]] ; then
echo "$sample.fa missing, skipped." >&2
continue
fi