I am working on a shell script which is getting data from another program and I am using that variable value to read the content from file and append few things inside those data at start and bottom:
Below is an example:
readonly file_location=$location
readonly client_id = $id
readonly client_types = $type_of_client
Here $location, $id and $type_of_client value is being passed from another program. Below is an example:
- $location will be full path name like this:
/home/david/data/12345678
- $id will be number:
120
- $type_of_client will be space separted word:
abc def pqr
Now inside this location /home/david/data/12345678
I have files like this: abc_lop.xml
, def_lop.xml
and pqr_lop.xml
. Meaning _lop.xml
is going to be same always so we can hardcode this. We just need to iterate client_types
variable and make a file name as shown above and read the whole file into a String for now (or may be if there is any other better way). This I got working as shown below:
#!/bin/bash
readonly file_location=$location
readonly client_id=$id
readonly client_types=$type_of_client
for word in $client_types
do
echo $word
file_value=`cat "file_location"/"$word"_lop.xml`
echo $file_value
done
Now I need to do something special like string manipulation in shell script and I am not sure how we can do that. Now whatever value we read from those file I need to append something at the top of it and something at the bottom of it in all those files which we are reading. I need to do exactly same thing as shown below:
For example: If file abc_lop.xml
is like this after we view it using vi
:
<Hello version="100">
<!-- some stuff here -->
</Hello>
I need to make it like this:
<hello_function>
<name>Data</name>
<Hello version="100">
<!-- some stuff here -->
</Hello>
</hello_function>
As you can see, I added this at the top <hello_function><name>Data</name>
and I added this at the bottom </hello_function>
. How can we do this in shell script?
So In general echo $file_value
should print out above appened values for each of those client_types
:
#!/bin/bash
readonly file_location=$location
readonly client_id=$id
readonly client_types=$type_of_client
for word in $client_types
do
echo $word
file_value=`cat "file_location"/"$word"_lop.xml`
// may be do something here?
echo $file_value // this should print out appended string value both at top and bottom.
done
Each of those XML files will be less than 10MB for sure.