2017-08-24 21 views
2

这是我之前关于从循环目录中自动收集选定文件的问题的开发。 这里我的脚本:在目录循环期间向阵列添加filles

#!/bin/bash 
    server=$(pwd) 
    results=${server}/results 
    output=${server}/simulations 

    date=$(date +"%m_%d_%Y") 

    for sim in "$output"/* ; do 
    if [[ -d $sim ]]; then 
     simulation=$(basename "$sim") 
     file=("${sim}"/gromacs*) 
     file_name=$(basename "${file}") 
     ((${#file[@]} == 1)) && [[ -e $file ]] || { 
     echo "ERROR: Exactly one file starting with 'gromacs' should exist" >&2 
     exit 1 
     } 
     (cd "${sim}" && exec cp $file "${results}/${simulation}.${date}") 
     echo "${file_name} from ${simulation} has been collected!" 
    fi 
    done 

它创建循环中的数组,并检查显示目录中的重复文件:

file=("${sim}"/gromacs*) 
      file_name=$(basename "${file}") 
      ((${#file[@]} == 1)) && [[ -e $file ]] || { 
      echo "ERROR: Exactly one file starting with 'gromacs' should exist" >&2 
      exit 1 
      } 

是否可以定义外循环这个数组,并追加$文件到每个处理过程中的现有数组?在循环结束时脚本结束时,我想用已收集文件的列表打印整个数组$文件。

谢谢!

+0

哪来你前面的问题? – 123

回答

0

是的,你可以做这样的:

#!/bin/bash 
declare -a my_array=() 

for sim in $output"/* ; do 
    ... 
    ... #do some conditions 
    my_array+=("$file") 
done