2

I'm playing with my Raspberry Pi Zero, and I'm trying to automate an FM transmitter script.

You can run the script with sudo fm_transmitter -f [frequency] -r [.wav music file]

For example:

sudo fm_transmitter -f 103 -r star_wars.wav

My code:

musics[0] = "/home/pi/radio/fm/star_wars.wav"

sudo /home/pi/radio/fm/fm_transmitter -f 103 -r musics[0]

It gives me an error because it can't find musics[0].

What could be the problem here?

3
  • 1
    Is this a Bash script or a strictly sh script? Arrays are a Bash thing.
    – Thomas Ward
    Jan 3, 2019 at 14:22
  • What are you trying to do with that music[0], is that an array? Jan 3, 2019 at 14:23
  • i want to feed in that star_wars.wav file into -r so i can change it when it finishes
    – Burgerl X
    Jan 3, 2019 at 14:23

1 Answer 1

5

You've set the first element of an array as /home/pi/radio/fm/star_wars.wav with the line:

musics[0]="/home/pi/radio/fm/star_wars.wav"

To access the contents, you need to expand that array element using $ like so:

sudo /home/pi/radio/fm/fm_transmitter -f 103 -r "${musics[0]}"

The quotes "" are used to preserve the format of the output, and the braces {} are part of expanding an array variable.

It's not clear why you're using an array here, when a standard variable would work. You could replace:

musics[0]="/home/pi/radio/fm/star_wars.wav"

with

musics="/home/pi/radio/fm/star_wars.wav"

and the line using that variable changes to:

sudo /home/pi/radio/fm/fm_transmitter -f 103 -r "$musics"

This is much simpler as you don't need to use index numbers (the part referred to by [0]).

10
  • i tried echo "${musics[0]}" and adding it into the sudo method, but it just says it cant find musics[0](even the echo)
    – Burgerl X
    Jan 3, 2019 at 14:30
  • i want to use an array so i can switch songs when they ended
    – Burgerl X
    Jan 3, 2019 at 14:32
  • If there are spaces around the = in the first line, the variable won't be assigned. Are you using the Bash shell?
    – Arronical
    Jan 3, 2019 at 14:32
  • + it says that musics[0] is not found and i an using bash
    – Burgerl X
    Jan 3, 2019 at 14:46
  • Make sure there are no spaces around the =, if it says it can't find musics[0] then it is trying to find the literal string 'musics[0]', which means that you're not trying to expand the variable at all. There is no need to add an echo command into the sudo fm_transmitter ... command.
    – Arronical
    Jan 3, 2019 at 14:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .