Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Bash: replace the former output with the new one

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Bash: replace the former output with the new one

#1 Post by kiyop »

hakerdefo asked a question by private message to me as follows:
hakerdefo wrote: I need your help

Sent: 2015-08-05 22:02
by hakerdefo
Hi there kiyop,
I need your help regarding a bash script. I'm working on a script and in that script I need to parse 'StreamTitle' from shoutcast-icecast radio streams. I'm using mpg123 to play the streams. Now with the help of grep and sed I've been able to extract the 'StreamTitle' but here comes my problem. As the track changes in the radio stream the last played track remains on the screen and currently playing track appears below it. Like this,

Code: Select all

The Rolling Stones - Jumpin Jack Flash
Bruce Springsteen - Cover Me
In the above example 'The Rolling Stones - Jumpin Jack Flash' was the track last played and 'Bruce Springsteen - Cover Me' is the currently playing track. As we continue to play the radio stream the list will keep growing. Now I want only the currently playing track displayed in terminal. I don't want the previously played tracks to appear. Is it possible via pure bash solution? Any ideas?
Here is the bash one liner I'm currently using,

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g"
Thanks in advance!
Cheers!!!
I answered as follows
kiyop wrote:Re: I need your help

Sent: 2015-08-07 18:44
by kiyop
If you want to display only the last line of standard output (stdout), add

Code: Select all

|tail -n1
such as

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g"|tail -n1
Refer

Code: Select all

man tail
If you want to display only the 2nd line of the standard output, use sed

Code: Select all

|sed -n 2p
Refer

Code: Select all

man sed
Then, hakerdefo replied as follows:
hakerdefo wrote:Re: I need your help

Sent: 2015-08-08 00:41
From: hakerdefo
To: kiyop
Thanks for the idea but I already tried it! It didn't work. Adding 'tail -n1' prevents anything from appearing on stdout. Only when you stops mpg123, title of the song that was playing appears on the screen.
Did you try it? I tried it across three different distros and different terminal emulators and the result remains the same.
Thus, I replied as follows:
kiyop wrote:Re: I need your help

Sent: 2015-08-08 13:26
by kiyop
I have never used mpg123 nor http://streaming.radionomy.com/-1ClassicRock.
So I do not know how the output of the following

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null
comes.
It outputs the info of a song whenever it starts playing a new song, doesn't it?
I cannot understand what you want to do.
Where do you execute the command? On a terminal window on GUI? Or on a tty?
Do you want to delete the info of the former song which has been displayed on a terminal, when a new song is started to be played?
If so, you can use "clear" command to delete all the info on the terminal.
But another person may know better way.
Why not posting this question of yours on one of debian user forums directly, instead of asking to me?
Could you please help hakerdefo?
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#2 Post by kiyop »

How about the following?

Code: Select all

refreshsecond=1
mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g" > temporarylogfile &
pid=$(ps|grep mpg123|tail -n1)
while [ -n "$(ps|grep "$pid")" ];do
a=$(tail -n1 temporarylogfile)
clear
echo $a
echo $a > temporarylogfile
sleep $refreshsecond
done
rm -r temporarylogfile
It will delete temporarylogfile in the current directory.

ADDED at Mon Aug 10 10:53:56 JST 2015;

The above
pid=$(ps|grep mpg123|tail -n1)
should be replaced with
pid=$(ps a|grep mpg123|tail -n1)

ADDED at Mon Aug 10 18:53:36 JST 2015;

The above
while [ -n "$(ps|grep "$pid")" ];do
should be replaced with
while [ -n "$(ps a|grep "$pid")" ];do

ADDED at Mon Aug 10 19:48:41 JST 2015;

The above
pid=$(ps|grep mpg123|tail -n1)
and
while [ -n "$(ps|grep "$pid")" ];do
should be replaced with
pid=$(ps a|grep -v grep|grep mpg123|tail -n1|sed -e "s/^ *//" -e "s/ .*//")
and
while [ -n "$(ps a|grep "^ *$pid")" ];do
Last edited by kiyop on 2015-08-10 10:46, edited 4 times in total.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

User avatar
hakerdefo
Posts: 258
Joined: 2014-05-05 05:31

Re: Bash: replace the former output with the new one

#3 Post by hakerdefo »

Thanks Kiyop for posting this on my behalf. To recap my question,
I'm using mpg123 to play radio streams like this one,
http://streaming.radionomy.com/-1ClassicRock
I'm running mpg123 in terminal emulator to play the stream. By default the output looks like following,

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
	version 1.14.4; written and copyright by Michael Hipp and others
	free software (LGPL/GPL) without any warranty but with best wishes

Directory: http://streaming.radionomy.com/

Terminal control enabled, press 'h' for listing of keys and functions.

Playing MPEG stream 1 of 2: -1ClassicRock ...
ICY-NAME: #1ClassicRock
ICY-URL: http://-1classicrock.playtheradio.com

MPEG 1.0 layer III, 128 kbit/s, 44100 Hz stereo

ICY-META: StreamTitle='The Beatles - Back in the U.S.S.R.';
Now the thing of interest here is the track info 'ICY-META: StreamTitle='The Beatles - Back in the U.S.S.R.';'. So I used grep & sed to extract this info,

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep --line-buffered "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g"
Now the output looks like this,

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep --line-buffered "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g"
The Beatles - Back in the U.S.S.R
Now as the stream continues to play and the tracks keep changing the output in terminal emulator will keep growing. For example after three tracks change it will look something like,

Code: Select all

The Beatles - Back in the U.S.S.R
The Rolling Stones - Jumpin Jack Flash
Bruce Springsteen - Cover Me
Where 'Bruce Springsteen - Cover Me' is currently playing track and the other two are tracks previously played. Now I only want the currently playing track to appear in the output. I don't want the tracks previously played displayed in terminal emulator output.
Kiyop suggested following,

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep --line-buffered "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g" | tail -n1
But this doesn't work. Adding 'tail -n1' to the command prevents anything from appearing on stdout of terminal emulator.
Any idead are welcome!
Cheers!!!

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#4 Post by kiyop »

To hakerdefo,

Have you read my former post: http://forums.debian.net/viewtopic.php? ... 33#p587976 ?
It worked correctly in the case that
"find / > temporarylogfile &"
is used instead of
"mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g" > temporarylogfile &"

And also, tput is useful.
Refer GarryRicketson's post: http://forums.debian.net/viewtopic.php? ... 91#p588091

For example,

Code: Select all

cols=$(tput cols)
tput sc
refreshsecond=1
mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | cut -d \' -f2 > temporarylogfile &
pid=$(ps a|grep -v grep|grep mpg123|tail -n1|sed -e "s/^ *//" -e "s/ .*//")
while [ -n "$(ps a|grep "^ *$pid")" ];do
a=$(tail -n1 temporarylogfile)
wn=$(echo -n "$a"|wc -m)
tput rc
echo -n "$a"
for ((i=1;$i<$[$cols-$wn];i++));do echo -n " "; done
echo $a > temporarylogfile
sleep $refreshsecond
done
rm -r temporarylogfile
I wonder if you don't understand that you can make the above commands into a shell script and that you can execute the shell script.
For example, make a file (such as test.sh) with

Code: Select all

#!/bin/bash
cols=$(tput cols)
tput sc
refreshsecond=1
mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | cut -d \' -f2 > temporarylogfile &
pid=$(ps a|grep -v grep|grep mpg123|tail -n1|sed -e "s/^ *//" -e "s/ .*//")
while [ -n "$(ps a|grep "^ *$pid")" ];do
a=$(tail -n1 temporarylogfile)
wn=$(echo -n "$a"|wc -m)
tput rc
echo -n "$a"
for ((i=1;$i<$[$cols-$wn];i++));do echo -n " "; done
echo $a > temporarylogfile
sleep $refreshsecond
done
rm -r temporarylogfile
and execute it by

Code: Select all

bash ./test.sh
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

BowCatShot
Posts: 959
Joined: 2006-07-15 12:08

Re: Bash: replace the former output with the new one

#5 Post by BowCatShot »

Once you've captured the information line that you want to display, by whatever means you can, you want to place a "\r" at the front of the text string so that each new line overwrites the old one. Here's one of my examples where I display temperature etc. every so often and only produce one line on the screen:

echo -e -n "\r$timenow Temperature $tempf degrees, Humidity $relativehumidity %, Dew Point $dewpointf degrees"

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#6 Post by kiyop »

To BowCatShot,

Thank you for useful info :)
Indeed,

Code: Select all

for a in 111 2 3;do echo -e -n "\r$a";done
outputs only "3" and the prompt begins just after the output "3" without any new line.

P.S.

Code: Select all

for a in 111 2 3;do echo -e -n "\r$a";done;echo
outputs "311". It is not good ;)

But ...
How does "\r" work correctly for standard output?

hakerdefo asked how to overwrite the former output by the new output of

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g"
I made a script "test2.sh" with the following contents:

Code: Select all

#!/bin/bash
i=1
while :;do
echo "test$i"
i=$[1+$i]
sleep 5
done
to output intermittently successive outputs. You know why I do not replace the above

Code: Select all

echo "test$i"
with

Code: Select all

echo -e -n "\rtest$i"
don't you? ;)
mpeg123 may output intermittently without any "\r" without "echo -e -n" ;)

I executed the following to add "\r" in front of each line (output) from "bash ./test.sh":

Code: Select all

bash ./test2.sh|sed -e 's/^/\r/'
and obtained

Code: Select all

test1
test2
test3
and so on. Of no avail.

I executed the following to use 'echo -e -n "\r"':

Code: Select all

echo -e -n "$(bash ./test2.sh|sed -e 's/^/\r/')"
but no output is displayed.

Maybe I do stupid thing :oops:
Please tell me how to use 'echo -e -n "\r"' correctly to overwrite former output with the new output from a command which outputs intermittently.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

BowCatShot
Posts: 959
Joined: 2006-07-15 12:08

Re: Bash: replace the former output with the new one

#7 Post by BowCatShot »

Please tell me how to use 'echo -e -n "\r"' correctly to overwrite former output with the new output from a command which outputs intermittently.
Without delving into the specifics of whatever command you're referring to, I would do something like

var=$(thecommand)
echo -e -n "\r$var"

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#8 Post by kiyop »

To BowCatShot,
BowCatShot wrote:Without delving into the specifics of whatever command you're referring to, I would do something like

var=$(thecommand)
echo -e -n "\r$var"
Thank you for the above reply :)
But I know that such a thing cannot work properly for successive intermittent output from continuing command (without finishing the command), because inputting the value to the valuable does not finish until the command finishes.
I confirmed that It does not work properly.

Code: Select all

var=$(bash ./test2.sh);echo -e -n "\r$var"
did not output anything.
Furthermore, nothing is displayed after I pressed CTRL+c to quit the execution.

Please read hakerdefo's post:
http://forums.debian.net/viewtopic.php? ... 33#p588028
hakerdefo wrote:So I used grep & sed to extract this info,

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep --line-buffered "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g"
Now the output looks like this,

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep --line-buffered "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g"
The Beatles - Back in the U.S.S.R
Now as the stream continues to play and the tracks keep changing the output in terminal emulator will keep growing. For example after three tracks change it will look something like,

Code: Select all

The Beatles - Back in the U.S.S.R
The Rolling Stones - Jumpin Jack Flash
Bruce Springsteen - Cover Me
Where 'Bruce Springsteen - Cover Me' is currently playing track and the other two are tracks previously played. Now I only want the currently playing track to appear in the output. I don't want the tracks previously played displayed in terminal emulator output.
To hakerdefo,

Read one of my former posts: http://forums.debian.net/viewtopic.php? ... 19#p588213
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

BowCatShot
Posts: 959
Joined: 2006-07-15 12:08

Re: Bash: replace the former output with the new one

#9 Post by BowCatShot »

But I know that such a thing cannot work properly for successive intermittent output from continuing command (without finishing the command), because inputting the value to the valuable does not finish until the command finishes.
Well, then I would approach the problem thusly:

I would put the command in question into another batch file in which the output is redirected to a file.

Now from within this batch file that is to do the displaying, I would first off trigger the command batch file to run in the background.

Then within this batch file I would monitor said file for activity. Then using the tail command I would display the last line of the file with the echo statement that I described.

If your situation is such that you could just tail the file at regular, say 1 second, intervals, then the problem becomes really easy.

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#10 Post by kiyop »

To BowCatShot,
BowCatShot wrote:I would put the command in question into another batch file in which the output is redirected to a file.

Now from within this batch file that is to do the displaying, I would first off trigger the command batch file to run in the background.

Then within this batch file I would monitor said file for activity. Then using the tail command I would display the last line of the file with the echo statement that I described.

If your situation is such that you could just tail the file at regular, say 1 second, intervals, then the problem becomes really easy.
Thus, you propose similar thing as what I proposed above concretely http://forums.debian.net/viewtopic.php? ... 63#p587976 and http://forums.debian.net/viewtopic.php? ... 63#p588213 ;)
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

BowCatShot
Posts: 959
Joined: 2006-07-15 12:08

Re: Bash: replace the former output with the new one

#11 Post by BowCatShot »

Thus, you propose similar thing as what I proposed above concretely
Yes I do. I wasn't trying to steal your thunder, just reinforce it.

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#12 Post by kiyop »

To BowCatShot,
BowCatShot wrote:
Thus, you propose similar thing as what I proposed above concretely
Yes I do. I wasn't trying to steal your thunder, just reinforce it.
Thank you :) ;)
I see. The former script of mine can be shrink a little bit:

Code: Select all

refreshsecond=1
wo=0
mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | cut -d \' -f2 > temporarylogfile &
pid=$!
while [ -n "$(ps a|grep "^ *$pid ")" ];do
a=$(tail -n1 temporarylogfile)
wn=$(echo -n "$a"|wc -m)
echo -e -n "\r$a"
for ((i=1;$i<$[$wo-$wn];i++));do echo -n " "; done
echo $a > temporarylogfile # can be omitted
sleep $refreshsecond
wo=$wn
done
rm -r temporarylogfile
To stop unnecessary output,

Code: Select all

refreshsecond=1
wo=0
ao=""
mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | cut -d \' -f2 > temporarylogfile &
pid=$!
while [ -n "$(ps a|grep "^ *$pid ")" ];do
a=$(tail -n1 temporarylogfile)
wn=$(echo -n "$a"|wc -m)
if [ ! "$a" = "$ao" ];then echo -e -n "\r$a";for ((i=1;$i<$[$wo-$wn];i++));do echo -n " ";done;wo=$wn;ao=$a;fi
echo $a > temporarylogfile # can be omitted
sleep $refreshsecond
done
rm -r temporarylogfile
To all,

I really wait better methods.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

Loomx
Posts: 55
Joined: 2010-09-28 01:11

Re: Bash: replace the former output with the new one

#13 Post by Loomx »

How about using pgrep and inotifywait for the loop? Then it doesn't have to check every second.
You might need to sleep 1 before looping to give mpg123 time to start.

e.g.

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | cut -d \' -f2 > temporarylogfile &

sleep 1  # wait for mpg123 to start

while pgrep mpg123 >/dev/null; do
    tail -n1 temporarylogfile
    inotifywait -q -e modify temporarylogfile >/dev/null
done

rm -r temporarylogfile

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#14 Post by kiyop »

Loomx wrote:How about using pgrep and inotifywait for the loop? Then it doesn't have to check every second.
You might need to sleep 1 before looping to give mpg123 time to start.

e.g.

Code: Select all

mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | cut -d \' -f2 > temporarylogfile &

sleep 1  # wait for mpg123 to start

while pgrep mpg123 >/dev/null; do
    tail -n1 temporarylogfile
    inotifywait -q -e modify temporarylogfile >/dev/null
done

rm -r temporarylogfile
Thank you for telling me inotifywait and pgrep :D

In one of my jessie, inotify-tools ( https://packages.debian.org/en/jessie/inotify-tools ) has not been installed. Thus I should install it before using inotifywait:

Code: Select all

apt-get install inotify-tools
To replace the former output with the new one, the following may be better although I have not confirmed that the following works correctly:

Code: Select all

wo=0
mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | cut -d \' -f2 > temporarylogfile &
sleep 1
while pgrep mpg123 >/dev/null; do
    a=$(tail -n1 temporarylogfile)
    wn=$(echo -n "$a"|wc -m)
    echo -e -n "\r$a";for ((i=1;$i<$[$wo-$wn];i++));do echo -n " ";done
    wo=$wn
    inotifywait -q -e modify temporarylogfile >/dev/null
done
rm -r temporarylogfile
The above script will not stop if mpg123 had been executed before the above script was executed.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

User avatar
hakerdefo
Posts: 258
Joined: 2014-05-05 05:31

Re: Bash: replace the former output with the new one

#15 Post by hakerdefo »

Sorry for a delayed reply :(
Thanks a metric ton kiyop for your valued suggestions :D
Thanks also to BowCatShot and Loomx for some great input :D
I was working on an Internet radio player script and this question was asked specifically for a section of that script. I've already released the first version of the script into the wilderness of www,

pmrp (Poor Man's Radio Player)

I definitely will be taking advantage of the suggestions made in this thread to improve the script in the future.
Thanks again to all of you!
Cheers!!!

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#16 Post by kiyop »

Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#17 Post by kiyop »

I read https://github.com/hakerdefo/pmrp/blob/master/pmrp
The above displays all the titles of the played radio stream including the old ones, doesn't it?

You may be able to shrink the script by using arrays. For example,
lines from #138 to #264 may be able to be replaced with the following:

Code: Select all

function UrbaN_181fM (){

Name=("Jammin 181" "Old School HipHop" "The Beat" "The Box" "Trinity" "True R&B")

link=("http://relay.181.fm:8042" "http://relay.181.fm:8068" "http://relay.181.fm:8054" "http://relay.181.fm:8024" "http://icyrelay.181.fm/181-trinityhiphop_128k.mp3" "http://relay.181.fm:8022")

clear

echo -e "Stations Playing $BLUE Urban Music $NC :"
echo ""
for i in 1 2 3 4 5 6
do
echo -e "$BLUE 0$i $NC ${Name[$[i-1]]}"
done
echo -e "$RED 07 $BLUE Return to 181FM Menu $NC"
echo ""
echo -e "Select a $BLUE station $NC between 1 to 6 and hit $BLUE [Enter] $NC to $BLUE PLAY $NC :"

read PLAY
if test "$PLAY" -ge 1 -a "$PLAY" -le 6 2>/dev/null
then
clear
echo -e "$BLUE PMRP $NC -> $BLUE 181FM $NC -> $BLUE Urban $NC -> $BLUE ${Name[$[i-1]]} $NC:"
echo ""
echo -e "Press $BLUE [Space] $NC to pause : Press $BLUE [Q] $NC to stop"
echo ""
echo ""
echo -e "$BLUE Playlist $NC:"
echo ""
$PLAYER "${link[$[i-1]]}" 2>&1 /dev/null | grep --line-buffered "StreamTitle" | sed -e 's/;.*//' -e 's/.*=//' -e "s/'//g"
UrbaN_181fM
elif [ "$PLAY" = 7 ]
then
clear
181fM
fi
clear
echo ""
echo -e "$RED $PLAY $NC $BLUE is an invalid selection! $NC"
echo ""
sleep 3
UrbaN_181fM
} 
You can also use "dialog" for selector.
You can install dialog by executing the following with root privilege:

Code: Select all

apt-get install dialog
And you can read manual of dialog:

Code: Select all

man dialog
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

User avatar
hakerdefo
Posts: 258
Joined: 2014-05-05 05:31

Re: Bash: replace the former output with the new one

#18 Post by hakerdefo »

kiyop wrote:I read https://github.com/hakerdefo/pmrp/blob/master/pmrp
The above displays all the titles of the played radio stream including the old ones, doesn't it?
Yes, it does.
And thanks for the great tip. That tip not only shrinks the script but makes it more readable and easier to understand.
Cheers!!!

HarborView
Posts: 41
Joined: 2014-02-01 02:21
Location: The Space Coast of the USA

Re: Bash: replace the former output with the new one

#19 Post by HarborView »

This works because xterm takes ANSI positioning codes.
See:
http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html

Code: Select all

#!/bin/bash
x="Now is the time*for all good men*to come to the aid*of their country"
IFS="*"
X=($x)
clear
for ((k=0; k<4; k++)); do 
  echo ${X[k]}
  sleep 1
done
echo

MoveUp=`echo -e "\033[1A"`
ClearLine=`echo -e "\033[K"`
echo "Press ENTER for the 2nd part"
read
echo
echo
for ((k=0; k<4; k++)); do 
  echo -n $MoveUp$ClearLine
  echo ${X[k]}
  sleep 1
done

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: Bash: replace the former output with the new one

#20 Post by kiyop »

Thank you for telling ANSI positioning codes :)
The former script of mine which does not display unnecessary output can be shrink a little bit:

Code: Select all

refreshsecond=1
ao=""
mpg123 http://streaming.radionomy.com/-1ClassicRock 2>&1 /dev/null | grep "StreamTitle" | cut -d \' -f2 > temporarylogfile &
pid=$!
echo -ne "\033[s"
while [ -n "$(ps a|grep "^ *$pid ")" ];do
a=$(tail -n1 temporarylogfile)
if [ ! "$a" = "$ao" ];then echo -ne  "\033[u\033[K$a";ao=$a;fi
echo $a > temporarylogfile # can be omitted
sleep $refreshsecond
done
rm -r temporarylogfile
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

Post Reply