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?
Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.
To invoke a function, simply use the function name as a command. To pass parameters to the function, add space-separated arguments like other commands. The passed parameters can be accessed inside the function using the standard positional variables i.e. $0, $1, $2, $3, etc.
The first line defines the expect command path which is #!/usr/bin/expect . On the second line of code, we disable the timeout. Then start our script using spawn command. We can use spawn to run any program we want or any other interactive script.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With