Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining "read" and "while"

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
Hi folks,

another task ahead of me ...
Maybe you could give me a little idea about how to proceed.

I got a nice little monitoring script. Something like this (short version, just to make clear where the problem is):

Code:
clear
while [ 1 = 1 ];
do
arcstat=$(ps -ef | grep archiver | wc -l)
clear
case $arcstat in
 0) echo "Archiver NOT running !";;
 *) echo "Archiver running";;
esac
sleep 30
done

I keep the window containing the script active, running on screen of a monitoring PC.

Question is: Is it possible to modify the script, so that it would be possible to enter a command (e.g. restart the archiver, if it goes down) without having to interrupt the while statement ?

A first I thought of using functions somehow. Like this:

Code:
function checking
{
clear
while [ 1 = 1 ];
do
arcstat=$(ps -ef | grep archiver | wc -l)
clear
case $arcstat in
 0) echo "Archiver NOT running !";;
 *) echo "Archiver running";;
esac
sleep 2
done
}

function input
{
while [ 1 = 1 ];
do
read action
case $action in
 Q|q) kill $checkingpid
      ;;
   *) $action;;
esac
done
}

checking &
checkingpid=$!

input

But that's not it yet. It reads my Inputs, but keeps clearing them every 2 seconds, so I have to be extremely fast, which is quite impossible if I'm trying to enter longer commands. Increasing the sleep value wouldn't work either, because e.g. if I set it to 30 and accidently start entering the command after let's say 28 seconds I'm facing the same problem again. I need a way to separate the checking function from the input function ...
What am I missing ?
How else could I solve this problem ?

Regards,
Thomas
 
Hi

As you not specified your shell, I suppose that you are using [tt]bash[/tt].
Bash:
[b]while[/b] [teal]:;[/teal] [b]do[/b]
  [b]if[/b] ps -ef [teal]|[/teal] grep -q [green][i]'archiver'[/i][/green][teal];[/teal] [b]then[/b]
    echo [green][i]"Archiver running"[/i][/green]
  [b]else[/b] 
    echo [green][i]"Archiver NOT running !"[/i][/green]
  fi

  [COLOR=chocolate]read[/color] -n [purple]1[/purple] -s -t [purple]30[/purple] char
  [b]case[/b] [green][i]"$char"[/i][/green] [b]in[/b]
    [green][i]'i'[/i][/green][teal])[/teal]
      [COLOR=chocolate]read[/color] -p [green][i]'enter command> '[/i][/green] action
      [b]case[/b] [green][i]"$action"[/i][/green] [b]in[/b]
        [teal]*)[/teal] echo [green][i]"processing command '$action'"[/i][/green]
      [b]esac[/b]
    [teal];;[/teal]
    [green][i]'q'[/i][/green][teal])[/teal] [COLOR=chocolate]break[/color] [teal];;[/teal]
  [b]esac[/b]
[b]done[/b]
What I am trying to show you is that you could do it in two steps :
[ul]
[li]First you hit a single character which starts interactive mode[/li]
[li]In interactive mode the looping is not working, so you can type looong commands easily[/li]
[/ul]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top