Why is my expect script failing on line 1? [closed]
Asked Answered
S

5

23

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.

Steelmaker answered 22/12, 2010 at 2:33 Comment(3)
if you delete everything but the shebang line does it still fail with the same error?Libertine
Yes. I reduce it to the one line and it fails.Steelmaker
I copied this from another server and the other server is able to execute this script flawlessly. Both are running Ubuntu Hardy.Steelmaker
D
26

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.

Debera answered 22/12, 2010 at 2:55 Comment(1)
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.Steelmaker
R
29

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
Revolting answered 18/11, 2016 at 21:48 Comment(1)
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.Habitforming
D
26

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.

Debera answered 22/12, 2010 at 2:55 Comment(1)
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.Steelmaker
L
11

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

Lachesis answered 22/12, 2010 at 2:35 Comment(0)
S
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.

Saddlecloth answered 22/12, 2010 at 3:0 Comment(0)
E
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.

Eckblad answered 1/12, 2020 at 11:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.