Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.debian.user > #239023 > unrolled thread
| Started by | Jude DaShiell <jdashiel@panix.com> |
|---|---|
| First post | 2021-08-19 11:00 +0200 |
| Last post | 2021-08-19 15:10 +0200 |
| Articles | 9 — 5 participants |
Back to article view | Back to linux.debian.user
bbc script Jude DaShiell <jdashiel@panix.com> - 2021-08-19 11:00 +0200
Re: bbc script "Thomas Schmitt" <scdbackup@gmx.net> - 2021-08-19 11:40 +0200
Re: bbc script Jude DaShiell <jdashiel@panix.com> - 2021-08-19 19:10 +0200
Re: bbc script rhkramer@gmail.com - 2021-08-19 14:30 +0200
Re: bbc script Greg Wooledge <greg@wooledge.org> - 2021-08-19 14:40 +0200
Re: bbc script Jude DaShiell <jdashiel@panix.com> - 2021-08-20 01:50 +0200
Re: bbc script Greg Wooledge <greg@wooledge.org> - 2021-08-20 03:00 +0200
Re: bbc script <tomas@tuxteam.de> - 2021-08-20 09:40 +0200
Re: bbc script "Thomas Schmitt" <scdbackup@gmx.net> - 2021-08-19 15:10 +0200
| From | Jude DaShiell <jdashiel@panix.com> |
|---|---|
| Date | 2021-08-19 11:00 +0200 |
| Subject | bbc script |
| Message-ID | <CNM41-7ca-1@gated-at.bofh.it> |
I tested the following script by having the $REPLY variable report its
value after the select statement and the value I entered was correct.
The problem I'm having is with the case statement I used. For some reason
the case statement isn't playing any of the stations and I have mpv on my
machine.
Have we got any case statement experts who wouldn't mind examining this
script and sending me a few pointers?
I have many other scripts with radio stations in them and if I can get
this technique working I'll be able to roll those into a single script
correctly:
Cut here.
#!/usr/bin/env bash
# file: bbc.sh
PS3="Enter a number to Choose station: "
select station in bbc1 bbc-1-extra bbc-2 bbc-3 bbc-4 \
b bc-4-long-wave-and-shipping bbc-4-extra bbc-5 \
bbc-6-music bbc-world-service quit
do
case $REPLY in
$REPLY==1)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_one.pls
;;
$REPLY==2)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_1xtra.pls
;;
$REPLY==3)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_two.pls
;;
$REPLY==4)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_three.pls
;;
$REPLY==5)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_fourfm.pls
;;
$REPLY==6)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_fourlw.pls
;;
$REPLY==7)
mpv http://stream.live.vc.bbcmedia.co.uk/bbc_radio_four_extra
;;
$REPLY==8)
mpv http://stream.live.vc.bbcmedia.co.uk/bbc_radio_five_live_online_nonuk
;;
$REPLY==9)
mpv http://stream.live.vc.bbcmedia.co.uk/bbc_6music
;;
$REPLY==10)
mpv http://stream.live.vc.bbcmedia.co.uk/bbc_world_service
;;
$REPLY==11)
exit 0
;;
esac
done
[toc] | [next] | [standalone]
| From | "Thomas Schmitt" <scdbackup@gmx.net> |
|---|---|
| Date | 2021-08-19 11:40 +0200 |
| Message-ID | <CNMGK-7GK-11@gated-at.bofh.it> |
| In reply to | #239023 |
Hi,
Jude DaShiell wrote:
> select station in bbc1 bbc-1-extra bbc-2 bbc-3 bbc-4 \
> b bc-4-long-wave-and-shipping
> bbc-4-extra bbc-5 \
> bbc-6-music bbc-world-service quit
> do
> case $REPLY in
> $REPLY==1)
man bash says about "case" that before ")" are patterns which are tested
whether they match.
So the text snippet "$REPLY==" before "1)" looks wrong.
This litle experiment works for me:
select station in AAA BB CC quit
do
echo $REPLY
case $REPLY in
1) echo aaa
;;
2) echo bb
;;
3) echo cc
;;
4) break
;;
esac
done
(As C programmer i am more into "if"-"elif"-chains than into "case", though.)
Have a nice day :)
Thomas
[toc] | [prev] | [next] | [standalone]
| From | Jude DaShiell <jdashiel@panix.com> |
|---|---|
| Date | 2021-08-19 19:10 +0200 |
| Message-ID | <CNTId-3Xl-1@gated-at.bofh.it> |
| In reply to | #239025 |
Thanks to all who have replied. It turns out too much code was on case
statement and once I cleared that the script now works as expected.
Here's the working version:
#!/usr/bin/env bash
# file: bbc.sh
PS3="Enter a number to Choose station: "
select station in bbc1 bbc-1-extra bbc-2 bbc-3 bbc-4 \
b bc-4-long-wave-and-shipping bbc-4-extra bbc-5 \
bbc-6-music bbc-world-service quit
do
case $REPLY in
1)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_one.pls
;;
2)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_1xtra.pls
;;
3)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_two.pls
;;
4)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_three.pls
;;
5)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_fourfm.pls
;;
6)
mpv http://open.live.bbc.co.uk/mediaselector/5/select/mediaset/http-icy-mp3-a/format/pls/proto/http/vpid/bbc_radio_fourlw.pls
;;
7)
mpv http://stream.live.vc.bbcmedia.co.uk/bbc_radio_four_extra
;;
8)
mpv http://stream.live.vc.bbcmedia.co.uk/bbc_radio_five_live_online_nonuk
;;
9)
mpv http://stream.live.vc.bbcmedia.co.uk/bbc_6music
;;
10)
mpv http://stream.live.vc.bbcmedia.co.uk/bbc_world_service
;;
11)
exit 0
;;
esac
done
On Thu, 19 Aug 2021, Thomas Schmitt wrote:
> Hi,
>
> Jude DaShiell wrote:
> > select station in bbc1 bbc-1-extra bbc-2 bbc-3 bbc-4 \
> > b bc-4-long-wave-and-shipping
> > bbc-4-extra bbc-5 \
> > bbc-6-music bbc-world-service quit
> > do
> > case $REPLY in
> > $REPLY==1)
>
> man bash says about "case" that before ")" are patterns which are tested
> whether they match.
> So the text snippet "$REPLY==" before "1)" looks wrong.
>
> This litle experiment works for me:
>
> select station in AAA BB CC quit
> do
> echo $REPLY
> case $REPLY in
> 1) echo aaa
> ;;
> 2) echo bb
> ;;
> 3) echo cc
> ;;
> 4) break
> ;;
> esac
> done
>
> (As C programmer i am more into "if"-"elif"-chains than into "case", though.)
>
>
> Have a nice day :)
>
> Thomas
>
>
[toc] | [prev] | [next] | [standalone]
| From | rhkramer@gmail.com |
|---|---|
| Date | 2021-08-19 14:30 +0200 |
| Message-ID | <CNPlf-13N-5@gated-at.bofh.it> |
| In reply to | #239023 |
On Thursday, August 19, 2021 04:59:19 AM Jude DaShiell wrote: > I tested the following script by having the $REPLY variable report its > value after the select statement and the value I entered was correct. > The problem I'm having is with the case statement I used. For some reason > the case statement isn't playing any of the stations and I have mpv on my > machine. > Have we got any case statement experts who wouldn't mind examining this > script and sending me a few pointers? > I have many other scripts with radio stations in them and if I can get > this technique working I'll be able to roll those into a single script > correctly: > Cut here. > #!/usr/bin/env bash > # file: bbc.sh > PS3="Enter a number to Choose station: " > select station in bbc1 bbc-1-extra bbc-2 bbc-3 bbc-4 \ > b bc-4-long-wave-and-shipping bbc-4-extra bbc-5 \ > bbc-6-music bbc-world-service quit > do > case $REPLY in > $REPLY==1) > mpv I am not a bash expert (not even close), but I don't see where your selection (in PS3) gets transferred to REPLY -- shouldn't that happen somewhere explicitly?
[toc] | [prev] | [next] | [standalone]
| From | Greg Wooledge <greg@wooledge.org> |
|---|---|
| Date | 2021-08-19 14:40 +0200 |
| Message-ID | <CNPuW-16Z-7@gated-at.bofh.it> |
| In reply to | #239032 |
On Thu, Aug 19, 2021 at 08:25:10AM -0400, rhkramer@gmail.com wrote: > On Thursday, August 19, 2021 04:59:19 AM Jude DaShiell wrote: > > #!/usr/bin/env bash > > # file: bbc.sh > > PS3="Enter a number to Choose station: " > > select station in bbc1 bbc-1-extra bbc-2 bbc-3 bbc-4 \ > > b bc-4-long-wave-and-shipping bbc-4-extra bbc-5 \ > > bbc-6-music bbc-world-service quit > > do > > case $REPLY in > > $REPLY==1) > > mpv > > I am not a bash expert (not even close), but I don't see where your selection > (in PS3) gets transferred to REPLY -- shouldn't that happen somewhere > explicitly? The "select" builtin command does it. Read "help select" for the brief version of the documentation. (The man page's section isn't much larger, though.) Also relevant: https://mywiki.wooledge.org/BashFAQ/115
[toc] | [prev] | [next] | [standalone]
| From | Jude DaShiell <jdashiel@panix.com> |
|---|---|
| Date | 2021-08-20 01:50 +0200 |
| Message-ID | <CNZXj-7Hw-3@gated-at.bofh.it> |
| In reply to | #239035 |
I had a read of your wiki article and think I almost understand it. To the left of the parens there's a single letter in that calculator example. That I understand. If you have a) add as part of the case statement is add in that partial case statement so when that selection is made add appears on the screen? If so, I can improve what I've done. On Thu, 19 Aug 2021, Greg Wooledge wrote: > On Thu, Aug 19, 2021 at 08:25:10AM -0400, rhkramer@gmail.com wrote: > > On Thursday, August 19, 2021 04:59:19 AM Jude DaShiell wrote: > > > #!/usr/bin/env bash > > > # file: bbc.sh > > > PS3="Enter a number to Choose station: " > > > select station in bbc1 bbc-1-extra bbc-2 bbc-3 bbc-4 \ > > > b bc-4-long-wave-and-shipping bbc-4-extra bbc-5 \ > > > bbc-6-music bbc-world-service quit > > > do > > > case $REPLY in > > > $REPLY==1) > > > mpv > > > > I am not a bash expert (not even close), but I don't see where your selection > > (in PS3) gets transferred to REPLY -- shouldn't that happen somewhere > > explicitly? > > The "select" builtin command does it. Read "help select" for the brief > version of the documentation. (The man page's section isn't much larger, > though.) > > Also relevant: https://mywiki.wooledge.org/BashFAQ/115 > >
[toc] | [prev] | [next] | [standalone]
| From | Greg Wooledge <greg@wooledge.org> |
|---|---|
| Date | 2021-08-20 03:00 +0200 |
| Message-ID | <CO134-8jQ-7@gated-at.bofh.it> |
| In reply to | #239069 |
> On Thu, 19 Aug 2021, Greg Wooledge wrote:
> > Also relevant: https://mywiki.wooledge.org/BashFAQ/115
On Thu, Aug 19, 2021 at 07:40:23PM -0400, Jude DaShiell wrote:
> I had a read of your wiki article and think I almost understand it. To
> the left of the parens there's a single letter in that calculator example.
> That I understand. If you have a) add as part of the case statement is
> add in that partial case statement so when that selection is made add
> appears on the screen? If so, I can improve what I've done.
The echo commands write the output to the terminal. They control what
the user sees as the menu.
echo "== Make your selection:"
echo "a) add"
echo "s) subtract"
echo "m) multiply"
echo "q) quit"
There's nothing complicated here. That's the whole point. You decide
what the user should see, then you write that to the terminal, using
echo, or printf, or cat and a here-doc, or whatever you like.
The case statement simply compares what the user typed to a bunch of
predefined strings. You choose what those strings are.
case $ch in
a) add; break;;
s) subtract; break;;
m) multiply; break;;
q) exit 0;;
*) echo "Unrecognized command. Please try again.";;
esac
Again, it's not complicated. If the user types "a", then you run the
"add" command. If the user types "s", you run the "subtract" command.
And so on.
If you wanted to allow the user to type either "a" or "ad" or "add" to
enter the addition menu, you could change the case statement to look
like this:
case $ch in
a | ad | add) add; break;;
And so on. It's your menu. You define how it should work.
In this example, "add" is a function. It's defined below the "main" menu
function, but it could be defined pretty much anywhere.
add() {
That's the start of the definition of "add". I didn't include the
definitions of "subtract" and "multiply" because they're similar, and
you should be able to figure them out. Of course, in your program,
you probably aren't going to prompt the user for numbers and then add
them. You're going to do something entirely different. So, again,
there's no point in writing a bunch of useless functions in this example.
The main points I'm trying to convey here are:
1) You don't need "select" to make a menu. You can just use echo and read.
2) In fact, "select" locks you in to a specific form of menu with very
little flexibility. If you use echo and read, you can make your menu
work however you like. You can do a *better* menu. (Hell, you could
get all fancy and use whiptail or dialog if you want.)
3) If you have nested menus, it's useful for each menu to be in its own
function. The parent menu should call the appropriate function to
transfer control to the next (child) menu. The child menu's function
should simply "return" (or fall off the end of the function) to transfer
control back to the parent. Don't explicitly *call* the parent again.
And please, don't overthink it. This is really basic stuff.
[toc] | [prev] | [next] | [standalone]
| From | <tomas@tuxteam.de> |
|---|---|
| Date | 2021-08-20 09:40 +0200 |
| Message-ID | <CO7ia-44K-7@gated-at.bofh.it> |
| In reply to | #239073 |
[Multipart message — attachments visible in raw view] — view raw
On Thu, Aug 19, 2021 at 08:56:22PM -0400, Greg Wooledge wrote:
> > On Thu, 19 Aug 2021, Greg Wooledge wrote:
> > > Also relevant: https://mywiki.wooledge.org/BashFAQ/115
Great explanation, Greg. I wish I had half of your talent :)
One small addendum: the "predefined strings" in the case statement
are actually patterns to match the (contents) of the variable in
question:
> The case statement simply compares what the user typed to a bunch of
> predefined strings. You choose what those strings are.
>
> case $ch in
> a) add; break;;
> s) subtract; break;;
> m) multiply; break;;
> q) exit 0;;
> *) echo "Unrecognized command. Please try again.";;
> esac
So the first one ("a") just matches when $ch contains exactly an "a".
But the last one ("*") matches everything (a way of saying "else",
or "if nothing matched, then...").
Same for the latter example:
> case $ch in
> a | ad | add) add; break;;
the "a | ad | add" is a pattern matching "a" or "ad", or "add". There
are more useful patterns.
Cheers
- t
[toc] | [prev] | [next] | [standalone]
| From | "Thomas Schmitt" <scdbackup@gmx.net> |
|---|---|
| Date | 2021-08-19 15:10 +0200 |
| Message-ID | <CNPXX-1vX-1@gated-at.bofh.it> |
| In reply to | #239032 |
Hi,
rhkramer@gmail.com wrote:
> I don't see where your selection (in PS3) gets transferred to REPLY
REPLY is filled by bash command "select".
man bash says:
select name [ in word ] ; do list ; done
[...]
The PS3 prompt is then displayed and a line read from the
standard input.
[...]
The line read is saved in the variable REPLY.
Have a nice day :)
Thomas
[toc] | [prev] | [standalone]
Back to top | Article view | linux.debian.user
csiph-web