euglug.org

[Eug-lug] Reading data to multiple variables

작성자

카테고리:



Bob MillerWed Jul 28 12:46:08 PDT 2004

Garl Grigsby wrote:

> Doesn't this result in all of the data being in a(1)?

No.

    jogger-egg ~> cat gdata
    05e307020000000000000000 Yes
    05e307020000000000000000 Yes 05e307020000000000000000 Yes
    jogger-egg ~> cat gscript
    #!/bin/sh

	    while read line
	    do
		a=($line)
		echo line $line
		echo a[\*] ${a[*]}
		echo a[0] ${a[0]}
		echo a[1] ${a[1]}
		echo a[2] ${a[2]}
		echo a[3] ${a[3]}
	    done < gdata
    jogger-egg ~> bash gscript
    line 05e307020000000000000000 Yes
    a[*] 05e307020000000000000000 Yes
    a[0] 05e307020000000000000000
    a[1] Yes
    a[2]
    a[3]
    line 05e307020000000000000000 Yes 05e307020000000000000000 Yes
    a[*] 05e307020000000000000000 Yes 05e307020000000000000000 Yes
    a[0] 05e307020000000000000000
    a[1] Yes
    a[2] 05e307020000000000000000
    a[3] Yes
    jogger-egg ~> 

> What I am trying to do is to check to see which of several USB
> harddrives is installed. So I am parsing /proc/scsi/usb-storage*/*
> for (1) the GUID, and (2) the whether that device is attached or
> not.

You don't need awk for that.  You could do something like this if
you wanted to process each USB device separately.

	for unit in /proc/scsi/usb-storage-*/*
	do
	    while read name value
	    do
		case "$name" in
		    GUID:)       guid="$value";;
		    Attached:)   attached="$value";;
		esac
	    done < "$unit"
	    echo ${unit}: GUID: $guid attached: $attached
	    # do something useful for one unit here.
	done

-- 
Bob Miller                              K<bob>
kbobsoft software consulting
http://kbobsoft.com                     kbob at jogger-egg.com