I'll try to explain every line you highlighted:
if [ "$#" != 3 ]; then exit 1; fi #dont understand
If there aren't exactly three arguments, exit with error.
mapName=$1; key=$2; value=`echo $3 | sed -e "s/ /:SP:/g"` #dont understand
- Set the variable
$mapName
with the value of first argument
- Set the variable
$key
with the value of second argument
Set the variable $value
with the value of third argument, after replacing spaces with the string :SP:
.
map="`echo "$map" | sed -e "s/--$key=[^ ]*//g"` --$key=$value" #dont understand
This will edit the $map
variable, by removing the first occurrance of the value of $key
followed by =
and then by non-space characters, and then append the string --
followed by the value of $key
, then by =
and finally the value of $value
.
eval $mapName="\"$map\"" #dont understand
This will evaluate a string that was generated by that line. Suppose $mapName
is myMap
and $map
is value
, the string that bash will evaluate is:
myMap="value"
So it will actually set a variable, for which its name will be passed by a parameter.
map=${!mapName}
This will set the variable $map
with the value the variable that has the same name as the value in $mapName
. Example: suppose $mapName
has a
, then $map
would end up with the contents of a
.
value="$(echo $map |sed -e "s/.*--${key}=\([^ ]*\).*/\1/" -e 's/:SP:/ /g' )"
Here we set the value of the $value
variable as the value of the $map
variable, after a couple of edits:
- Extract only the contents that are specified in the expression between parenthesis in the sed expression, which matches characters that aren't spaces. The text before it specifies where the match should start, so in this case the spaces must start after the string
--
followed by the value of the $key
variable, followed by =
. The `.*' at the start and the end matches the rest of the line, and is used in order to remove them afterwards.
Restore spaces, ie. replace :SP:
with actual spaces.
eval map="\"\$$mapName\""
This creates a string with the value "$mapName", ie. a dollar, followed by the string contained in mapName, surrounded by double quotes. When evaluated, this gets the value of the variable whose name is the contents of $mapName
.
Hope this helps a little =)