[SLUG] scripting context problem

From: Eben King (eben1@tampabay.rr.com)
Date: Sun Mar 12 2006 - 18:32:07 EST


I wrote this small script to calculate the amount of disk space a package
takes up:

  1 #!/bin/bash
  2
  3 for pkg in `dpkg -l '*' | grep '^i' | colrm 1 4 | cut -f 1 -d ' '` ; do
  4 sumstring="0"
  5 dpkg -L $pkg | while read file ; do
  6 [[ -e "$file" ]] || continue
  7 [[ -d "$file" ]] && continue
  8 sumstring="$sumstring `ls -lg "$file" | tr -s ' ' | cut -f 4 -d ' '` +"
  9 echo "ss='$sumstring'"
10 done
11 echo "ss='$sumstring'"
12 sum=`echo $sumstring 1024 / p | dc`
13 echo "$sum $pkg"
14 done

Problem is, "sumstring" in the inner loop (lines 5-10) is not the same as
"sumstring" outside of there. So when I query the value in line 9, it's "0
115 + 265 + 335 +" (or whatever) but it resets to "0" when I query it in
line 11. How can I get its value out? I've run into this problem before;
what I did then was to write its value to disk in a known location -- slow.

In C (and most programming languages), you would arrange to pass it out as
the return value of the function or a parameter. Or you could declare a
variable as global, so it's accessible from everywhere.

How do I get this value usable? Must I write it to disk, and hope it stays
in the cache so it's fastP Thanks.

-- 
-eben    ebQenW1@EtaRmpTabYayU.rIr.OcoPm    home.tampabay.rr.com/hactar

Q: What kind of modem did Jimi Hendrix use? A: A purple Hayes. ----------------------------------------------------------------------- This list is provided as an unmoderated internet service by Networked Knowledge Systems (NKS). Views and opinions expressed in messages posted are those of the author and do not necessarily reflect the official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:54:25 EDT