【shell】循环将字符串写入数组中?

bash shell脚本语法怪异,其他语言循环写入数组或列表都很简单实现,或有相应函数来做。

以下用两种方法来实现:

方法一

c=0
for i in `ls ./Data_Analysis/Quantitation/*-VS-*.xls`;do
    pre=`echo $i |sed 's/.*\///'`                                                                                                   
    filelist[$c]="$pre"
    ((c++))
done

echo $c

for i in ${filelist[@]}
do
    sample=`echo $i | sed 's/.xls//'`
    echo $sample
done

方法二

     filelist=()  
      for i in `ls ./Data_Analysis/Quantitation/*-VS-*.xls`;do
          cp $i ./protein
          sample=`echo $i |sed 's#.*/##' |sed 's/.xls//'`
          filelist+=("$sample")
      done

     echo ${filelist[@]}

    for i in ${filelist[@]};do
        echo $i
    done

你可能感兴趣的:(【shell】循环将字符串写入数组中?)