[clug] Please help

Michael Ellerman michaele at au1.ibm.com
Tue Jun 1 08:24:23 GMT 2004


Hey Jason,

That happens because everything on the right of a pipe is run in a sub shell. 
If you put a "sleep 30" inside the inner while loop and do a pstree, you'll 
see something like this:

michael at concordia:~$ pstree -ap 26240
foo,26240 ./foo
  `-foo,26242 ./foo	# This is the bit inside the inner while loop
      `-sleep,26243 30

Here's how I'd do it:

---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<
#!/bin/bash

hostfile=/etc/hosts

function main
{
    echo "This program will find a host name in $hostfile"
    printf "Enter an IP address: "

    while read findIP
    do
        if [[ $findIP = "999" ]]; then
            break
        fi

        grep -F "$findIP" $hostfile | find_ip $findIP

        printf "\nEnter an IP address: "
    done

    echo "Process teminated normally"
}

function find_ip
{
    typeset found=0

    while read ip host aliases
    do
        if [[ "$ip" = "$1" ]]; then
            found=1
            break
        fi
    done

    if [[ $found -eq 0 ]]; then
        echo -e "\nThe host name for IP address $1 cannot be located in 
$hostfile\n"
    else
        echo -e "\nThe host name for IP address $1 is $host"
        if [[ $(echo $host | grep -c labs) -gt 0 ]]; then
            echo "This refers to a lab printer"
        fi
    fi
}

main "$@"
---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: signature
Url : http://lists.samba.org/archive/linux/attachments/20040601/dc2d2df8/attachment.bin


More information about the linux mailing list