Kalai
Kalai

Reputation: 319

File existence in expect commands

I am new in using 'expect' commands, my requirement is to create an expect script to check if a file exists in a specific location.

The script i am using is

#!/usr/bin/expect 
set PATH .ssh
set fname id_rsa
set timeout 2
spawn -noecho ls $PATH
expect { "id_rsa" { puts "found" }
timeout { puts "not found" } }

But this script not working. Is there any approach available for this requirement like File.Exists ($fname) ? I need to decide next action based on the file existence. like if file exits, then do nothing, else create a rsa key pair, but stuck at this point.

Any help would be appreciated.

Thanks

Upvotes: 0

Views: 2114

Answers (2)

Rahul R Dhobi
Rahul R Dhobi

Reputation: 5806

You can use if condition in bash script for checking file is present or not.

if [ -e $PATH/id_rsa ]; then
   echo "found"
else
   echo "not found"
fi

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246764

In Tcl/expect:

if {[file exists [file join $PATH id_rsa]]} {
    puts found
} else {
    puts "not found"
}

Also, your PATH variable is dependant on your current directory. You should use

set PATH [file join $env(HOME) .ssh]

Also, when shell scripting, never use the variable PATH -- you won't be able to locate any programs if you redefine PATH. In general, don't use ALL_CAPS_VARNAMES, leave those for the system.

Upvotes: 1

Related Questions