Re: Bash – wait for the user to press any key
Johnny Rosenberg
gurus.knugum at gmail.com
Mon Jun 2 16:54:38 UTC 2014
2014-06-02 17:35 GMT+02:00 John Howe <madlinuxman at hotmail.com>:
> On 01/06/14 23:10, Johnny Rosenberg wrote:
>
> I have something like this:
> while read -r -a Line; do
> …
> …
> done << "$SomeFile"
>
> Since my script are going to do some dangerous things (deleting files –
> hopefully the right ones), I first want to test it by omitting the
> dangerous lines and adding a lot of echo lines, letting me know the values
> of all important variables.
> $SomeFile has a few thousands of lines, so I don't want to go through it
> all, but I would like to see the result of maybe the first 20 lines or so.
>
> So I want the script to stop before looping, letting me study all the
> variables before going to the next line in the file.
>
> But I figure that if I use ”read” to do that, that ”read” command would
> also be fed with $SomeFile, wouldn't it?
>
> So I want something like this:
> while read -r -a Line; do
> …
> …
> echo "Press any key to continue"
> SomeCommand
> done << "$SomeFile"
>
> Ooops, I guess I accidently added a ”<” too much…
>
>
>
>
> I use.........
>
> # init
> function pause(){
> read -p "$*"
> }
>
> do some work here
>
> read -p "Press [Enter] key to continue or Crtl c to exit..."
>
I suppose you mean ”pause” instead of ”read -p”, right?
>
> Hope it helps
>
Maybe it should, but it doesn't seem to work the way I was talking about in
my original post. Here's are my test files:
<File>
# looptest.sh
#!/bin/bash
function pause(){
read -p "$*"
}
while read x; do
echo $x
pause "Press any key to continuea"
done < Information
</File>
<File>
# Information
This
text
is
very
very
interesting
annoying
</File>
<Execution>
$ ./looptest.sh
This
is
very
annoying
$
</Execution>
The text is printed immediately, no pause or anything. So it seems, just
like I said in my original post, like the file (”Information” in this case)
does the input for me. Is there a way to force a read command to read from
the keyboard in a situation like this?
Johnny Rosenberg
> --
> Best Regards
> John
>
> *Windows users take note: all emails from this system are scanned for viruses using ClamAV and the latest definitions*
>
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20140602/d4193388/attachment.html>
More information about the ubuntu-users
mailing list