[Gllug] bash indirect variable referencing ugliness

Julian Somers lists at bigpip.com
Tue Nov 21 15:45:38 UTC 2006


Take a deep breath, hold your nose, and scroll down to my hideous hack
to do indirect variable reference in this bash script.

Is there a more elegant way to do this? I wrote it yesterday, and
already I have forgotten what half of those evals and backtics are for!
I hate to think what I'll make of it in a month's time.

thanks for your time,
Julian


#!/bin/bash

# tunes dvb cards and creates dvb network interfaces 

services="ebd9s hb4 thor2"

adapter_thor2=0
adapter_ebd9s=1
adapter_hb4=2

lnb_settings_ebd9s="9750,10600,11700"
lnb_settings_hb4="9750,10600,11700"
lnb_settings_thor2="9750,10600,11700"

pid_ebd9s=5050
pid_hb4=3331
pid_thor2=2401

ip_ebd9s=10.10.10.1
ip_hb4=10.10.10.2
ip_thor2=10.10.10.3

# files & bins
base=/usr/wiredocean
frequencies="${base}/frequencies"
output_file=${base}/monitor.output
#szap=/usr/bin/szap
szap="${base}/szap_sbs"
pmacctd=/usr/sbin/pmacctd
pmacct=/usr/bin/pmacct
dvbnet=/usr/bin/dvbnet

# named pipes for reading signal data
szapout_ebd9s=/var/tmp/szapout_ebd9s
szapout_hb4=/var/tmp/szapout_hb4
szapout_thor2=/var/tmp/szapout_thor2

for service in $services;
do

	# indirect variable name MADNESS!
	adapter=`eval echo "$(eval echo adapter_${service})"` 
	lnb=`eval echo "$(eval echo lnb_settings_${service})"` 
	szapout=`eval echo "$(eval echo szapout_${service})"` 
	pid=`eval echo "$(eval echo pid_${service})"` 
	ip=`eval echo "$(eval echo ip_${service})"` 

	$szap  -a ${!adapter} -l ${!lnb} -c frequencies $service 2>&1 > ${!szapout} &

	# create network adapter
	$dvbnet -p ${!pid} 2>&1 > /dev/null

	# what interface name?
	interface=`dvbnet -l | grep ${!pid} | awk '{ print $5 }'  | sed ' s/,//g'`

	ifconfig $interface ${!ip}
	`echo 0 >> /proc/sys/net/ipv4/conf/${interface}/rp_filter`

	echo started $service using dvb adapter ${!adapter} as $interface

done



-------------- next part --------------
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug


More information about the GLLUG mailing list