I have a bash
script that creates and executes an expect
script by stitching together dozens of different files containing pieces of expect
code. Those files contain environment variables that need to be expanded. Example:
expect.piece:
send "command\r"
sleep $timeout
send "command argument\r"
script.sh:
#let's try it like this
eval echo $(cat expect.piece)
#or maybe like this
eval "echo \"$(cat expect.piece)\""
output:
send command\r sleep 1 send command argument\r
send commandr
sleep 1
send command argumentr
Desired otput:
send "command\r"
sleep 1
send "command argument\r"
I need a solution without sed
string substitution (there is a lot of environment variables) and without modifying original expect
script files. I guess it could be done line by line, but is there a more elegant solution?
I invented the solution for this problem, it is a kludge, but it works.
expect.piece:
script.sh:
output:
First, we need to escape all the backslashes, backticks and quotes in the file, because they will be removed during the evaluation. Then, we need to replace all the newline characters with pluses, in order to make it in a single line. After that, we evaluate that line (the evaluation will "expand" all the environment variables and command substitutions) and replace pluses back to newlines.