linux user group brescia

immagine del castello

Archivio della mailing list

Shell programming: variable substitution/expansion

Luca Coianiz luca a coianiz.it
Gio 30 Ott 2003 13:17:02 UTC

 Sto provando a scrivere una piccola procedura che mi attiva dei "monitor"
in tty inutilizzate ed ho incontrato un piccolo problema:

uso un file di configurazione /etc/sysmonitors.usr.conf
	[...]
# Number of monitor to start (from 1 up to N)
# BIG RED WARNING: UPDATE THE COUNTER IF YOU ADD/DELETE MONITORS !!!
NMON=3

# Define the seconds to wait before check a started/stopped monitor.
CHKDELAY=1

# Define the seconds to wait before restart stopped monitors.
RELDELAY=1

# Watch command path
WATCH="/usr/bin/watch"

#  Here you can define which "monitors" should be started, which tty will be
# addressed and the delay loop for watching them.
#
#  $MONCMDn        holds the monitor command
#  $MONTTYn        is the tty (/dev/ttyN) where the monitor is activated
#  $MONDLYn        is the refresh delay in sec(s) (see -n parm of watch(1))
#
##
## SysMon #1: netstat
##
MONCMD1='/bin/netstat -Nnepa'
MONTTY1=8
MONDLY1=10

##
## SysMon #2: pstree
##
MONCMD[2]='/usr/bin/pstree -clu'
MONTTY2=9
MONDLY2=1
	[...ecc...]

 ed il programma per l'attivazione/disattivazione in fase di boot/shutdown:
/etc/init.d/sysmonitors.usr

#!/bin/sh -x
#
	[...CUT...]
#
### BEGIN INIT INFO
# Provides:       sysmonitors.usr
# Required-Start:
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:   0 1 5 6
# Description: Silently starts some (user) system monitors on unused tty(s).
### END INIT INFO	('sta roba serve a YaST2)

# Get the configuration					# 20030822=LC
. /etc/sysmonitors.usr.conf

# Load the (user-defined) text colors definitions (z<colorname>).
#. /etc/usr.colortable  ### DEBUG ### (Re-enable when finish testing)

case "$1" in
 start)
  echo -e "${zbold}Starting user system monitors ($NMON defined)${znorm}"
   for ((a=1; a<=$NMON; a++))				# 20021205=LC
    do
#         startproc -s ${MONFCMD[$a]}		<== tmp disabled
#         CMD1=${MONCMD[$a]}			<== tmp disabled
          CMD1="$((MONCMD$a))"
          CMD="$WATCH -n $((MONDLY$a)) $CMD1 > /dev/tty$((MONTTY$a)) &"
exit 1
	[...ecc...]

 Eseguendo quanto sopra ottengo il seguente output:

+ . /etc/sysmonitors.usr.conf
++ NMON=3
++ CHKDELAY=1
++ RELDELAY=1
++ WATCH=/usr/bin/watch
++ MONCMD1=/bin/netstat -Nnepa
++ MONTTY1=8
++ MONDLY1=10
++ MONCMD[2]=/usr/bin/pstree -clu
++ MONTTY2=9
++ MONDLY2=1
++ MONCMD[3]=/usr/bin/top -d 1 i s n 1 b
++ MONTTY3=11
++ MONDLY3=1
+ echo -e 'Starting user system monitors (3 defined)'
Starting user system monitors (3 defined)
+ (( a=1 ))
+ (( a<=3 ))
./sysmonitors.usr.new: /bin/netstat -Nnepa: syntax error: operand expected
(error token is "/bin/netstat -Nnepa")

 Se invece definisco MONCMD1 come un vettore (v. ad es. MONCMD[2] sopra):

+ . /etc/sysmonitors.usr.conf
++ NMON=3
++ CHKDELAY=1
++ RELDELAY=1
++ WATCH=/usr/bin/watch
++ MONCMD[1]=/bin/netstat -Nnepa
++ MONTTY1=8
++ MONDLY1=10
++ MONCMD[2]=/usr/bin/pstree -clu
++ MONTTY2=9
++ MONDLY2=1
++ MONCMD[3]=/usr/bin/top -d 1 i s n 1 b
++ MONTTY3=11
++ MONDLY3=1
+ echo -e 'Starting user system monitors (3 defined)'
Starting user system monitors (3 defined)
+ (( a=1 ))
+ (( a<=3 ))
+ CMD1=/bin/netstat -Nnepa
+ CMD=/usr/bin/watch -n 10 /bin/netstat -Nnepa > /dev/tty8 &
+ exit 1

 e tutto finisce bene.

 Ora... è vero che potrei (senza problemi) definire le stringhe-comando dei
"monitor" come vettori, nel file di configurazione... ma mi rimane oscuro un
particolare: come mai "/dev/tty$((MONTTY$a))" viene correttamente risolto in
"/dev/tty8" (quindi, se $a = 1, $MONTTY1 = 8) mentre non c'è verso di far
risolvere CMD1="$((MONCMD$a))" in CMD1="/bin/netstat -Nnepa" dato che, se
$a=1, $MONCMD1 = "/bin/netstat -Nnepa" senza beccarsi l'errore ?

 Cioè... non dovrei beccarmi lo stesso errore in fase di risoluzione di
$((MONTTY$a)) ?

 10x in advance x l'help :-)

	Ciao,
		Luca

P.S.
 Ovviamente il "trabiccolo" è GPLware... se qualcuno fosse così temerario da
volerlo provare e/o interessasse. ;-)
 Io lo uso per avere uno "status at glance" di alcuni aspetti del sistema.





Maggiori informazioni sulla lista Lug