0

I am new to linux and scripting. I am trying to execute the following but would like to see what is happening here, in the first line - #!/usr/bin/expect , do i need to install/download any libraries for this. I am guessing expect is a tool we can use, but probably need to install it first before using it, right? is spawn a inbuilt command ? what about sftp?

1. #!/usr/bin/expect
2. export PASSWORD="*******"
3. expect -c 'spawn sftp user@site.com; 
expect "*Password: ";
send "$env(PASSWORD)\r";
expect "sftp>";
send "get some_file.txt \r";

arve
  • 569
  • 2
  • 10
  • 27
  • 1
    Try to make your title unique to your _individual_ question. We have thousands of questions about expect and linux; the goal of the site is to build up a knowledge base where people can find their questions already asked-and-answered, so it's important for the title and text to make it clear how and why every individual one is distinct. – Charles Duffy Sep 29 '20 at 21:18
  • 1
    And yes, `/usr/bin/expect` is a 3rd-party tool that needs to be installed. It's not bash, or part of bash; an expect script needs to be written in expect syntax (which is based on TCL), not bash syntax. – Charles Duffy Sep 29 '20 at 21:19
  • That means that if you use a `#!/usr/bin/expect` shebang, you can't have `export ANYTHING="value"` or `expect -c '...'`, because those are _bash_ syntax. Whereas if you use `#!/usr/bin/env bash` or such, then you can use bash syntax at the top of your script, and call expect from bash later. – Charles Duffy Sep 29 '20 at 21:20
  • @CharlesDuffy - thank you very much. I am new to linux environment , so i apologize in advance for any redundant questions. I am confused in one thing, #!/usr/bin/expect this line says, there is a expect tool that is located in that path right? and you mentioned export ... and expect -c are bash syntax. so expect in the shebang and expect in this line -> expect -c , are not related in anyway right? also, so I should be able to do this without the third party tool expect? – arve Sep 29 '20 at 21:54
  • The shebang line specifies the interpreter to use to run your script. `#!/usr/bin/python` means it's a Python script, `#!/bin/sh` means it's a sh script, `#!/usr/bin/bash` means it's a bash script, `#!/usr/bin/expect` means it's an expect script, etc. What that means is that when you run the script, the OS starts python or sh or bash or expect and tells _that program_ to execute the script. – Charles Duffy Sep 29 '20 at 21:57
  • ...so, if you use `#!/usr/bin/expect`, then (1) `/usr/bin/expect` needs to exist, and (2) the script needs to be written in a format that `expect` understands. – Charles Duffy Sep 29 '20 at 21:58
  • 1
    Whereas when you run `expect -c '...'`, that's a _POSIX-family shell_ command telling _the shell used as an interpreter_ to start `expect`, and tell that copy of expect to run the commands in `...` as an expect script. So it only makes sense with `#!/bin/sh` or `#!/usr/bin/env bash` or so forth; if you used `#!/usr/bin/expect`, then `expect` has to be already running for your script to be running at all, so a command to start expect makes no sense. – Charles Duffy Sep 29 '20 at 21:59
  • @CharlesDuffy - thank you very much again. I will try it. if there any examples/samples you know of that might help me learn more on this, please send it my way. – arve Sep 29 '20 at 23:33
  • Expect uses the [Tcl language](http://www.tcl.tk/) which has different syntax from shell. You need to learn Tcl before you can write Expect scripts. If you are more comfortable with shell syntax you can try my [sexpect (Expect for Shells)](https://github.com/clarkwang/sexpect). – sexpect - Expect for Shells Sep 29 '20 at 23:35

0 Answers0