Expect: invalid command name "*" - expect

In my Expect script this line is throwing an error:
expect "Address or name of remote host [*]? "
In the log I see this error:
switchnumber1#invalid command name "*"
while executing
"*"
invoked from within
"expect "Address or name of remote host [*]? ""
The remote host ip address in the brackets could change.

Expect uses Tcl. In Tcl, [...] in double quotes is the command substitution syntax which is like $(...) (or `...`) in Bash.
To include a literal [ char you could write:
expect "Address or name of remote host \[*]? "

Related

Can't run an expect script: invalid command name "Yes/No"

I have this expect script that need to execute some other shell script to accept a Licence agreement
#!/usr/bin/expect
spawn ./xxx.sh
expect -ex "--More--"
send -- " "
expect "Do you agree with this license ?[Yes/No]"
send "Y\r"
But when I run it I get this error
invalid command name "Yes/No"
while executing
"Yes/No"
invoked from within
"expect "Do you agree with this license ?[Yes/No]""
(file "./xxx.sh" line 5)
I don't know what I'm doing wrong
expect is an extension of the Tcl language. In Tcl, you use square brackets for command substitution. Like the bash shell, command substitution occurs within double quoted strings.
To prevent your code from attempting to execute the Yes/No command:
use different quotes: Tcl uses curly braces as the non-interpolating quotes:
expect {Do you agree with this license ?[Yes/No]}
escape the brackets to prevent command substitution:
expect "Do you agree with this license ?\[Yes/No\]"

Expect script not working and send status getting skipped

I have written a script to execute a setup on the server, but while executing send statement is getting skipped and other one getting executed.
Below is my script :
enter code here[code written][1]
====================ERROR m getting=============================
Enter the name of the new namespace you want to create [coe_ns] :
Enter the name of the clearinghouse you want to create [coe_ch] : invalid command name "C"
while executing
"C"
invoked from within
"expect -re "* Option to execute (Continue/Restart/Quit) [C]: $""
(file "./TNS_Server_Setup.sh" line 21)
I have a doubt that send command before that is not getting executed, also am I passing "ENTER" right way.
expect uses square brackets as the command substitution syntax (in the same way that the shell uses backticks or $(...). So when you write
expect -re "* Option to execute (Continue/Restart/Quit) [C]: $"
expect sees [C] and tries to invoke the command C.
Also, that standalone * will throw a "quantifier operand invalid" error, because that's a special character for regular expressions. You either need to escape it or remove it from the expression.
Use a different quoting mechanism that inhibits command substitution (like in the shell where you'd use single quotes)
expect -re {Option to execute (Continue/Restart/Quit) [C]: $}
You did not include your code in the question so we can't see your send command, but to "just hit enter", you would send "\r"
Update:
If you need to match multiple things, the expect command can do that: give it a list of pattern action pairs. Expect will match any of them.
expect {
-re {Option to execute (Continue/Restart/Quit) [C]: $} {
send "\r"
exp_continue
}
-re {enter the datatype $} {
send "array\r"
exp_continue
}
"some pattern that denotes we're done"
}

Syntax error for '&' in a remote command

I want to ssh to a node and run a program in a background using &. So, the script looks like
#!/bin/bash
ssh NODENAME 'lmgrd -c license.lic &;
exit;'
However, the bash interpreter complains for syntax error near unexpected token ;. It seems that &
& already finisheds a command. So after & you simply don't use any terminator.

Want to use double quote inside a double quote

#!/opt/sfw/bin/expect --
spawn telnet -e ! [lindex $argv 0]
expect "<"
send "ACT-USER::ABCDEFG:123::\"ABCD\";"
Above is not working giving error due to double quote inside a double quote
Error:
spawn telnet -e !
Telnet escape character is '!'.
invalid flags
send: spawn id exp7 not open
while executing
"send "ACT-USER::ABCDEFG:123::'\"ABCD\"';""
(file "./get-amp-pms.sh" line 4)
Use curly braces instead of the double quotes for the send, as follows:
send {ACT-USER::ABCDEFG:123::"ABCD";}
The error is unrelated to the double-quoting; the spawned process is exiting on an error before the command string is being sent.
expect is tcl, so tcl quoting mechanisms work. Sending a string with double quotes can be done by escaping the quoted with backslashes, or using braces:
send "message with \"double quotes\""
send {message with "double quotes"}
Not sure But we can use single quote in few commands like(sed) to overcome this issue and freely use double quote inside single quote without hampering the functionality.So try using the parameters enclosed in single quote(Example : send 'paramters' )to execute it

Ruby and system input

I have a command which is run with the system command.
Could I log what is passed to the shell?
P.S. I've got the problems with qoutes =)
You mentioned having "the problem with the quotes". I assume you mean double quotes? Modify Vapire's solution to this:
command = %Q{echo "double quoted string"} # store the command as string
puts command # print it before you execute it
system(command) # execute it in the shell
You can simply print the string before you're sending it to system command
command = "whoami" # store the command as string
puts command # print it before you execute it
system(command) # execute it in the shell

Resources