LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2012, 12:28 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Breaking a loop from the keyboard.


Hi: I have a program like this:
Code:
#!/bin/bash

while [ 1 ]; do
   LIST=$(ls *wav)
   for i in $LIST; do 
       play $i
       sleep 10
   done
done
OK. When this script is running, I want to stop it whenever I want, for example with ^C. But ^C wont do. I think maybe substituting something like a read bultin command in the while argument would be a solution. Does read wait for the Return key?
 
Old 01-29-2012, 12:59 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Maybe use signal handler:
Code:
#!/bin/bash

kill_script ()
{
    echo "SIGTERM received, exiting..."
    exit
}

trap kill_script SIGINT SIGTERM

while [ 1 ]; do
   LIST=$(ls *wav)
   for i in $LIST; do 
       play $i
       sleep 10
   done
done
 
1 members found this post helpful.
Old 01-29-2012, 01:47 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thank you very much. Your script is running.
 
Old 01-31-2012, 08:34 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Some suggestions:
  1. Don't parse ls for filenames. The shell's built-in globbing will do the job just fine here.

  2. Use arrays to store lists of items, rather than scalar variables (Or just use globbing patterns directly).

  3. QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS.
    You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-broken by the shell.
    http://mywiki.wooledge.org/Arguments
    http://mywiki.wooledge.org/WordSplitting
    http://mywiki.wooledge.org/Quotes

  4. Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

Code:
#!/bin/bash

kill_script ()
{
    echo "SIGTERM received, exiting..."
    exit
}

trap kill_script SIGINT SIGTERM


while true; do
	list=( *.wav )			# If it's not necessary to regenerate the list
					# every time it loops, leave this step outside.
	for i in "${list[@]}"; do 	# or just use "for i in *.wav" here.
		play "$i"
		sleep 10
	done
done

Last edited by David the H.; 01-31-2012 at 08:35 AM.
 
Old 01-31-2012, 09:05 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Does Ctrl+C work if you put the sleep 10 in the background and wait for it? Like this sample from lm_sensors' fancontrol script (the "Sleep while still handling signals" comment has always intrigued):
Code:
while true
do
    UpdateFanSpeeds
    # Sleep while still handling signals
    sleep $INTERVAL &
    wait $!
done
 
Old 01-31-2012, 09:51 AM   #6
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
David the H. thanks for the links and explanation.
 
Old 01-31-2012, 11:53 AM   #7
jasmine555
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Rep: Reputation: Disabled
I'am using linux fedora and mandrake. So I must forget kbhit(), even if some DOS emulators can run it but I don't want that. I did some seaching about termios and it seem to be very complex to use.
 
Old 01-31-2012, 05:31 PM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by David the H. View Post
Some suggestions:
  1. Don't parse ls for filenames. The shell's built-in globbing will do the job just fine here.

  2. Use arrays to store lists of items, rather than scalar variables (Or just use globbing patterns directly).

  3. QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS.
    You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-broken by the shell.
    http://mywiki.wooledge.org/Arguments
    http://mywiki.wooledge.org/WordSplitting
    http://mywiki.wooledge.org/Quotes

  4. Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

Code:
#!/bin/bash

kill_script ()
{
    echo "SIGTERM received, exiting..."
    exit
}

trap kill_script SIGINT SIGTERM


while true; do
	list=( *.wav )			# If it's not necessary to regenerate the list
					# every time it loops, leave this step outside.
	for i in "${list[@]}"; do 	# or just use "for i in *.wav" here.
		play "$i"
		sleep 10
	done
done
Thank you very much for your tips. About variable substitution I've had long correspondence in the forum. It seems to be a golden rule.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gdb with fork() - not breaking, following multiple child processes in loop TheMac Programming 2 11-02-2011 08:38 AM
problem with comparison and breaking out of loop java xskycamefalling Programming 3 10-12-2010 09:11 AM
help: cin>> in while loop , it reads keyboard only once stefanolima Programming 5 03-12-2010 08:09 AM
Python, breaking out of infinite loop edM Programming 7 05-05-2009 11:27 PM
KDE 3.2 breaking keyboard? eurleif Linux - Software 0 02-15-2004 06:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration