Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 636
lk121 Avatar asked Jun 12 '13 07:06

lk121


People also ask

How do you pass arguments in Expect script?

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.

How do you pass a command line argument to a function in shell 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.

How do I run a command in Expect script?

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.


1 Answers

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 
like image 193
bartimar Avatar answered Sep 17 '22 13:09

bartimar