1

I've recently started coding in bash and I'm making a diagnostic check CD for rackmounted computers to print out to an LCD screen.

So far it works but what I want it to do is when it gets to the part where it checks what ethernet ports are unplugged, it stays there for 30 minutes or so, repeating the check and afterward proceeds through the script.

IE: When you unplug the 1st ethernet port, the LCD displays LAN 1 DIS, plug it in and it goes away. With 1&2 unplugged it alternates between saying LAN 1 DIS and LAN 2 DIS, so I want it to keep doing this for a specified period of time and then continue.

I've looked up cron and that seems to be useful for making timers on permanent systems but I just want the cd to boot, do the check and then eject it and let the real OS take over and I've tried using:

while [ `sleep 30m` ];
  do    

   /mnt-system/KNOPPIX/lcdwriter.pl "";
   BAD="";

  for i in 0 1 2 3
  do

  #LAN number to ETH number mapping
  case $i in
     0)
        j=1;
     ;;
     1)
        j=2;
     ;;
     2)
        j=4;
     ;;
     3)
        j=3;
     ;;
  esac


     STATUS=`/usr/sbin/ethtool eth$i | grep "Link detected: no"`;

     if [ "x$STATUS" != "x" ]
     then
        BAD=$BAD$j;
        echo "LAN $BAD Disconnected";
        /mnt-system/KNOPPIX/lcdwriter.pl "LAN $BAD   DIS";

    sleep 5;
     fi
     done;
       done;

1 Answer 1

0

Not tested, but the script below should do about what you want. It uses a counter to keep track of how much time is left in the loop. It gets decremented and tested after each iteration through the loop.

#! /usr/bin/bash
#LAN number to ETH number mapping
function eth2lan() {
    case "$1" in
    0)
        echo 1;
        ;;
    1)
        echo 2;
        ;;
    2)
        echo 4;
        ;;
    3)
        echo 3;
        ;;
    esac
}
timeleft=$(( 30 * 60 ))

while true; do
    for i in 0 1 2 3; do
        /mnt-system/KNOPPIX/lcdwriter.pl ""
        if /usr/sbin/ethtool eth$i | grep -qs "Link detected: no"; then
            echo "LAN $(eth2lan $i) Disconnected"
            /mnt-system/KNOPPIX/lcdwriter.pl "LAN $(eth2lan $i)   DIS";
        fi
    done
    timeleft=$(( $timeleft - 5 ))
    if [[ "$timeleft" -lte 0 ]]; then
        break
    fi
    sleep 5
done
1
  • Thanks. I'm attempting this now, we'll see where it goes. Jun 8, 2011 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.