I have two mappings file like this as shown below:
primary_mapping.txt
{1=[343, 0, 686, 1372, 882, 196], 2=[687, 1, 1373, 883, 197, 736, 1030, 1569], 3=[1374, 2, 884, 737, 198, 1570], 4=[1375, 1032, 1424, 3, 885, 1228], 5=[1033, 1425, 4, 200, 886]}
secondary_mapping.txt
{1=[1152, 816, 1488, 336, 1008], 2=[1153, 0, 817, 337, 1489, 1009, 1297], 3=[1, 1154, 1490, 338], 4=[1155, 2, 339, 1491, 819, 1299, 1635], 5=[820, 1492, 340, 3, 1156]}
In the above mapping files, each clientId
has primary and secondary mapping. For example: clientId 1
has 343, 0, 686, 1372, 882, 196
primary mapping and 1152, 816, 1488, 336, 1008
secondary mapping. Similarly for other clientIds
as well.
Below is my shell script in which it prints primary and secondary mapping for a particular clientid:
#!/bin/bash
mapfiles=(primary-mappings.txt secondary-mappings.txt)
declare -a arr
mappingsByClientID () {
id=$1 # 1 to 5
file=${mapfiles[$2]} # 0 to 1
arr=($(sed -r "s/.*\b${id}=\[([^]\]+).*/\1/; s/,/ /g" $file))
echo "${arr[@]}"
}
# assign output of function to an array
# this prints mappings for clientid 3. In general I will take this parameter from command line.
pri=($(mappingsByClientID 3 0))
snd=($(mappingsByClientID 3 1))
Now let's say if we can't find primary or secondary mapping for a particular clientid
then I want to exit from the shell script with nonzero status code by logging message. I tried exiting from subshell and it didn't worked for me. Is this possible to do?