Specifying a generic interpreter for a program like expect?

7,318

Solution 1

One trick that mostly works (for perl, python, php interpreters, and probably others):

#!/usr/bin/env expect

I think env is always in /usr/bin/. A lot of interpreters can run that way now. Other hacks used to exist, but weren't understandable, or weren't all that portable.

Solution 2

I suggest

#!/bin/sh
expect_path="$(which expect)"
"$expect_path" "$0" "$@"

I hope this works; I am not familiar with the differences between bash and sh.

Share:
7,318

Related videos on Youtube

Ezequiel
Author by

Ezequiel

Updated on September 18, 2022

Comments

  • Ezequiel
    Ezequiel over 1 year

    I am writing expect script which can be used on mulple operating systems but the problem is

    i can't use #!/usr/bin/expect evreywhere so instead i tried to do

     #!`which expect`
    

    at the top but it failed

    [sesiv@itseelm-lx4151 ~]$ ./sendcommand
    -bash: ./sendcommand: `which: bad interpreter: No such file or directory
    

    any solution for this.

    • Hauke Laging
      Hauke Laging about 11 years
      Is there anything you can use everywhere, /bin/bash e.g.?
    • Ezequiel
      Ezequiel about 11 years
      i am not sure about this but, i think most standard OS will have /bin/sh say AIX,solaris,linux,redhat,suse,HP
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' about 11 years
  • Jpark822
    Jpark822 about 11 years
    This is also the portable way to run bash (#!/usr/bin/env bash, which will work even when your bash is in /usr/bin/local/bash or elsewhere).
  • Keith
    Keith about 11 years
    You can also expand on this by putting a check that it was actually found, and using exec before the path.