shell脚本:读取远程文件 [英] Shell script: read files from remote

查看:1932
本文介绍了shell脚本:读取远程文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用shell脚本,

我如何从另一台服务器读取文件(user@192.168.10.x:/家庭/管理/数据),并将其存储在文件数组? (code的4号线)

  1 reportTypes =(0001 0102 8902)
2
3#收集匹配前pression所有文件到一个数组
4个文件=(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)

6#只取百强之首
7档=($ {文件[@]:0:100})
8
9禁用了javascript -s nullglob#允许水珠扩大到零参数
10
11#回声$ {文件[@]}
12
13我在$ {reportTypes [@]};做
14 printf的-v VAL'%04D'$ I
15 groupFiles =($(为$ j中{文件[@]};做回声$焦耳;完成| grep的$ {} VAL))
16
17#为每个报告类型序列文件
18 forqlift创建--file =报告$ {} VAL .SEQ$ {groupFiles [@]}
19 DONE


编辑:

我试着用替换4号线:

 而IFS =读-rd''文件;做
  文件+ =($文件)
完成< ≤(SSH用户@主机CD /家庭/管理/数据和放大器;&安培; 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行),它失败。

 错误forqlift.ui.Driver  -  java.io.FileNotFoundException:Rep_0001_20150102_0.csv(没有这样的文件或目录)


解决方案

所以,现在有些问题的含糊之处已被清除了重写这个答案。

您要复制的文件,匹配模式,从远程主机到本地主机,然后遍历它们。然后使用SCP(或rsync的),下载它们,然后遍历本地他们之后。

 #这个副本从远程主机当前目录的匹配文件名。该报价是很重要的。
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\" ./#既然文件是本地访问,你可以用一个for循环遍历他们
在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;做
    printf的做一些与%s \\ n$文件
DONE

Using shell-script,

How do I read files from another server (user@192.168.10.x:/home/admin/data) and store it in "files" array? (4th line of code)

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


EDIT:

I tried replacing line 4 with:

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")

but when I am to generate hadoop sequence file (using forqlift, line 18), it fails.

ERROR  forqlift.ui.Driver - java.io.FileNotFoundException: Rep_0001_20150102_0.csv (No such file or directory)

解决方案

So rewriting this answer now that some of the ambiguities of the question has been cleared up.

You want to copy files, matching a pattern, from a remote host to localhost, then iterate them. Then use scp (or rsync), to download them, then iterate them locally afterwards.

# 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

这篇关于shell脚本:读取远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆