Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?
Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop. For instance, if I specified that the song is to repeat 5 times, if during the first iteration through the loop, if I use CTRL+c to exit the script, I will have to press CTRL+c four more times before the script will terminate.
My objective is to be able to type 'q' or some character in order to quit the process. I have found limited information about this topic online, and what I have found has not worked for me.
Although my script provides more options than what is listed below, here it is in its most basic form (note - I'm using mplayer to play the audio file):
Does anyone have any recommendations on how I can exit from the script using keyboard input in such a way that I'm not stuck in the loop and that the script actually terminates?
Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?
Thank you chacko193. I read the "trap" manpage and thought that it might work, but perhaps I'm not using it correctly... although it allows CTRL+c to exit the script, it also only allows the audio file to be executed one time; for some reason it won't loop for the specified number of times. Using the same code as in my previous example, I've added the trap signal:
It seems like there should be a way to code this programatically rather than relying on another application like trap, but I just don't see it. I tried using the solutions that were suggested at the link below but only ended up with an infinite loop. (BTW - I keep trying to wrap this link in URL tags but for some reason vBulletin keeps telling me that I have reached the limit of having 5 URL's in my post so I'm instead wrapping it in CODE tags.)
I'm not familiar with the bash shell but that line
just doesn't look right. That is telling the shell to only execute "mplayer" when an interrupt 0 is captured.
Thanks port43! Although this method does not accomplish what I had hoped to do in terms of "listening" for user input (pressing 'q' for quit), exiting the script with CTRL+c instead will work just fine for me and I'll be perfectly happy with that. The script is working very well now thanks to your input and suggestions. Before today, I had never heard of "trap".
As a musician, I will often times use this script when trying to commit a song or a series of songs to memory. Here's the new version:
You don't need to repeat the trap command if they're identical; just add one at the top of the script. Don't forget to reset it once it's no more needed.
Yep, as RudiC says, move the trap to the top of your script - one trap for the whole script (unless you want to change the way the script behaves at different times with different signals).
Hello,
I am running a bash script to do an rsync back on a computer running MacOS High Sierra. This is the script I am using,
#!/bin/bash
# main backup location, trailing slash included
backup_loc="/Volumes/Archive_Volume/00_macos_backup/"
# generic backup function
function backup {... (12 Replies)
Hi everyone, I'm new here and just a beginner in linux scripting.
Just want to ask for help on this one.
I am trying to create a script that will accept user input (year-month and user/s).
I wanted to have the script to continue running, until the user inputs a DATE and name/s of user/s. ... (2 Replies)
I have the following while loop that I put in a script, demo.sh:
while read rna; do
aawork=$(echo "${rna}" | sed -n -e 's/\(...\)\1 /gp' | sed -f rna.sed)
echo "$aawork" | sed 's/ //g'
echo "$aawork" | tr ' ' '\012' | sort | sed '/^$/d' | uniq -c | sed 's/*\(*\) \(.*\)/\2: \... (3 Replies)
Heyas,
Since this question (similar) occur every now and then, and given the fact i was thinking about it just recently (1-2 weeks) anyway, i started to write something :p
The last point for motivation was... (17 Replies)
I want to run a script that calls remote ssh and if it gets hung, I want to be able to kill that part of the script and run another command in the script
for example I have a statement like this:
if ];
then
export tapes=$(for tape in $(su - nacct -c 'ssh remote1 "cat... (1 Reply)
I'm writing a bunch of scripts to automatically configure Ubuntu and I want to run the code below to remove the white dots from the login screen:
sudo xhost +SI:localuser:lightdm
sudo su lightdm -s /bin/bash
gsettings set com.canonical.unity-greeter draw-grid false
The problem is that... (3 Replies)
Hi Sir,
I am just learning bash scripting and I came across a challenge.
I need to input F11 to a script among many text inputs.
For all the text inputs i did following.
# sh test.sh < input.txt
where input.txt contains all the text inputs in new lines.
This worked fine until i... (1 Reply)
Need help in scripting . Below is the situation and need your inputs
Checking all the processes, scripts running time based on user input time . Below Example
ps -aef -o user,pid,etime,stime,args| grep sleep
<user> 28995 01:24 14:14:39 sleep 120
<user> 29385 00:52 14:15:10... (8 Replies)
I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files:
for i in 250.*; do
|| awk... (4 Replies)