I want to know if the code below can be improved of how it is written is the way to write it. If it wasn't clear I am a newbie to bash but my script completes its purpose.
This code is trying to define the variables s$counter= s1, s2, s3, s4...
in terms of the previously defined variables s1$j
and s2$j
with j
a string that is an item of a list (all defined previously).
counter=1
for j in ${list[@]}
do
eval s$counter=$(eval "echo \$s1$j")
eval s$((counter+1))=$(eval "echo \$s2$j")
counter=$((counter+2))
done
The inner eval
, meaning $(eval "echo \$s1$j")
es meant to return the value of s1$j
. The second eval
, meaning eval s$counter =...
is meant to define the variables s1, s2, s3, s4 ...
So an example would be: let's say for the first foor loop j=a
then $(eval "echo \$s1$j") == $s1a
, with the value of s1a
defined earlier in the script, for example let it be "s1a=10
", so that when the second eval
is evaluated we have the command that states "s1=10
".
It works, but could something happen that would make this a possible memory threat or something like that?.
The same idea with this line of code.
eval $(echo "sed -i '$(eval echo '17i$sed17line')' $file")
where sed17line
is what I want to add in the line 17 of file
. It Depends on what I want to use the script for, which is why I am using it as a variable and hence why I am using a combination of echo
and eval
.