22

The very first line of my expect script fails. Here are the entire contents of my script and it fails:

#!/usr/bin/expect -f

And it fails right off the bat with

": no such file or directory

as my response. Expect is in fact installed and is located in /usr/bin/ and I am running this from root. I have no extra spaces or lines before the # sign either. Of course there was more to the script originally but it fails way before it gets to the good stuff.

Dennis Day
  • 607
  • 2
  • 5
  • 13

5 Answers5

25

I had this issue and found I didn't have the expect interpreter installed! Oddly enough, if you ran the command in the shell it worked. However, through a shell script I got this error:

/usr/bin/expect: bad interpreter: No such file or directory

I fixed it by simply installing the Expect interpreter. The package name that was chosen was: expect libtcl8.6

Just run:

sudo apt-get install expect
Cadoiz
  • 1,446
  • 21
  • 31
G_Style
  • 588
  • 8
  • 16
  • Unfortunately, I get `spawn: command not found` and `send: command not found`. I also tried to install expect with `sudo zypper install expect` (on OpenSuse), but it told me `'expect' is already installed.` – Cadoiz Oct 25 '21 at 11:04
24

Tried it and here is the result: /usr/bin/expect^M: bad interpreter

Is it possible that there's a Windows newline (the ^M) in there that's confusing the script? You can try od to see what newline character(s) is after the expect and tofromdos or an editor (e.g. emacs in hexl-mode) to remove it. See the man pages for more info.

Cadoiz
  • 1,446
  • 21
  • 31
Bert F
  • 85,407
  • 12
  • 106
  • 123
  • 2
    Ding! Ding! Ding! We have a winner. Even though I used nano to enter this, it saved the file in MSDOS mode. Started fresh and it seemed to fix the problem. – Dennis Day Dec 22 '10 at 02:59
10

Your line endings are wrong. Shove it through dos2unix or tr -d '\r'.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

I don't really know expect, to be honest, but when I run that on my system it "works" fine. Nothing happens, but that's what I'd expect. I don't get any error message. According to the man page,

#!/usr/bin/expect -f

is the correct way to start your script. Expect then slurps up the script you are executing as the cmdfile.

The way I got it to reproduce the problem was to actually put a ^M at the end of the line instead of a normal newline (saw Bert F's response and that prompted me to try it). I'm sure vim's :set list command will show any odd characters.

Cadoiz
  • 1,446
  • 21
  • 31
Glenn McAllister
  • 1,813
  • 17
  • 14
0

If you observe the error, there was a windows newline character, that is added because its copied from windows machine via mail or winscp. So to avoid this error copy the script using scp linux to linux and execute the script.

Cadoiz
  • 1,446
  • 21
  • 31
TRG
  • 1