我正在开展一个项目,我需要将文件从一台机器移动到另一台机器。所有机器都是Ubuntu机器,它运行的是Ubuntu 12.04。所以我想,我可以直接scp文件。
现在,下面是我的文本文件,它将由另一个程序生成,其中包含有关哪台机器具有哪些文件的详细信息。
{ dbx45.dc1.host.com=[0, 1024, 4, 1028],
dbx46.dc1.host.com=[1, 1025, 5, 1029],
dbx47.dc1.host.com=[2, 1026, 6, 1030],
dbx48.dc1.host.com=[3, 1027, 7, 1031] }
在上面的文本文件中,我有一组key = value
对组合,其中每个机器负责key is the client machine name
和value is the name of files
。例如,在上面的文件中,
dbx45.dc1.host.com is the client machine responsible for 0 file name, 1024 file name, 4 file name, 1028 file name.
dbx46.dc1.host.com is the client machine responsible for 1 file name, 1025 file name, 5 file name, 1029 file name.
dbx47.dc1.host.com is the client machine responsible for 2 file name, 1026 file name, 6 file name, 1030 file name.
dbx48.dc1.host.com is the client machine responsible for 3 file name, 1027 file name, 7 file name, 1031 file name.
现在所有文件都只驻留在这两台机器上,如下所述。这意味着,文件随机分布在这两台机器中,因此不像只有一台机器包含所有文件。
slc4b03.dc1.host.com
chd1b02.dc2.host.com
上述两台机器中的所有文件都位于此文件夹中 -
/bat/data/snapshot/
,文件名就像这样 -
`t1_weekly_0_200003_5.data` (this is for file 0)
`t1_weekly_1024_200003_5.data` (this is for file number 1024)
`t1_weekly_4_200003_5.data` (this is for file number 4)
and similarly for others as well.
现在的问题是
(/export/home/username/primary)
我可以从任何机器运行这个shell脚本,因为我们只是scp(-ing)它。
我不确定我们如何在shell脚本中执行此操作?有什么想法吗?
更新: -
文件编号位于文件名中。
此文件 - t1_weekly_0_200003_5.data
用于文件0.同样,此文件为 - t1_weekly_1024_200003_5.data
用于文件1024.
答案 0 :(得分:1)
部分答案:数据结构可以通过微小的修改读入Perl(Perl非常能够处理)。此代码获取显示的数据文件并将其转换为此代码打印的结构:
#!/usr/bin/env perl
use strict;
use warnings;
# Slurp input into $data
my $data;
{
local $/;
$data = <>;
}
print "data = <<$data>>\n";
# Convert to eval-able format
$data =~ s/{/ \$x = {/m;
$data =~ s/}/};/m;
$data =~ s/([^ ]+)=/"$1" => /gm;
print "data = <<$data>>\n";
# Eval it
my $x;
eval $data;
# Dump the data structure
foreach my $key (keys %{$x})
{
foreach my $val (@{$x->{$key}})
{
print "$key: $val\n";
}
}
示例运行:
data = <<{ dbx45.dc1.host.com=[0, 1024, 4, 1028],
dbx46.dc1.host.com=[1, 1025, 5, 1029],
dbx47.dc1.host.com=[2, 1026, 6, 1030],
dbx48.dc1.host.com=[3, 1027, 7, 1031] }
>>
data = << $x = { "dbx45.dc1.host.com" => [0, 1024, 4, 1028],
"dbx46.dc1.host.com" => [1, 1025, 5, 1029],
"dbx47.dc1.host.com" => [2, 1026, 6, 1030],
"dbx48.dc1.host.com" => [3, 1027, 7, 1031] };
>>
dbx46.dc1.host.com: 1
dbx46.dc1.host.com: 1025
dbx46.dc1.host.com: 5
dbx46.dc1.host.com: 1029
dbx47.dc1.host.com: 2
dbx47.dc1.host.com: 1026
dbx47.dc1.host.com: 6
dbx47.dc1.host.com: 1030
dbx48.dc1.host.com: 3
dbx48.dc1.host.com: 1027
dbx48.dc1.host.com: 7
dbx48.dc1.host.com: 1031
dbx45.dc1.host.com: 0
dbx45.dc1.host.com: 1024
dbx45.dc1.host.com: 4
dbx45.dc1.host.com: 1028
您需要考虑从那里处理数据,但至少您有一个结构,您可以从中确定在给定计算机上保存哪些文件。您可以轻松创建反向索引列表,每个文件保存在哪台机器上。
您可以(并且可能应该)在修改和评估数据之前添加一些数据验证,以最大程度地降低恶意用户将危险信息输入数据文件的可能性。 (许多程序从磁盘读取可执行代码(在一些办公产品中考虑宏)很容易被恶意代码欺骗。不要让你的程序成为其中之一!