我正在尝试检查主机上的 filename 中是否存在特定行。因此,只有从 grep 命令中找到特定模式时,才会创建 dummy.txt。然后我想检查 dummy.txt 是否存在作为条件并继续。
但即使创建了 dummy.txt,if 语句也无法找到它:
if {[file exists $fileName] }
并且控制权被转移到 else 块。可能是什么原因?有没有更简单的方法来实现我想要的?
set Host [lindex $argv 0 ]
spawn ssh -o StrictHostKeyChecking=no userid@$Host
expect "*assword*"
send "password\n"
expect "+"
send "cd path/that/contains/filename\n"
expect "*$*"
send "pwd\n"
set fileName dummy.txt
send {grep '\[abc\]' filename | wc -l | { read line; [ "$line" != "0" ] && echo "$line"> dummy.txt; }}
send "\n"
expect "+"
send {grep 'xyz' filename | wc -l | { read line; [ "$line" != "0" ] && echo "$line"> dummy.txt; }}
send "\n"
expect "+"
send "chmod 777 dummy.txt\n"
if {[file exists $fileName] } {
send {printf "\nabc xyz already exists"}
send "\n"
expect "+"
send "ls -l\n"
expect "+"
send "rm dummy.txt\n"
expect "+"
send "ls -l\n"
expect "+"
send "exit\n"
} else {
send {printf "cannot find $fileName"}
send "\n"
expect "+"
send {printf "\n[abc]\nxyz\n" >> filename}
send "\n"
expect "+"
send "cat filename\n"
expect "+"
send "exit\n"
}
答案1
if {[file exists $fileName] }
将检查文件是否存在于本地系统(正在运行的系统)上,但如果存在,则expect
有条件创建的文件将位于远程主机(脚本使用 进行登录的主机)上。此外,是,但有条件创建的文件是。ssh
filename
dummy
dummy.txt