BashScripts.org
http://bashscripts.org/forum/

loop until interrupted
http://bashscripts.org/forum/viewtopic.php?f=8&t=1491
Page 1 of 1

Author:  blendmaster345 [ Fri Nov 11, 2011 3:04 pm ]
Post subject:  loop until interrupted

I need a script that will loop until interrupted by a keypress form the parameters.
something like this:
Code:
SCRIPT c echo "loop"
loop
loop
loop
loop
loop
loop
loop
c
loop ended

Author:  jsz [ Fri Nov 11, 2011 4:39 pm ]
Post subject:  Re: loop until interrupted

Using stty(1) and trap(0):

Code:
#!/bin/bash                                                                                                                   
if ((! $#)); then
    printf 'usage: %s <intr_char> <command>\n' "${0##*/}" >&2
    exit 1
fi

if ((${#1} != 1)); then
    echo 'error: interrupt character too long.' >&2
    exit 1
elif [[ $1 != *[[:alpha:]]* ]]; then
    echo 'error: interrupt character must be a letter.' >&2
    exit 1
fi

stty intr "$1"
trap 'echo loop ended; stty intr ^C; exit' SIGINT

shift
if ((! $#)); then
    echo 'warning: no command specified.' >&2
fi

while :; do
    "[email protected]"
done

Author:  blendmaster345 [ Fri Nov 11, 2011 5:00 pm ]
Post subject:  Re: loop until interrupted

[email protected]:~/scripts$ ./loop
./loop: line 10: syntax error near unexpected token `elif'
'/loop: line 10: `elif [[ $1 != *[[:alpha:]]* ]]; then

Author:  jsz [ Fri Nov 11, 2011 5:25 pm ]
Post subject:  Re: loop until interrupted

I can't reproduce the error you're getting...

It should've output this:
Code:
usage: loop <intr_char> <command>

even if there were syntax errors or control characters around elif...

Are you missing the ; then in:
Code:
if ((${#1} != 1)); then

?

If the answer is yes, then you haven't typed:
Code:
./loop

only.

Author:  blendmaster345 [ Fri Nov 11, 2011 5:35 pm ]
Post subject:  Re: loop until interrupted

I realized it had DOS encoding for some reason and I fixed it. I retried it with the -x parrameter to #! /bin/bash and I got
Code:
[email protected]:~/scripts$ ./intloop c echo "hi"
+ printf 'usage: %s <intr_char> <command>\n' bash
usage: bash <intr_char> <command>
+ exit 1

Author:  jsz [ Fri Nov 11, 2011 5:52 pm ]
Post subject:  Re: loop until interrupted

How weird... the first condition is like... ignored, and there is no error.
Which bash(1)'s version are you using?

Try the POSIX sh(1) version:

Code:
#!/bin/sh                                                                                                                     
if [ $# -eq 0 ]; then
    echo 'usage: <intr_char> <command>' >&2
    exit 1
fi

if [ "${#1}" -ne 1 ]; then
    echo 'error: interrupt character too long.' >&2
    exit 1
fi

case $1 in
    *[![:alpha:]]*)
        echo 'error: interrupt character must be a letter.' >&2
        exit 1
esac

stty intr "$1"
trap 'echo loop ended; stty intr ^C; exit' 2

shift
if [ $# -eq 0 ]; then
    echo 'warning: no command specified.' >&2
fi

while :; do
    "[email protected]"
done

Author:  blendmaster345 [ Fri Nov 11, 2011 7:20 pm ]
Post subject:  Re: loop until interrupted

I'm using Ubuntu 11.04's BASH interpreter.
bash --version gives me
Code:
GNU bash, version 4.2.8(1)-release (i686-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Author:  choroba [ Sat Nov 12, 2011 4:50 am ]
Post subject:  Re: loop until interrupted

Code:
char=''
while [[ $char != c ]] ; do
   read -t.01 -n1 char
   echo -n . # Do your loop
done

Author:  jsz [ Sat Nov 12, 2011 7:03 am ]
Post subject:  Re: loop until interrupted

blendmaster345 wrote:
I'm using Ubuntu 11.04's BASH interpreter.
bash --version gives me
Code:
GNU bash, version 4.2.8(1)-release (i686-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.



Well, it's fine. It should works!

I'd suggest checking whether there are control characters again,
and make sure that the syntax is correct.

Also, does the POSIX sh(1) version work?

Author:  blendmaster345 [ Sun Nov 13, 2011 11:58 am ]
Post subject:  Re: loop until interrupted

choroba wrote:
Code:
char=''
while [[ $char != c ]] ; do
   read -t.01 -n1 char
   echo -n . # Do your loop
done

the c thing was only an example. I want you to be able to use any key.

Author:  jsz [ Sun Nov 13, 2011 12:41 pm ]
Post subject:  Re: loop until interrupted

@choroba: Does my solution work for you? I don't understand why it won't for blendmaster345.

----

blendmaster345 wrote:
choroba wrote:
Code:
char=''
while [[ $char != c ]] ; do
   read -t.01 -n1 char
   echo -n . # Do your loop
done


the c thing was only an example. I want you to be able to use any key.


Following choroba's idea:

Code:
#!/bin/bash

if ((! $#)); then
    echo 'usage: <intr_char> <command>' >&2
    exit 1
fi

intr_char=$1

shift
if ((! $#)); then
    echo 'warning: no command specified.' >&2
fi

while [[ $input != $intr_char ]] ; do
    read -n 1 -t .01 input
    "[email protected]"
done


But this is less efficient.
You'll sometimes have to type « c » multiple times, because it relies on read(0)'s timeout.

You also didn't answer me.

Author:  blendmaster345 [ Sun Nov 13, 2011 1:27 pm ]
Post subject:  Re: loop until interrupted

Oh, sorry. I'm not sure. Like I said, I am using Ubuntu 11.04's default terminal configuration.

Author:  choroba [ Sun Nov 13, 2011 2:16 pm ]
Post subject:  Re: loop until interrupted

jsz wrote:
@choroba: Does my solution work for you? I don't understand why it won't for blendmaster345.

Yes, both the solutions work (bash 4.1.7, openSUSE 11.3).

jsz wrote:
You'll sometimes have to type « c » multiple times, because it relies on read(0)'s timeout.

I was able to reproduce the problem only with very simple commands: if the command inside the loop took some time (more than .1s), I was not able to reproduce it.

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com