Problem
In a shell script, I’m giving an argument to Expect via the command line.
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 "$ "
However, it isn’t working. What can I do about it?
Asked by lk121
Solution #1
If you want to read from arguments, all you have to do is follow the steps below.
set username [lindex $argv 0];
set password [lindex $argv 1];
And print it
send_user "$username $password"
That script is going to print.
$ ./test.exp user1 pass1
user1 pass1
Debug mode is available.
$ ./test.exp -d user1 pass1
Answered by bartimar
Solution #2
This might be a better way to go:
lassign $argv arg1 arg2 arg3
Your method, on the other hand, should work as well. Verify that arg1 has been obtained. Send user “arg1: $arg1n” is an example.
Answered by spbnick
Solution #3
#!/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
Answered by user128364
Solution #4
This guide’s response is appealing to me.
It starts the parse argument procedure.
#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)"
This is just a taste of what’s to come. It’s critical to read the entire handbook and include enough user argument checks.
Answered by Duffmannen
Solution #5
It’s worth noting that argv 0 can also be the name of the script you’re calling. As a result, argv 0 will not work if you run it that way.
I’m a runner.
expect script.exp password
As a result, argv 1 equals password and argv 0 equals script.exp.
Answered by Sid Dakota
Post is based on https://stackoverflow.com/questions/17059682/how-to-pass-argument-in-expect-through-the-command-line-in-a-shell-script