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?

0

6 Answers 6

130

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
3
  • 10
    While this works, be careful using it as your process will appear with the arguments, username and password, when doing something like "ps aux"
    – joebeeson
    Oct 30, 2015 at 13:53
  • can we loop the argument list and put them in array?
    – Vivek
    Sep 7, 2017 at 7:34
  • That's why you should pass it a file where your username and password are kept privately.
    – Han Zhang
    May 14, 2022 at 5:50
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".

1
  • The code above is for TCL which expect is using, and doesn't make sense for Bash.
    – spbnick
    Dec 8, 2018 at 9:31
7
#!/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
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.

1
  • 2
    I like argument checking all the time
    – Sohail Si
    Apr 15, 2022 at 9:12
-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.

-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.

3
  • The expect script is missing
    – Timo
    Mar 27, 2018 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, 2018 at 14:08
  • 2
    ok, I expected the content of your script which references the parameters from outside as the others showed in their examples.
    – Timo
    Aug 15, 2018 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.