【问题标题】:Expect script: How to archive and delete files from it?期望脚本:如何从中存档和删除文件?
【发布时间】:2017-06-21 12:37:00
【问题描述】:

我想使用纯期望脚本而不调用外部 shell 来执行 tar 和 rm 操作。我目前的结果是有效的,但使用外部 bash 脚本。

附:我从期望脚本执行 tar 或 rm 的所有尝试都失败了。

主脚本

#!/usr/bin/expect
# my scripts folder is here:
set myhome /home/scripts
set fullBpath /home/backup/SDH/
set date [exec date +%F_%k-%M]
# Some expect script commands was removed here.
exec $myhome/zip_clean.sh $fullBpath $date
send_user "Script terminated.\n"
exit

zip_clean.sh

#!/bin/bash
cd $1
tar cvzf OME6500-Backup-$2 *.cmp *.inf
rm *.cmp *.inf

【问题讨论】:

标签: bash file expect


【解决方案1】:

发现并测试了非常简单的方法:

system "cd $fullBpath; tar --remove-files -cvzf OME6500-Backup-$rundate.tgz *.cmp *.inf"

【讨论】:

    【解决方案2】:

    您可以使用命令file 和命令glob。还 对于使用日期,您可以使用clock:

    set date [clock format [clock seconds] -format %Y-%m-%d_%k-%M]
    cd $fullBpath
    exec tar cvzf OME6500-Backup-$date [glob *.cmp *.inf]
    file delete [glob *.cmp *.inf]
    

    【讨论】:

    • system "cd $fullBpath; tar cvzf OME6500-Backup-$rundate.tgz *.cmp *.inf; rm *.cmp *.inf"
    • 您的示例在 tar 执行中产生错误。存档创建为空,脚本终止。
    • glob 返回文件名列表,并且 tar 期望的不是列表,而是每个单独的元素。试试这个:set files [glob *.cmp *.inf]; set status [catch {exec tar cvzf $tarfile {*}$files} output]; puts $output; if {$status == 0} {file delete {*}$files} -- {*} 语法就地扩展列表(参见 Tcl 的 12 条语法规则中的 rule 5
    猜你喜欢
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多