2013-08-07 29 views
0

我是bash脚本编写的新手,但我想将某些文件设置为bash脚本中for循环的变量。我有这样的代码:如何在bash中将文件设置为变量

a=home/my_directory/*.fasta 
b=home/my_directory/*.aln 

for i in {1..14} # I have 14 files in my_directory with file extension .fasta 
do 
clustalo -i $a -o $b # clustalo is a command of Clustal Omega software, -i is 
         # input file, -o is output file 
done 

我只想在my_directory中使用我所有的fasta文件,并创建另外14个新的aln文件。但有了这个代码,它不起作用,因为Clustal程序不能识别这个集合文件。所以,如果你能帮助,我会非常感激。

回答

1

如果你知道恰好有14个文件,这样做:

for i in {1..14}; do 
    clustalo -i home/my_directory/$a.fasta -o home/my_directory/$b.aln 
done 

如果要处理的所有*.fasta文件,然而,许多有,然后执行:

for file in home/my_directory/*.fasta; do 
    clustalo -i "$file" -o "${file%.fasta}.aln" 
done 

要明白这一点,${file%.fasta}给我们$file.fasta扩展剥离。

如果要首先将文件名存储在变量中,最好的方法是使用数组变量。通过在变量赋值处添加圆括号,然后使用奇怪的语法"${array[@]}"来访问数组值。

files=(home/my_directory/*.fasta) 

for file in "${files[@]}"; do 
    clustalo -i "$file" -o "${file%.fasta}.aln" 
done 
+0

是否有可能将这些新文件保存到新目录? – user1997808

相关问题