How to find if a file exists in expect script

梦想与她 提交于 2019-12-11 02:28:44

问题


I have a statement inside my expect script like this

send "sed -i -e 's/$oldport/$newport/' backup.txt\r"
expect "$ "

However I wish to first check whether the file backup.txt exist and if it does, then edit it.

How do I acheive this?

Thanks


回答1:


Since expect is an extension of Tcl, all Tcl commands are available to use:

if {[file exists backup.txt]} {
    send "sed -i -e 's/$oldport/$newport/' backup.txt\r"
    expect "$ "
}



回答2:


Here is a quick approach by using ls

set file "backup.txt"
send "ls $file\r"
expect {
        "\r\n$file" {puts "\nfile exists"}
        "cannot access $file" {puts "\nfile not found"}
}

You can just replace puts "\nfile exists" with your statement.



来源:https://stackoverflow.com/questions/23909157/how-to-find-if-a-file-exists-in-expect-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!