1

I have an expect script which checks if a file exists on a remote server and then tries to print ( using cat) it if it does

For some reason the expect script ignores these commands.

send "if {[file exists "./abc.test"]} {cat ./abc.txt\r}"
Sharjeel
  • 347
  • 4
  • 18

3 Answers3

1

I have never used Expect, but your code sample...

if {[file exists "./abc.test"]} {send "cat ./abc.txt\r"}

...seems to check for file existence on the machine expect runs on, not the remote one. So unless the script is being run on the remote server, it won't work.

user1686
  • 8,717
  • 25
  • 38
1

Here is what I did and it worked now

cat /some/path/abc.txt 2> /dev/null

This way, the file gets printed if it exists and does not throw an error if it doesn't.

Sharjeel
  • 347
  • 4
  • 18
0

Hm, you're probably doing things in a rather roundabout way.

First, please describe your context:

  • Exactly how (complete command line) do you invoke this script?
  • How does it access the remote server? There is no remote access in the script you provide.

Finally, what are you really trying to do? If you want to print a file on a remote system, it's probably easier to directly invoke ssh. Expect is meant for scripting programs which do not have a scripting language (such as simple ftp clients). It's needlessly complicated to use it with a command shell, which already has a powerful scripting language.

sleske
  • 9,851
  • 4
  • 33
  • 44
  • I use a bash script to invoke the expect script (for automated login ).I need to do some checks on a list of remote servers and the code snippet I mentioned above is one of those checks I have put in the expect script. – Sharjeel Aug 06 '09 at 08:29