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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Doing a tail in a script and then return back and continue script
# 1  
Old 11-04-2010
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.

PHP Code:
#!/bin/bash
 
askFile() {
  echo -
"Enter file: "
  
read FILE
}
 
doTail() {
  
tail -"${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
# 2  
Old 11-04-2010
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.
Code:
       + 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.

This User Gave Thanks to DGPickett For This Post:
# 3  
Old 11-04-2010
Modify your doTail() function :

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


Last edited by ctsgnb; 11-04-2010 at 02:32 PM.. Reason: indentation ... to prevent from Scruti slapping ;)
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 11-04-2010
Thanks for your reply. I got it working now. Didn't know the trap command. Will have a look into that.
# 5  
Old 11-04-2010
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

Code:
less +F -S

instead of tail (or more).

Best regards,
Lakris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to continue shell script after exit 0?

Hi, I am writing a shell script where I am sourcing other shell script in that script I have mention exit 0 due to that it is not continue the first script. Except doing any changes to source script is there any way I can continue the my first script. (3 Replies)
Discussion started by: sonujatav
3 Replies

2. Shell Programming and Scripting

solaris create password in a script and continue

:eek:Below is my code to create a user account but it doesn't take a password automatically. I have to run the password command seperately to do this What I want to do is to be able to accept the password in a script. In linux with the "useradd' command you can give the "-p" flag to accept the... (3 Replies)
Discussion started by: slufoot80
3 Replies

3. Shell Programming and Scripting

prevent multiple tail in back ground

Dears i have a scrip run in unix that need to use the tail -f command, as below: DATE=`date '+%m%d%y'` tail -f /var/messages | grep "start" >> /export/logs/start_${DATE}.out but the problem that the tail -f will be stop and working in the background in then end of the day (23:59:59)... (0 Replies)
Discussion started by: thehero
0 Replies

4. Shell Programming and Scripting

tail -f in Case not getting back to select choice

Hi all, Im trying to do multiple things in one script.. firstly ...i am trying to call another script in one script... secondly i am redirecting the output of second script in one file. thirdly i am also using tail -f after the calling of 2nd script. fourthly, all this is implemented in case... (6 Replies)
Discussion started by: Kartik.sh
6 Replies

5. Shell Programming and Scripting

How to continue running a script while offline?

Hi there, I'm not really stranger to Linux and shell scripting but I am to servers. Anyway, I usually run scripts on a shared science machine, accessible via ssh. My scripts are usually run with mpi, e.g. mpirun -np 16 ./my_script the things after entering the science machine and running... (6 Replies)
Discussion started by: matteo86
6 Replies

6. Shell Programming and Scripting

Restart and then continue script

How can I get a script to complete a update, varifiy completion, resboot, and continue with script? Is it possbile to get script to add itself to the "startup application" list #!/bin/bash clear sudo apt-get update #Verify/test the update completed #Reboot #Start/comtinue... (9 Replies)
Discussion started by: wolfgangcs
9 Replies

7. Shell Programming and Scripting

Issue with Error handling,not able to continue the script further

Hi, I am trying to write a script to cleanup files in a log directory .. cd log find Datk** -mtime +7 -exec rm -f {} \; 2> /dev/null Have used the above to clean up files in log directory more then 7 days older. The file can be something like ( auto-generate by some processes and... (2 Replies)
Discussion started by: nss280
2 Replies

8. Shell Programming and Scripting

How to continue script if right word is not entered?

Hello, I am writing a script and in this script, I want to be able to have the script continue running if the correct word is not entered... Here is an excerpt from me script: read request if ; then echo "You have asked for the System Temperature..." cat... (1 Reply)
Discussion started by: niconico96
1 Replies

9. Shell Programming and Scripting

continue line in perl script

HI , I am new to the perl , I am using a if condition and in that if condition i am checking 7 variables value. so it continue to second line .And if i user "\" for the continue line it showing error. Example : if(a >9 || b>8 || c> 10 \ d > 11) { print(); } The above statement is... (3 Replies)
Discussion started by: julirani
3 Replies

10. Shell Programming and Scripting

Continue Script when File Found

Hello All, I am trying to write a script that will only continue executing my script if a file exits. I know the directory of the file, so its just a matter of seeing if the file exists yet. If the file has not yet been created, I want the script to wait 10 minutes (600 seconds) and try again.... (7 Replies)
Discussion started by: Jose Miguel
7 Replies
Login or Register to Ask a Question