Updating value from new last line of tempfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Updating value from new last line of tempfile
# 1  
Old 05-31-2015
Updating value from new last line of tempfile

Heyas

A variable within a loop doesnt get updated - eventhough the 'inputfile' changes.

I have a tempfile ($TMP.playstatus), which contains this data:
Code:
   0.11 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0   
   0.14 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0   
   0.16 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0   
   0.19 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0   
   0.23 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0   
   0.26 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0   
   0.28 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0   
   0.31 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0   
   0.35 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0

Where constantly a new line is appended.

I have this semi working:
Code:
	$SHELL "$TMP" &
	PID=$!
	sleep 1.4
	while ps $PID > /dev/zero
	do	CUR=$(tail -n1 $TMP.playstatus|awk '{print $1}')
# Deleted stuff
		echo tui-progress -bm "$PTS" -c "$CUR" "$CUR/$PT"
	done
	return

Deleted stuff (previous tries):
Code:
	#CUR=$($AWK '/M-A:/ {print $1}' $TMP.playstatus )
		#M-A:
		#echo "--> $CUR <--"
		#tail -n1 $TMP.playstatus
		#CUR=${CUR/\/*}
		#echo $CUR
		#$AWK '/M\-A:/ BEGINN {print $1}' $TMP.playstatus
	#	[ -z "$CUR" ] && \
	#		pkill ffplay && \
	#		tui-printf -S 1 "Cant read current playtime from $video, aborting..." && \
	#		tail -n1 $TMP.playstatus && \
	#		echo "---" && cat "$TMP" && \
	#		exit 1
		#exit
		#sh -x \

Regardless of what i try, the $CUR-value is read properly only the first time - so it seems to me, but afterwards it remains the same/doesnt change...
As in, beeing spamed with:
Code:
tui-progress -bm 3526 -c 0.04 0.04 /58:46
tui-progress -bm 3526 -c 0.04 0.04 /58:46
tui-progress -bm 3526 -c 0.04 0.04 /58:46

OR, almost the same:
Code:
tui-progress -bm 3526 -c 0.01 0.01/58:46
tui-progress -bm 3526 -c 0.01 0.01/58:46
tui-progress -bm 3526 -c 0.01 0.01/58:46

To my understanding the variable $CUR should be updated just before its echo'd within each loop.

Any advice/ideas please?
Thank you in advance

---------- Post updated at 11:14 ---------- Previous update was at 10:38 ----------

Maybe this different output helps:
Code:
		sleep 0.4
		while ps $PID > /dev/zero
		do 	CUR=$(tail -n1 "$STATUS"|$AWK '{print $1}')
			>"$STATUS"
			echo tui-progress -bm "$PTS" -c "$CUR" "$CUR/$PT"
			sleep 0.7
		done

Outputs as:
Code:
tui-progress -bm 3526 -c 0.01 0.01/58:46
tui-progress -bm 3526 -c  /58:46
tui-progress -bm 3526 -c  /58:46

Since the background job is told to redirect its output to $STATUS i thought after the cleaning the new lines would still be added.
Even when i DELETE the tempfile ($STATUS), it doesnt read a value while the files doesnt exist, and after... it jumps back to the first value (0.01), as if its 0.01 was even on the lowest line...

SmilieSmilie

Last edited by sea; 05-31-2015 at 05:50 AM..
# 2  
Old 05-31-2015
In your first sample the sleep statement is missing, which is essential in this situation, otherwise the script checks uncontrolled number of time per second and unnecessarily strains the processor core...

In your 2nd sample the background job does not redirect its output to $STATUS.
Code:
>"$STATUS"

means truncate file "$STATUS", which empties the file, without deleting it.



---
Also, it is unusual to redirect output to /dev/zero, which is an input device, consider using /dev/null instead..

Last edited by Scrutinizer; 05-31-2015 at 04:19 PM..
# 3  
Old 05-31-2015
I can't reproduce that. Producing your temp file in background, and running your semi-working script snippet, I get
Code:
tui-progress -bm 3526 -c 0.109 0.109/0.01/58:46
tui-progress -bm 3526 -c 0.110 0.110/0.01/58:46
tui-progress -bm 3526 -c 0.111 0.111/0.01/58:46
tui-progress -bm 3526 -c 0.113 0.113/0.01/58:46
tui-progress -bm 3526 -c 0.114 0.114/0.01/58:46
tui-progress -bm 3526 -c 0.115 0.115/0.01/58:46
tui-progress -bm 3526 -c 0.116 0.116/0.01/58:46
tui-progress -bm 3526 -c 0.118 0.118/0.01/58:46
tui-progress -bm 3526 -c 0.119 0.119/0.01/58:46
etc.

What you should NOT do is redirect your stdout into your input file (reading from and redirecting into $STATUS), be it written by another process or not. If not, you'll erase it and then read only the lines back that have just been written by yourself. If written, you'll get a random mixup of lines by yourself and lines by the other process (what seems to be your sample output).

---------- Post updated at 21:54 ---------- Previous update was at 21:45 ----------

You can help me with a hint/trick: How did you get those <TAB> chars into the code text?
# 4  
Old 05-31-2015
@ The tab chars are in my code, copy paste preserves them usualy.

Thanks for your feedback, sadly i seem to apply this somehow wrong...

Function:
Code:
	PlayStatus() { # FILE
	# Prints the extra status play bar
	# Time as Progress, yay 
		# Vars
		PT=$(PlayTime)		# Get nice displayed playtime
		PTS=$(PlayTimeSecs)	# Get playtime as seconds
		[ "00" = "${PT:0:2}" ] && \
			PT="${PT/00:}"	# Cut off 'empty' leading hours
		PT="${PT/.*}"		# Cut off miliseconds
		STATUS="$TMP.playstatus"
		
		# Make required changes at the command file
		$SED s,'v quiet','hide_banner',g -i "$TMP"
		#$SED s,"||","1\>$STATUS 2\>$STATUS ||",g -i "$TMP"
		$SED s,"||"," 2\>$STATUS ||",g -i "$TMP"
		
		# Start job
		$SHELL "$TMP" &
		PID=$!
		sleep 0.7
		
		# TMP command
		tui-title "DIV"
		tui-cat $TMP
		tui-title "DIV"
		
		# Print information line
		while ps $PID > /dev/null
		do	CUR=$(tail -n1 "$STATUS"|awk '{print $1}')
			echo tui-progress -bm "$PTS" -c "$CUR" "$CUR/$PT"
			sleep 0.7
		done
		return
}

sh -x : output, still doesnt update the $CUR variable Smilie
Code:
+++ echo 00:58:46.03
++ str_work=00:58:46.03
++ base=46.03
++ mins=00:58
++ hours=00
++ H=0
++ M=3480
++ echo 46.03 3480 0
++ awk '{print int ($1 + $2 + $3)}'
+ PTS=3526
+ '[' 00 = 00 ']'
+ PT=58:46.03
+ PT=58:46
+ STATUS=/home/sea/.cache//vhs.tmp.playstatus
+ sed 's,v quiet,hide_banner,g' -i /home/sea/.cache//vhs.tmp
+ sed 's,||, 2\>/home/sea/.cache//vhs.tmp.playstatus ||,g' -i /home/sea/.cache//vhs.tmp
+ PID=6890
+ sleep 0.7
+ /bin/bash /home/sea/.cache//vhs.tmp
+ tui-title DIV
# |                                                      DIV                                                       | #
+ tui-cat /home/sea/.cache//vhs.tmp
# | ffplay -hide_banner -window_title "VHS (2.1.6) : Play audio File : joined_files.mp                             | #
# |                             3" -i "joined_files.mp3" -nodisp  2>/home/sea/.cache//vhs.tmp.playstatus || exit 1 | #
+ tui-title DIV
# |                                                      DIV                                                       | #
+ ps 6890
++ tail -n1 /home/sea/.cache//vhs.tmp.playstatus
++ awk '{print $1}'
+ CUR=0.01
+ echo tui-progress -bm 3526 -c 0.01 0.01/58:46
tui-progress -bm 3526 -c 0.01 0.01/58:46
+ sleep 0.7
+ ps 6890
++ tail -n1 /home/sea/.cache//vhs.tmp.playstatus
++ awk '{print $1}'
+ CUR=0.01
+ echo tui-progress -bm 3526 -c 0.01 0.01/58:46
tui-progress -bm 3526 -c 0.01 0.01/58:46
+ sleep 0.7
+ ps 6890
++ tail -n1 /home/sea/.cache//vhs.tmp.playstatus
++ awk '{print $1}'
+ CUR=0.01
+ echo tui-progress -bm 3526 -c 0.01 0.01/58:46
tui-progress -bm 3526 -c 0.01 0.01/58:46
+ sleep 0.7
^C

What am i doing wrong?
# 5  
Old 05-31-2015
Quote:
Originally Posted by RudiC
... ... ...

You can help me with a hint/trick: How did you get those <TAB> chars into the code text?
If you copy text containing <tab> characters, paste it into a message editing window on this site, and put CODE or ICODE tags around the pasted text, the <tab>s will be preserved.

Note, however, that if you copy text out of a vi editing window, you won't be copying <tab> characters because vi (and vim and other clones) display <space>s on the editing window instead of depending on currently set tab stops. But, if you cat the edited code to your screen and copy the cat output, the <tab>s will be preserved.
# 6  
Old 05-31-2015
OK, i guess we're getting closer.. while its playing in the background, i tried to read the status file manualy:

Code:
0 ~/.cache $ awk '/M-A/ {print $1}'  vhs.tmp.playstatus 
0.01

0 ~/.cache $ grep "M-A" vhs.tmp.playstatus 
 112.85 M-A:  0.000 fd=   0 aq=    4KB vq=    0KB sq=    0B f=0/0

But i just cant explaing the differende ?! Smilie

'Funny' thing is, with grep, it prints all the vars on a single line, and on the manual check, i see them prints over each other.
Inside the script function, the grep filtering causes a 'freeze', it keeps playing, but the code stucks at grep.

Eg:
Code:
while ps $PID > /dev/null
		do	#CUR=$(tail -n1 "$STATUS"|awk '{print $1}')
			#CUR=$($AWK '{print $1}' $STATUS | tail -n 1)
			list=$(awk '{print $1}' $STATUS)
			for l in $list;do CUR=$l;done
			#CUR=$(tail -n1 $STATUS|grep "M-A" )
			#CUR=$(tail -n1 $STATUS|grep "M-A" | awk '{print $1}')
			echo tui-progress -bm "$PTS" -c "$CUR" "$CUR/$PT"
			sleep 0.7
		done

I'd expect for each and every single 'line' here the behaviour RudiC reports Smilie

Last edited by sea; 05-31-2015 at 08:24 PM..
# 7  
Old 05-31-2015
What is the output from:
Code:
ls -l vhs.tmp.playstatus
wc vhs.tmp.playstatus
od -bc vhs.tmp.playstatus

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Ubuntu

16.04 not updating

Just installed the latest version on my hp stream, was tired of windows. Of the few times I've used Linux, this is the first time it won't update. Im connected to the net the updater recognizes that the OS needs 21 updates. But it won't download/install the updates. It also won't load the... (1 Reply)
Discussion started by: DabblingMadman
1 Replies

3. SCO

FoxPro 2.6 and updating UW

I have an app in FP26 with 400 prgs currently en UW7.1.1 thats work perfectly. I have a new server and install UW7.1.4 and FP26. Then I had copy my App to new server. But the next command doesn't work: use database @10,10 get name read I can't write in the get. ... (5 Replies)
Discussion started by: AralVorkosigan
5 Replies

4. Shell Programming and Scripting

Need help for updating the folder

Hello Expert, is there any command through which i can update/replace the folder after every two months. lets say i have folder /usr/local/x_folder created on 01/01/2011 I need to update it on 31/03/2011 Please help me to get the logic :confused: Thanks (3 Replies)
Discussion started by: aks_1902
3 Replies

5. AIX

Updating AIX OS

Hello I have a server whose os level reads 5300-01-00-0000. It has to be upgraded to the latest level which is 5300-12-04-1119. What would I need to do go get the server to this point? Would I need to go through each patch level until I get it to current? If so, would anyone happen to know... (1 Reply)
Discussion started by: stayfuzzy
1 Replies

6. Shell Programming and Scripting

updating a single line by script

hi, I'm trying to add a # to the beginning of the line where the a word is included. and the i want to run the whole original script. and if possible I would prefer that line would stay in the same order in the text file, the line which includes the word. Thanks, (10 Replies)
Discussion started by: ozum
10 Replies

7. Shell Programming and Scripting

Updating a line in a large csv file, with sed/awk?

I have an extremely large csv file that I need to search the second field, and upon matches update the last field... I can pull the line with awk.. but apparently you cant use awk to directly update the file? So im curious if I can use sed to do this... The good news is the field I want to... (5 Replies)
Discussion started by: trey85stang
5 Replies

8. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies
Login or Register to Ask a Question