Find the answer to your Linux question:
Results 1 to 3 of 3
I have a script which uses a function and also uses expect. If I have #!/bin/bash as the first line, the function is recognised. The expect calls are not, failing ...
  1. #1
    Just Joined!
    Join Date
    Feb 2010
    Posts
    10

    Bash script with expect - headers

    I have a script which uses a function and also uses expect.

    If I have #!/bin/bash as the first line, the function is recognised. The expect calls are not, failing first with spawn.

    If I have #!/bin/expect as the first line, the expect and spawn work fine. The function is not recognised.

    My friend Google brings up examples with both these lines one after another in script files, just like Im doing. Except for me it doesnt appear to work.

    Where am I making my beginners mistake in not understanding how this works ?

  2. #2
    Just Joined!
    Join Date
    Jan 2011
    Location
    Fairfax, Virginia, USA
    Posts
    94
    Hi, I think I can offer some insight into what your seeing.

    In UNIX, when you try to run something, the kernel sees if it has a "magic number". You can see these magic numbers in /usr/share/misc/magic and there is a command called file(1) that uses this file that you can play with. The cool part comes next ... if there aren't magic numbers, it looks at the first and second bytes of the file your trying to run for "#!". If it sees that pattern, then it expects an interpreter path to follow. Bash and expect are two interpreters.

    A cool example from the old UNIX Power Tools book was a script that erases itself by invoking rm(1):
    Code:
    #!/bin/rm
    
    This file will be removed if you run it ... all this text here is ignored
    To summarize, when you start your scripts with #!/bin/bash, then a bash interpreter (in non interactive mode) is started. When you issue expect commands to the bash interpreter, it doesn't know how to handle them.

  3. #3
    Just Joined!
    Join Date
    Feb 2010
    Posts
    10
    Thanks for the reply Brian. OK, that makes sense now, and has lead me to the solution of coding the expect statements as

    /usr/bin/expect <<EOD
    ...
    ...
    EOD

    So thankyou for the hint which lead me to the answer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...