Search code examples
linuxbashshellexpect

How to pass argument in Expect through the command line in a shell script


I am passing argument in Expect through the command line in a shell script.

I tried this

#!/usr/bin/expect -f
    
set arg1 [lindex $argv 0]
    
spawn lockdis -p
expect "password:" {send "$arg1\r"}
expect "password:" {send "$arg1\r"}
expect "$ "

But it's not working. How can I fix it?


Solution

  • If you want to read from arguments, you can achieve this simply by

    set username [lindex $argv 0];
    set password [lindex $argv 1];
    

    And print it

    send_user "$username $password"
    

    That script will print

    $ ./test.exp user1 pass1
    user1 pass1
    

    You can use Debug mode

    $ ./test.exp -d user1 pass1