Reputation: 117
I use an expect script to login to a remote server; to do so, I need to set up 2 variables:
set username <user>
set password <passwd>
I grep user
& passwd
from a file. How to set up expect variable with grep ouptut?
when I try:
set username "grep '(?<=username:)[^<]*' file"
set password "grep '(?<=password:)[^<]*' file"
expect says:
invalid command name "^<" while executing "^<" invoked from within
"send "grep -oP '(?<=username:)[^<]*' file""
(file "./test" line 11)
when I try:
set username "grep -oP '(?<=username:)[\^\<]*' file)"
expect says:
can't read "(grep -oP '(?<=username:)":
no such variable while executing
"send "$(grep -oP '(?<=username: )[\^\<]*' file)""
(file "./test" line 11)
I am just wondering what could be the correct syntax.
Upvotes: 1
Views: 697
Reputation: 246774
You'll want something like this:
set fid [open file r]
set contents [read $fid]
close $fid
lassign [regexp -inline {username:([^<]+)} $contents] x username
lassign [regexp -inline {password:([^<]+)} $contents] x password
You're having a hard time with Tcl syntax, so start with the Tcl tutorial.
Upvotes: 2