85

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lk121
  • 1,283
  • 2
  • 11
  • 10

6 Answers6

132

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
bartimar
  • 3,374
  • 3
  • 30
  • 51
10

A better way might be this:

lassign $argv arg1 arg2 arg3

However, your method should work as well. Check that arg1 is retrieved. For example, with send_user "arg1: $arg1\n".

spbnick
  • 5,025
  • 1
  • 17
  • 22
8
#!/usr/bin/expect
set username [lindex $argv 0]
set password [lindex $argv 1]
log_file -a "/tmp/expect.log"
set timeout 600
spawn /anyscript.sh
expect "username: " { send "$username\r" }
expect "password: " { send "$password\r" }
interact
doug65536
  • 6,562
  • 3
  • 43
  • 53
user128364
  • 4,533
  • 3
  • 20
  • 12
7

I like the answer provided with this guide.

It creates a parse argument process.

#process to parse command line arguments into OPTS array
proc parseargs {argc argv} {
    global OPTS
    foreach {key val} $argv {
        switch -exact -- $key {
            "-username"   { set OPTS(username)   $val }
            "-password"   { set OPTS(password)   $val }
        }
    }
}
parseargs $argc $argv
#print out parsed username and password arguments
puts -nonewline "username: $OPTS(username) password: $OPTS(password)"

The above is just a snippet. It's important to read through the guide in full and add sufficient user argument checks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Duffmannen
  • 136
  • 1
  • 5
-2

Note, sometimes argv 0 is the name of the script you are calling. So if you run it that way, argv 0 doesn't work.

For me I run

expect script.exp  password

That makes argv 1 = password and argv 0 = script.exp.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-4

Arguments with spaces are fine, assuming the argument you want is the first after the script name ($0 is script name, $1 is the first argument, etc.)

Make sure you use "$ARG", not $ARG as it will not include the whitespace, but break them up into individual arguments. Do this in your Bash script:

#!/bin/bash

ARG="$1"
echo WORD FROM BASH IS: "$ARG" #test for debugging

expect -d exp.expect "$ARG"

exit 0

Also, as the first answer states, use debug mode, (the -d flag). It will output your argv variables as Expect sees them and should show you what is going on.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eulerworks
  • 365
  • 3
  • 8
  • The expect script is missing – Timo Mar 27 '18 at 18:57
  • 1
    It isn’t, “missing” it works the same way for any expect script that would be called via bash. Though in this example it would need to be called, “exp.expect.” There was no need for a downvote, my answer explains how to handle what was in question. – eulerworks Aug 13 '18 at 14:08
  • 2
    ok, I `expect`ed the content of your script which references the parameters from outside as the others showed in their examples. – Timo Aug 15 '18 at 06:40