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

**URL:** <https://community.unix.com/t/doing-a-tail-in-a-script-and-then-return-back-and-continue-script/276731>\
**Category:** Shell Programming and Scripting\
**Created:** [November 4, 2010, 12:39pm UTC](https://community.unix.com/t/doing-a-tail-in-a-script-and-then-return-back-and-continue-script/276731 "2010-11-04T12:39:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![markdark](https://community.unix.com/letter_avatar/markdark/32/6_22734026db12803ebb3e059622ac3534.png) [@markdark](https://community.unix.com/u/markdark)\
**Post date:** [November 4, 2010, 12:39pm UTC](https://community.unix.com/t/doing-a-tail-in-a-script-and-then-return-back-and-continue-script/276731/1 "2010-11-04T12:39:18Z")

</div>

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.

```php
#!/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

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/6_22734026db12803ebb3e059622ac3534.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [November 4, 2010, 1:05pm UTC](https://community.unix.com/t/doing-a-tail-in-a-script-and-then-return-back-and-continue-script/276731/2 "2010-11-04T13:05:27Z")

</div>

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.

```nohighlight
       + 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.

```

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_22734026db12803ebb3e059622ac3534.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [November 4, 2010, 1:10pm UTC](https://community.unix.com/t/doing-a-tail-in-a-script-and-then-return-back-and-continue-script/276731/3 "2010-11-04T13:10:15Z")

</div>

Modify your doTail() function :

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

```

---

<div class="post-metadata">

**Author:** ![markdark](https://community.unix.com/letter_avatar/markdark/32/6_22734026db12803ebb3e059622ac3534.png) [@markdark](https://community.unix.com/u/markdark)\
**Post date:** [November 4, 2010, 1:36pm UTC](https://community.unix.com/t/doing-a-tail-in-a-script-and-then-return-back-and-continue-script/276731/4 "2010-11-04T13:36:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lakris](https://community.unix.com/letter_avatar/lakris/32/6_22734026db12803ebb3e059622ac3534.png) [@Lakris](https://community.unix.com/u/Lakris)\
**Post date:** [November 4, 2010, 3:05pm UTC](https://community.unix.com/t/doing-a-tail-in-a-script-and-then-return-back-and-continue-script/276731/5 "2010-11-04T15:05:54Z")

</div>

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

```nohighlight
less +F -S 

```

instead of tail (or more).

Best regards,  
Lakris
