Dom
Dom

Reputation: 1722

Can't use awk in expect script: invalid command name "awk"

I am trying to write a script in awk to upgrade firmware on some devices. I am trying to read a list of nodes using awk, but for some reason it can't find and execute an awk command and I keep getting the error invalid command name "awk" while executing "awk {{'NR==$i' test.txt}}" ("for" body line 2). I am new to scripting in expect, but I have seen other people execute awk in expect and I have no idea why it says awk is an invalid command name.

#!/usr/bin/expect -f

set logfile [open logfile.txt "w"]
#set len [awk 'END {print NR}' test.txt]
set timeout -1
spawn $env(SHELL)
match_max 100000
for {set i 1} {$i <= 2} {incr i 1} {
   set node [awk {{'NR==$i' test.txt}}]
   send -- "./myconsole\r"
   expect "*Option-> "
   send -- "4\r"
   expect "*Firmware upgrade was successful for node"
   puts $logfile "Node ID $node success"
}

expect "*Option-> "

send -- "^D^C\r\r"

close $logfile

Anyone have any ideas why awk is invalid?

Upvotes: 2

Views: 2291

Answers (2)

Cjolly
Cjolly

Reputation: 490

Actually it is possible that the command will work without using exec. Expect which is a tcl interpreter with the expect extensions will have the unknown function which when the lookup of a command or proc fails it is called. This unknown function can invoke the exec on the command for you and return the result. Most interpreters are setup that way. One of the cool things about Tcl is that you can rewrite/augment the unknown proc to suit your needs. What is another more likely possiblity is that you do not have awk on your path. In expect ( and by extension tcl), you can check this like so:

set AWK [auto_execok "awk"
if { [ string length $AWK ] == 0 } { 
   puts "Awk not found on path!"
   exit 1
} else { 
   puts "Awk has been found and is : $AWK"
} 
exec $AWK ....

Upvotes: 0

vapace
vapace

Reputation: 350

You have to use [exec awk ...]

Upvotes: 2

Related Questions