Doing a tail in a script and then return back and continue script

Hello all,

I am trying to do a tail in a script. But when I quit the tail my script quits also. This is not what I want. I am struggling to get this done.

#!/bin/bash
 
askFile() {
  echo -n "Enter file: "
  read FILE
}
 
doTail() {
  tail -F "${1}"
}
 
askFile
doTail "${FILE}"
askFile
doTail "${FILE}"

Above is just a simple example what I am trying to do. As you see I first ask for a file. Then I do a tail on this file. If you now quit the tail by entering CTRL+C the total script stops. But I want it to ask for a file again.

Does anyone know how to do this?

Thanks in advance for any help.

Mark

You need to trap SIGINT in your script so you can prevent the shell default handling. See man ksh look for trap, below, man signal and man kill.

       + trap [ -p ] [ action ] [ sig ] ...
	      The  -p  option causes the trap action associated with each trap
	      as specified by the arguments to	be  printed  with  appropriate
	      quoting.	 Otherwise,  action will be processed as if it were an
	      argument to eval when the shell receives	signal(s)  sig.   Each
	      sig can be given as a number or as the name of the signal.  Trap
	      commands are executed in order of signal number.	Any attempt to
	      set  a trap on a signal that was ignored on entry to the current
	      shell is ineffective.  If action is omitted and the first sig is
	      a  number,  or if action is -, then the trap(s) for each sig are
	      reset to their original values.  If action is  the  null	string
	      then  this signal is ignored by the shell and by the commands it
	      invokes.	If sig is ERR then action will be executed whenever  a
	      command has a non-zero exit status.  If sig is DEBUG then action
	      will be executed before each command.  The variable  .sh.command
	      will  contain  the  contents  of	the  current command line when
	      action is running.  If the exit status of the trap is 2 the com-
	      mand  will  not  be executed.  If the exit status of the trap is
	      255 and inside a function or a dot script, the function  or  dot
	      script  will return.  If sig is 0 or EXIT and the trap statement
	      is executed inside the body of a function defined with the func-
	      tion  name syntax, then the command action is executed after the
	      function completes.  If sig is 0 or EXIT for a trap set  outside
	      any  function  then  the command action is executed on exit from
	      the shell.  If sig is KEYBD, then action will be executed  when-
	      ever  a key is read while in emacs, gmacs, or vi mode.  The trap
	      command with no arguments prints a list of  commands  associated
	      with each signal number.
1 Like

Modify your doTail() function :

doTail() {
while :
do
     trap break INT
     tail -F "${1}"
done
}
1 Like

Thanks for your reply. I got it working now. Didn't know the trap command. Will have a look into that.

Hi,
another solution would be to use the tail-function of less, if You have it, which I often recommend as a more flexible alternative. And at least on my machines, the Ctrl-C does not "travel back" to the originating script. It will require an extra keypress (q for quit) but it gives a lot more options for searching and browsing the file. Just use

less +F -S 

instead of tail (or more).

Best regards,
Lakris