Ending an infinite loop with the pipe command in scope

I have a very simple unix bash script. I use it to execute a command every second. It has the following form:

while : ; do
  cat /proc/`pidof iBrowser.bin`/smaps | awk -f ./myawkscript.awk >> $DIRPATH
  sleep 1
done

The script is working fine, but it will not stop! If I press ctrl-C while the script is running, the process will not stop, and I get the following error:

cat: cannot open '/ proc // smaps': no ​​such file or directory

Does anyone know how to avoid this?

+3
source share
2 answers

You should consider the trap function. See this and this .

To catch ctrl-c, you must define a handler, for example:

ctrl_c ()
{
    # Handler for Control + C Trap
    echo ""
    echo "Control + C Caught..."
    exit
}

And then specify that you want to capture it with this handler:

trap ctrl_c SIGINT

As an alternative...

script , &, ,

$ ./your_script.sh &

[ ]:

$ ./your_script.sh &
[1] 5183

( 1). ,

$ kill %1

, , ,

+3
awk -f ./myawkscript.awk /proc/`pidof iBrowser.bin`/smaps >> $DIRPATH \
  || exit 1

script, awk , , pidof - . UUOC.

+1

All Articles