UNIX shell脚本中的嵌套表达式

时间:2015-07-24 14:35:03

标签: bash shell unix expect

我有一个使用scp下载文件的脚本,然后通过附加当前日期来保存文件。

我收到错误消息:

private  static IWebDriver GetGrid1()
{     
     foreach (var m in new ShellWindows().Cast<IWebDriver>().Where(m => m.Url.Length > 22 && m.Url.Substring(0, 23) == "https://grid.sample.com"))
            return m;
     throw new Exception("WebDriver:Please open the Grid website prior to pressing the button");       
 }

以下是代码:

local_computer:~ memo$ ./scp_auto_save.sh 
can't read "(date +%d-%m-%y)": no such variable
while executing
"spawn scp "root@ch1.local:/root/*.csv" ./ch1_$(date +%d-%m-%y).csv"
(file "./scp_auto_save.sh" line 4)

Q1: 如何使用scp命令正确表达$(日期+%d-%m-%y)?

Q2: 我有很多文件存储在20台远程机器中。如何scp每台机器?

例如:

#!/usr/bin/expect -f
# connect via scp - ch1
spawn scp "root@ch1.local:/root/*.csv" ./ch1_$(date +%d-%m-%y).csv
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "password\r" 
    } 
}
interact

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

set date [timestamp %Y-%m-%d]   ; # this is an expect-specific command
set date [clock format [clock seconds] -format %Y-%m-%d]   ; # this works too

set hosts {
    ch1.local
    ch2.local
    ...
}

foreach host $hosts {
    spawn scp "root@$host:/root/*.csv" ./${host}_$date.csv
    expect {
        -re ".*es.*o.*" {
            exp_send "yes\r"
            exp_continue
        }
        -re ".*sword.*" {
            exp_send "password\r" 
        } 
    }
    expect eof
}