I wrote a little bash script using sed
on some html pages to extract some urls.
To avoid each time grabbing sed results in a variable then read it again I simply made 3 functions and piped together.
first_function $1 | second_function | third_function
Let's say that:
- the first function finds item urls in a list from a given href (
$1
) - the second function extracts from each of the piped urls an image
src
- the third function assembles output HTML
at the moment I echo $lot_url
in second_function
so I can read
it in third_function
along with $img_url
and put it in HTML.
It would be cleaner if I could just hold it as a global variable that's accessible fromsecond_function
to third_function
but seems I can not.
This becomes more necessary as the number of values to pass between functions grows.
Here a full sample code:
first_function(){ curl -s "$1" | sed -nr ' #extract sub urls ' } second_function(){ while read lot_url; do echo "$lot_url" curl -s "$lot_url" | sed -nr ' #extract img src ' done } third_function(){ while read lot_url; read img_url; do echo "<a href="$lot_url"><img src="$img_url" /></a>" done } first_function "$1" | second_function | third_function