1

This is my code:

alias radio='
if [ -e "$station" ]
then
    open $station
else
    say "I still don't know what your favorite radio station is sir. Would you mind giving me the link?"
    echo "What is the link of your favorite station?"
    read station
    echo "station="$station"" >> ~/.fis/config
    say "You can now try the command again."
fi'

The code runs up to the part where it asks for the link. When I provide it with a link I get the following error:

-bash: station=http://www.cidadefm.iol.pt/player/player.html?: No such file or directory

Does anyone have any idea of what might be wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Aslet
  • 127
  • 2
  • 7
  • is `say` an unix command? – Bill May 14 '13 at 00:32
  • @Bill It's a mac command. Not sure if available in all unix distributions though. – Aslet May 14 '13 at 00:36
  • Out of curiosity...do you have a file `./http://www.cidadefm.iol.pt/player/player.html` in your current dir from where you run your script (`open $station`) – Bill May 14 '13 at 00:41
  • @Bill Very funny, but it is my understanding that the open command can open links with your main browser, and when I use it directly into the terminal it works. Obviously I am doing something wrong when assigning the link to `station`, but I am not sure what exactly I'm doing wrong. – Aslet May 14 '13 at 00:45
  • this is from `man` page of `open` -- `open - Open a file-based or command pipeline channel` – Bill May 14 '13 at 00:48
  • btw, since you are using `mac`, there may be something specific to `mac` which I am unaware of..so I apologize in advance if I am wrong. – Bill May 14 '13 at 00:50
  • @Bill Yeah.. The mac terminal gives a somewhat different description: `If the file is in the form of a URL, the file will be opened as a URL. You can specify one or more file names (or pathnames), which are interpreted relative to the shell or Terminal window's current working directory. For example, the follow- ing command would open all Word files in the current working directory:` – Aslet May 14 '13 at 00:50
  • Look at this..this may be helpful for you. http://stackoverflow.com/questions/10006958/open-an-html-file-with-default-browser-using-bash-on-mac – Bill May 14 '13 at 00:51

2 Answers2

3

WhatsWrongWithMyScript.com helpfully points out that the apostrophe in "don't" is terminating the single quoted expression. Instead of fixing this by using "don'\''t", please use a function instead:

radio() {
  if [ -e "$station" ]
  then
      open $station
  else
      say "I still don't know what your favorite radio station is sir. Would you mind giving   me the link?"
      echo "What is the link of your favorite station?"
      read station
      echo "station=\"$station\"" >> ~/.fis/config
      say "You can now try the command again."
  fi
}
that other guy
  • 116,971
  • 11
  • 170
  • 194
2

The main problem is that you are using $station outside of quotes. There's probably an & breaking up the command.

You seem to use the "station" variable name for two different purposes. That's confusing.

Also, kind of awkward to put all that into an alias. I'd use a function

radio () {
    local file=~/.fis/config
    if [ -f "$file" ]
    then
        station=$(< "$file")
    else
        say "I still don't know what your favorite radio station is sir. Would you mind giving me the link?"
        echo "What is the URL of your favorite station?"
        read staton
        echo "$station" > "$file"
    fi    
    open "$station"
}
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Ok guys, I think the problem might have to do with the fact that $station is not a file, but rather a string. is `-e` the correct way to check if a string exists? Thanks for all the help – Aslet May 14 '13 at 01:19
  • 1
    to check a string is non-empty, use `if [[ -n "$string" ]]` -- see http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions – glenn jackman May 14 '13 at 02:39