0

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?

1 Answer 1

1

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 .

Sign up to request clarification or add additional context in comments.

3 Comments

Okay this works as I am not getting the error. Can you guide me on the keypress command. When I do send -- "x\r" it inputs xr but I want to only input x and enter.
Ok, I think that's another quoting issue. Try doubling the backslash to avoid this, i.e. send -- "x\\r". You probably need to do this with the other send command also.
Aweome man..Thank you so much this really helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.