shell脚本:读取远程文件(Shell script: read files from remot

2019-10-23 01:39发布

使用shell脚本,

如何从另一台服务器读取文件(user@192.168.10.x:/首页/管理/数据),并将其存储在“文件”阵列? (第四行代码)

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\0' 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)

Answer 1:

所以,现在一些问题的模糊性已经清理重写这个答案。

要复制的文件,匹配的模式,从远程主机到本地主机,然后遍历它们。 然后使用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 %s\n' "$file"
done


Answer 2:

好了,现在我我的答案更改为工作脚本:

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)的文件会被保存到指定数组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到本地主机。 你可以改变任何你想要的远程服务器的本地主机。



文章来源: Shell script: read files from remote