【发布时间】:2018-02-02 03:47:22
【问题描述】:
这是我之前关于从循环目录中自动收集所选文件的问题的发展。 这是我的脚本:
#!/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
}
是否可以在循环之外定义这个数组并在每次处理期间将 $file 附加到现有数组中?在脚本结束时,循环完成后,我想打印整个数组 $file 以及已收集的文件列表。
【问题讨论】:
-
你之前的问题呢?