Okay, so I am just wondering why, in the block of code below, the commented out code only outputs 1 file - the last file that would be created? I don't have much experience with sed or writing shell scripts, but they seem to be identical to me except the second for loop doesn't use variables to specify the replacements used in the sed command. I'm guessing it has something to do with how I edit the strings stored in the variables with the while loop iterators.
i=32;
j=2;
SEPORIG="ghsep1";
#SEPHN="ghsep"$j"";
SEPIP="10.84.194.31";
#NEWIP="10.84.194."$i"";
SEPORGN=ghsep1.json;
#SEPNEW=ghsep"$j".json;
#while [ $i -lt 90 ];
#do
# sed "s/$SEPORIG/$SEPHN/; s/$SEPIP/$NEWIP/" $SEPORGN > $SEPNEW;
# i=$(( $i + 1 ));
# j=$(( $j + 1 ));
#done;
while [ $i -lt 90 ];
do
sed "s/$SEPORIG/"ghsep"$j""/; s/$SEPIP/"10.84.194."$i""/"$SEPORGN > ghsep"$j".json;
i=$(( $i + 1 ));
j=$(( $j + 1 ));
done;
Basically this code just edits the hostname and IP address of a JSON file used to create specifications for a server. The iterators and conditionals are hardcoded because we know how many servers we will deploy using these JSON configuration files. I think the code is probably very ugly due to my limited knowledge with either JSON or shell scripts.
Can anyone provide me any insight into how the 2 blocks of code for the loops differ? Maybe I'm just missing something and new a fresh set of eyes. Also, if anyone has any suggestions on how I can improve the script based on what I've told you, that would be great! I feel like there is probably some things I can do to clean up or shorten the code, or maybe not since it requires a decent amount of hardcoding for the size of the script? This is especially true due to the fact that not every hostname will be "ghsep##", there are at least 4 other types of hostnames - for example "gmtip##".
答案 0 :(得分:0)
Taking from your commented code :
sed "s/$SEPORIG/$SEPHN/; s/$SEPIP/$NEWIP/" $SEPORGN > $SEPNEW
will overwrite file $SEPNEW
(which has fixed value ghsep2.json) for each iteration, this is why it only gives you one file and only the last iteration is prevailing.
In you new code, you are iterating file with $j
giving you a new file for each iteration : ghsep"$j".json