Yes, I know what are you trying to say.
I searched "how to read lines from a file".
And I already have a function to load each line of the file into an array.
In my first post I gave an example of the command I need to build. I need to add those {}, [], "", '',\, etc. And these special chars are not in the file. I need to add them while I'm building the command. And depending on how many lines there are in the file, those special chars needs to be moved into the right place.
So, it's not just simply read the commands from the file and parse them to the command. I need to add the special chars in the correct place.
For instance, I'll give 2 different examples.
Let's say this file has 2 long strings that are parameters to the file and 1 other long string that may comes from inside the script itself, the command syntax looks like:
Code: Select all
app-command argument-1 "" "{\"param-1\":val1,\"param-2\":val2}" 1 "" "[\"param-3\",\"param-3\"]"
As you see command has 2 sections, one comprising a "{}" pair and another comprising a "[]" pair. There are other parts also but these are fixed. Never changes. The first "", the middle 1 and the middle "" right after the 1. And inside each pair of {} and [], there are also escaped ", : and ,.
So, for 2 long strings in the file plus that other long string which is not in the file, command needs to place the 2 long string from the file within the "{}" pair, both strings wrapped by the escaped " and followed by the :val and inside "[]" pair must be the other long string.
For 3 long string present in the file, command needs to be like:
Code: Select all
app-command argument-1 "" "{\"param-1\":val1,\"param-2\":val2,\"param-3\":val3}" 1 "" "[\"param-4\",\"param-4\",\"param-4\"]"
Note: param-3 from example 1 and param4 from example 2 are the same. Just had to name them different because I added a 3rd param to the "{}" pair.
I thought trying something with 'sed', but problem is the same.
Now I'm thinking to try with some loop, to add params until they run out but I'm not sure it's the best way. I'll try though.
EDITED;
Well, I did a little bit of experimenting and it's not yet complete but looks good (at least the result)
Code: Select all
send_to_mult_addr(){
load_addr_data
x=0
val1=0.02
com_params="\"\" \"{\""
for i in "${addr_arr[@]}"
do
com_params+="\"$i"
com_params+="\":"$val1","
done
printf "$com_params"
}