使用shell-script,
如何从另一台服务器(user@192.168.10.x:/home/admin/data)读取文件并将其存储在"文件"数组中?(代码第四行)
1 reportTypes=(0001 0102 8902)
2
3 # collect all files matching expression into an array
4 files=(Rep_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-1].csv)
5
6 # take only the first hundred
7 files=( "${files[@]:0:100}" )
8
9 shopt -s nullglob # allow a glob to expand to zero arguments
10
11 # echo ${files[@]}
12
13 for i in ${reportTypes[@]}; do
14 printf -v val '%04d' "$i"
15 groupFiles=( $( for j in ${files[@]} ; do echo $j ; done | grep ${val} ) )
16
17 # Generate sequence file for EACH Report Type
18 forqlift create --file="Report${val}.seq" "${groupFiles[@]}"
19 done
编辑:
我尝试用:
替换第4行while IFS= read -rd '' file; do
files+=( "$file" )
done < <(ssh user@host "cd /home/admin/data && printf '%s ' Rep_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-1].csv")
但是,当我要生成hadoop序列文件(使用forqlift,第18行)时,它会失败。
ERROR forqlift.ui.Driver - java.io.FileNotFoundException: Rep_0001_20150102_0.csv (No such file or directory)
因此,现在重写了这个答案,以使问题的某些歧义已清除。
您想将文件复制,将模式匹配,从远程主机到本地主机,然后迭代它们。然后使用SCP(或Rsync)下载它们,然后在本地迭代它们。
# this copies the matching filenames from the remote host to the current dir. The quotes are important.
scp "user@host:/home/admin/data/Rep_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-1].csv" ./
# Now that the files are accessible locally, you may iterate them with a for-loop
for file in Rep_[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]_[0-1].csv; do
printf 'Do something with %sn' "$file"
done
好吧,现在我将答案更改为工作脚本:
declare -a ARRAY
count=0;
for F in $(ssh localhost find ./ -name "*.sh" | xargs basename -a) ; do
ARRAY[$count]=$F
((count++))
done
ELEMENTS=${#ARRAY[@]}
for (( i=0;i<$ELEMENTS;i++)); do
echo ${ARRAY[${i}]}
done
您要搜索的文件(在此脚本 *.sh中)将保存到名为Array的bash数组中。
这是测试的,并且可以工作。
user@user-virtual-machine:~$ ./filenames2array.sh
user@localhosts password:
deleteme1.sh
deleteme9.sh
deleteme2.sh
deleteme7.sh
deleteme8.sh
deleteme10.sh
deleteme4.sh
deleteme3.sh
deleteme5.sh
filenames2array.sh
deleteme6.sh
user@user-virtual-machine:~$
这是通过SSH编写的,以连接到Localhost。您可以将本地主机更改为所需的任何远程服务器。