Gurus - Please help with this urgent situation.
I have the following problem to solve using a shell script:
- There are about 13 files named CONTAINER_1.lst, CONTAINER_2.lst, CONTAINER_3.lst .....CONTAINER_13.lst
- Each of these files contain about 8 lines (in most cases) containing container instance names like CONTAINER_X_InstanceY where X will correspond to the container in question (ranging from 1 thru 13) and Y is some random variable value
- For each of the 13 containers, there is a file named, CONTAINER_X_commands.sh where X corresponds to the container number. Each of the CONTAINER_X_commands.sh file contains about 3 to 5 commands with a fixed embedded string called "HELLOWORLD"
- For each container name, I would like to first read all the CONTAINER_X_InstanceY variables (one at a time), then repeat each of command in the CONTAINER_X_commands.sh file, N number of times where N is the number of lines in the CONTAINER_X.lst (equal to the number of container instances) Once the yanking/creating multiple copies of commands is done, I would like to replace each occurrence of the "HELLOWORD" string with CONTAINER_X_InstanceY container instance name variable
Here is the code I have written so far :
for container_list in `ls -1 CONTAINER*.lst | sort -u`
do
container_name=`echo $container_list | sed 's;\(.*\)\..*;\1;'`
cmd_suffix=_commands.sh
command_file=$container_name$cmd_suffix
for container_instance_name in `cat $container_list`
do
for command in `cat $command_file`
do
echo $command | sed 's/HELLOWORLD/$container_instance_name/g;' >> /tmp/all_commands_in_singlefile.lst
done
done
done
There are 3 problems in my output :
- It's printing each word in the commands.sh file on separate line. I would like to keep each command intact on it's own line without breaking each command word on separate line (I think this is an issue with my innermost for loop)
- the sed is actually replacing HELLOWORLD with the literal string "$container_instnace_name" instead of using the actual variable value
- I am also suspecting that the output file is containing lesser number of rows of commands, than I would actually anticipate. But this is yet to be substantiated because of newlines introduced in my output.
Any help will be greatly appreciated!
indi