如何在预期中将文件名指定为当前日期

如何在预期中将文件名指定为当前日期

我在expect中写了一个脚本,如下所示。我想将日期作为变量提供给 cd 命令,但是当我将日期作为变量提供给命令时,它添加 '' 逗号,因此它显示错误,如下所示。如何去掉那些逗号?

#!/usr/bin/expect
#!/bin/bash

set DATE [exec date +%c]
set DATE2 [exec date +'%Y%m%d']


log_user 0
log_file -a /lch/portal/scripts/sftpcheck21/log/sftpcheck21.log
send_log "test ran on $DATE \n"
spawn sftp -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -o "Port=8022" [email protected]@sftapx21
expect "[email protected]@sftapx21's password:"
send "London@123\n"
expect "sftp>"
send "cd /PIMCOXXX_FDM/SwapClear/$DATE2\n"
expect "sftp>"
send "lcd /lch/portal/scripts/sftpcheck21\n"
expect "sftp>"
send "get 'P-PSWC-PIMCOXXX_FDM-$DATE2-233518_$DATE2_REP000F1d - Trade Level Pricing_ 1.TXT'\n"
expect "sftp>"
send "exit\n"
interact
log_file

输出如下

sftp> cd /PIMCOXXX_FDM/SwapClear/'20220130'
Couldn't canonicalize: No such file or director

答案1

单引号在expect或Tcl中没有特殊含义。通常情况下shell 会删除那些,但 Tcl 没有。

有一些内置方法可以获取预期的日期:

  1. 使用 Tclclock命令
    set DATE [clock format [clock seconds] -format %Y%m%d]
    
  2. 使用期望timestamp命令
    set DATE [timestamp -format %Y%m%d]
    

答案2

我按如下方式更新了脚本并解决了问题

#!/bin/bash

set DATE [exec date +%c]
#set DATE2 [exec date +'%Y%m%d']
#set DATE3 [ exec /bin/date -d "yesterday" +%Y%m%d]
#set DATE3 [ exec /bin/date -d "-4 day" +%Y%m%d]
set DATE3 [ exec /bin/date -d "today"  +%Y%m%d]

log_user 0
log_file -a /lch/portal/scripts/sftpcheck21/log/sftpcheck21.log
send_log "test ran on $DATE \n"
spawn sftp -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -o "Port=8022" [email protected]@sftapx21
expect "[email protected]@sftapx21's password:"
send "London@123\n"
expect "sftp>"
send "cd /PIMCOXXX_FDM/SwapClear/$DATE3\n"
expect "sftp>"
send "lcd /lch/portal/scripts/sftpcheck21\n"
expect "sftp>"
send "get 'P-PSWC-PIMCOXXX_FDM-$DATE3-233518_$DATE3\_REP000F1d - Trade Level Pricing_ 1.TXT'\n"
expect "sftp>"
send "exit\n"
interact
log_file

send "get 'P-PSWC-PIMCOXXX_FDM-$DATE3-233518_$DATE3\_REP000F1d - Trade Level Pricing_ 1.TXT'\n在使用 \ 作为变量 DATE3 的分隔符的行中

相关内容