Here is my version of script that is first trying to get docker stats container wise but and then moves onto the expect part to start a tester tool to fetch stats by pressing the x key. Script is as follows:
#!/bin/bash
#!/usr/bin/expect -f
docker stats --no-stream --format "{\"container\": \"{{ .Name }}\", \"memory\": { \"raw\": \"{{ .MemUsage }}\", \"percent\": \"{{ .MemPerc }}\"}, \"cpu\": \"{{ .CPUPerc }}\"}"
#docker cp new.sh main:/app/aicore/state_tools/
docker exec main bash -c "cd aicore/state_tools; pwd;
/usr/bin/expect << 'EOF'
set timeout -1
for { set i 0 } { $i < 300000 } { incr i } {
spawn /app/aicore/state_tools/ss_tester
expect "Exit"
send -- "x\r"
expect "press"
send -- "\s03"
sleep 300
}
EOF"
The error I get is as follows: missing operand at @ in expression " @< 300000 " (parsing expression " < 300000 ") invoked from within "for { set i 0 } { < 300000 } { incr i } { spawn /app/aicore/state_tools/ss_tester expect Exit send -- xr expect press send -- s03 sleep 300 }"
Can someone please guide what am I doing wrong in the code?
CodePudding user response:
Looks like a quoting problem. You have quoted the EOF for the here-document, which should prevent substitution on its contents, however that applies after it has been passed to docker. But before it gets to that stage it gets processed by the shell which is running your script, and that will try to substitute the $i before it even gets passed to docker. I would try putting a backslash in front of $i
to prevent this, i.e. \$i
.