Exit to while read


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit to while read
# 8  
Old 11-11-2017
You haven't said what shell or operating system you're using. When I try the following script on a macBook Pro running macOS Sierra version 10.12.6 using ksh (version sh (AT&T Research) 93u+ 2012-08-01), I think it does what you want:
Code:
#!/bin/ksh
logfile=/tmp/test.log

func_monitoring() {
	printf '	Monitoring "%s"...\n' "$logfile"
	tail -f -n0 "$logfile" | 
	while read line
	do	printf '(func_monitoring read) %s\n' "$line"
		if [ "$line" = "pattern1" ]
		then	echo "patter1"
			return
		elif [ "$line" = "pattern2" ]
		then	echo "patter2"
			return
		fi
	done
}

func_menu() {
	while :
	do	clear
		printf "%s

			[1] Monitor the log

			[2] Exit


		Please Select Number [1-2]: " "$directions"

		directions=
		read -n1 yourch
		case $yourch in
		(1)	echo
			func_monitoring
			sleep 5;;
		(2)	clear
			echo Goodbye.
			exit 0;;
		(*)	directions='	Please select choice 1 or 2';;
		esac
	done
}
func_menu

it loops on the read in func_monitoring until a line is found that matches one of the two given strings. Then it prints a line and sleeps for 5 seconds. And then it returns to the menu display loop in func_menu.

When I try running the same code with bash (version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)) func_monitoring doesn't return to func_menu until it has read another line from your log file after finding a matching line (which I assume is why you're trying to kill the tail after finding a matching line).
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 11-12-2017
Wouldn't a case statement lend itself to evaluating the patterns in func_monitoring?
# 10  
Old 11-12-2017
Quote:
Originally Posted by RudiC
Wouldn't a case statement lend itself to evaluating the patterns in func_monitoring?
Yes. Using case patterns to match lines instead of using test's = operator would help if matching words on a line instead of looking for exact matches to a complete line is the desired goal.

But, making that change won't affect when tail terminates after the remainder of the pipeline terminates. For that we need to know what shell is being used and what version of that shell. I've shown a way to make it work with the Korn shell if I understand what lxdorney is trying to do correctly (and I'm not sure that I do). I believe that some versions of bash have an option to make the last process in a pipeline the process group leader of that pipeline (like ksh does) instead of the first process in the pipeline (like bash does by default), but I'm not going to try to research ways to do this in every possible shell when lxdorney could tell us what version of what shell is being used so we could reasonably limit our research.
# 11  
Old 11-12-2017
OS: centos 6.9
# 12  
Old 11-12-2017
Quote:
Originally Posted by lxdorney
OS: centos 6.9
Great. Now we know what operating system you're using.

But, since i neither use nor have access to a system that is running that OS, I still don't know what what shell is installed as /bin/sh on your system. What output do you get when you run the command sh --version?

What output do you get when you run the command bash --version? And, what output do you get when you run the command bash --lastpipe?

Do you have ksh installed on your system? What output do you get from the command ksh --version?

I provided a script in post #8 in this thread. Did you try installing it and executing it with ksh? If so, what happened? If not, why not?
# 13  
Old 11-12-2017
Code:
GNU bash, version 4.1.2(2)-release-(x86_64-redhat-linux-gnu)
Usage:  sh [GNU long option] [option] ...
        sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --rpm-requires
        --restricted
        --verbose
        --version
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option
Type `sh -c "help set"' for more information about shell options.
Type `sh -c help' for more information about shell builtin commands.

---------- Post updated at 09:07 PM ---------- Previous update was at 09:02 PM ----------

I tried the script and execute this command to append the test.log
Code:
echo "pattern1" >>/tmp/test.log

but same is not go back to menu and when try to execute again the
Code:
echo "pattern2" >>/tmp/test.log

, then is back to the menu.


Sorry my apologies, 2 append with any patterns, "pattern1 or pattern2" is working, I will edit my reply

Last edited by lxdorney; 11-12-2017 at 10:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX Pipe -Exit when there are no bytes to read

Hi All, I'm creating a program which reads millions of bytes from the PIPE and do some processing. As the data is more, the idea is to read the pipe parallely. Sun Solaris 8 See the code below: #!/bin/sh MAXTHREAD=30 awk '{print $1}' metadata.csv > nvpipe & while do ... (3 Replies)
Discussion started by: mr_manii
3 Replies

2. Shell Programming and Scripting

Read exit value from Shell script

Hi all, I have a situation to read exit value of a command (not exit code) and process further. bash-4.2$ returnvalue=`ssh DOMAIN\\\\user1@10.7.7.68 'cmd /c "del C:\Users\user1\db_test.bak"'` Could Not Find C:\Users\user1\db_test.bak bash-4.2$ echo $? 0 bash-4.2$ echo $returnvalue... (3 Replies)
Discussion started by: baluchen
3 Replies

3. Shell Programming and Scripting

Need the difference between exit 1 & exit 7

Hi In one of the script I am seeing some thing like exit 7,exit 1,exit 2,exit 3,exit 9,exit6.What is the difference between all of this exit.Can anyone help here please (3 Replies)
Discussion started by: ginrkf
3 Replies

4. UNIX for Advanced & Expert Users

Timing READ command - any key to exit

Hello everyone, I would like some help on an issue I have related to the read command with ksh93 (Unix AIX). I want to implement a 'press any key to exit' scenario. I have the following code: STTY=$(stty -g) if ;then stty -echo -icanon time 0 min 0 fi k="" while ];do read k #... (5 Replies)
Discussion started by: gio001
5 Replies

5. UNIX for Advanced & Expert Users

Returning exit code from a while read $inputline

Hi searched hi and wide for this with no luck. Maintaining a bash script that #!/usr/bin/bash #does some stuff like setting env etc. f_do_dbwork ... .. #Now I want to exit with the value of $err however $err is re-initialised to 0 on exiting the function Always 0 .... ... (3 Replies)
Discussion started by: StevePr
3 Replies

6. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

7. Shell Programming and Scripting

urgent -read exit status

i have written a shell script that invokes main class of a java prg. the java prg does a System.exit(0) or (1) based on condition. how can i read or check the status in unix.... (4 Replies)
Discussion started by: iamcool
4 Replies

8. UNIX for Dummies Questions & Answers

what is meaning of exit(0) and exit(1)

can u tell me what is the meaning of exit(0),exit(1),exit(2) what is diff amonng these. Amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

9. Programming

exit(0) versus exit(1)

What is the difference between using exit(0) and exit(1) to exit a program? Which should I use? (9 Replies)
Discussion started by: enuenu
9 Replies

10. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies
Login or Register to Ask a Question