Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?
# 8  
Old 03-05-2014
That's good to know, thank you. I looked at the man page and did not see how to reset the trap. After some googling, I think that a trap reset might look something like this?

Code:
trap - SIGINT SIGQUIT SIGTSTP

Is that the correct way to reset the trap?

Also, if the script is terminated with CTRL+c (without resetting the trap), the trap settings won't effect other scripts that I run afterwards, will it? Does the trap only apply to the script that it was called from?

Thank you!
# 9  
Old 03-06-2014
Yes, that is how you reset: trap - <value>.

I should point out that in my example I used integer values and yours used the signal name(s); either one is valid. Here are the values I used(kill -l will list other values):

  • SIGHUP 1 death of controlling terminal or process
  • SIGINT 2 <Ctrl><C>
  • SIGQUIT 3 <Ctrl><D>
  • SIGTERM 15 Software termination
If you have properly used the shebang at the top of your script to identify the shell interpreter, ie. #!/bin/ksh then your script is spawned in a subshell and the trap does not need to be reset because when the script execution terminates the trap values are lost.

The point of the reset is if you have something like: trap 'ls -l' 2 (which will cause <Ctrl><C> to do a long listing) for part of your script, you'll might want to put it back with trap - 2 for normalcy.

One last thing, trap is usually used to clean up when a script is aborted so it probably is used more like:
Code:
trap 'rm /tmp/mytempfile.txt /path/to/other/tempfile.txt; exit 2' 1 2 3 15


Last edited by port43; 03-06-2014 at 10:00 AM..
This User Gave Thanks to port43 For This Post:
# 10  
Old 03-06-2014
Thank you all for your assistance - I really appreciate it. Things seem to be running smoothly at this point and are to my liking. Here is the finished version of the script:

Code:
#!/bin/bash

#######################################################################                                                                                      
##                                                                   ##
## This script will play and repeat either one or more audio files   ##
## that are  contained in the current working directory.             ##
## *Script requires that mplayer and figlet are installed.           ##
##      author: Darrin Goodman                                       ##
##      email: hilltopyodeler@gmail.com                              ##
##                                                                   ##
## This program is free software and is available without warranty.  ##
## You are free to redistribute it and/or modify it under the terms  ##
## of the GNU General Public License as published by the             ##
## Free Software Foundation, either version 3 or any later           ##
## version of the license.                                           ##
##                                                                   ##
#######################################################################



## Set up trap so that it will allow you to terminate the script at any time using CTRL+C
## https://www.unix.com/shell-programming-scripting/244861-bash-question-howto-exit-script-user-input-while-process-running-mid-loop.html
trap 'exit 1' 1 2 3 15

###############################################################################
## BEGIN FUNCTIONS SECTION

doWhat()
{
echo -n "Repeat one file (1) or many files (M)? "
read quantity
echo ""
  case $quantity in
    1) oneFile ;;
    m|M) manyFiles ;;
    q|Q) quitski ;;
    *) incorrectEntry ;;
  esac
}

repeatNum()
{
  echo -n "Enter the number of times you would like to play the file(s): "
  read num
  echo ""
}

oneFile()
{
  echo -n "What is the name of the file? "
  read file
  echo ""
  checkFile
  repeatNum
  echo "-------------------------------------------------------------------------------"
  echo "-------------------------------------------------------------------------------"
  echo ""
  for ((i=1; i <= $num; i++))
     do mplayer $file;
  done
  again
}

checkFile()
{
  ## Check to make sure that the file exists
  if [ ! -f $file ]; then
    echo "   Hmmm... [ $file ] - This file does not exist; did you type it correctly?"
    echo ""
    oneFile
  else
    echo ""
  fi
}

manyFiles()
{
  echo -n "Enter the file type or extension (mp3 / ogg / *) "
  read ext
  echo ""
  repeatNum
  echo "-------------------------------------------------------------------------------"
  echo "-------------------------------------------------------------------------------"
  echo ""
  for ((j=1; j <= $num; j++))
     do mplayer *.$ext;
  done
  again
}

incorrectEntry()
{
  echo "     **************************************"
  echo "     *                                    *"
  echo "     *  INVALID ENTRY! Please try again.  *"
  echo "     *                                    *"
  echo "     **************************************"
  echo ""
  doWhat
}

again()
{
  echo "-------------------------------------------------------------------------------"
  echo "-------------------------------------------------------------------------------"
  figlet "Done."
  figlet "What next?"
  echo "-------------------------------------------------------------------------------"
  echo "-------------------------------------------------------------------------------"
  echo ""
  echo -n "What would you like to do now? ....Start over (s) or Quit (q)? "
  echo ""
  read whatNow
  case $whatNow in
    s|S) doWhat ;;
    q|Q) quitski ;;
    *) echo "Huh?" ; again ;;
  esac
}

quitski()
{
  clear
  echo ""
  figlet Good Bye!
  echo ""
  echo ""
  exit
}

## END FUNCTIONS SECTION
###############################################################################

## Clear the screen
clear

figlet "Music Repeater"
echo "-------------------------------------------------------------------------------"
echo "This program will play one or more audio files, however many times you specify."
echo "Exit this script at any time with CTRL+C."
echo ""

## What do you want to do? Repeat one or many files?
doWhat

## End of program; exit
figlet END

# Reset all traps 
trap - SIGINT SIGQUIT SIGTSTP

exit 0

This script suits my needs well. There may be more efficient ways to code this process, so please feel free make appropriate changes and re-post it if you like, and feel free to use it if it benefits you in some way.

Thanks again to those of you who offered assistance with getting the script to terminate with CTRL+c.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Terminal running bash/rsync script does not close with exit (MacOS High SIerra)

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)
Discussion started by: LMHmedchem
12 Replies

2. Shell Programming and Scripting

Help Me. The script should not exit until the user gives an input.

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)
Discussion started by: Helskadi
2 Replies

3. Shell Programming and Scripting

While loop with input in a bash script

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)
Discussion started by: faizlo
3 Replies

4. Shell Programming and Scripting

[BASH] Script to manage background scripts (running, finished, exit code)

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)
Discussion started by: sea
17 Replies

5. Shell Programming and Scripting

Killing a bash process and running the second part of script

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)
Discussion started by: newbie2010
1 Replies

6. Ubuntu

Exit user in bash script

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)
Discussion started by: maerlyngb
3 Replies

7. UNIX for Dummies Questions & Answers

Feed Input to a script running on bash

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)
Discussion started by: gaurav kumar
1 Replies

8. Shell Programming and Scripting

Process running time by taking user input

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)
Discussion started by: ajayram_arya
8 Replies

9. UNIX for Dummies Questions & Answers

Writing a loop to process multiple input files by a shell script

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)
Discussion started by: evelibertine
4 Replies

10. Shell Programming and Scripting

exit script if user input not four characters

#!/usr/bin/bash ###script to input four characters. wxyz echo "input first string" read instring1 echo "input second string" read instring2 ## echo "first string is:" $instring1 echo "second string is:" $instring2 ##IF instring1 or instring2 are NOT 4 characters (xxxx) , exit 1. ##how?? ... (2 Replies)
Discussion started by: ajp7701
2 Replies
Login or Register to Ask a Question