猛砸如果既节约了变量配置文件声明不起作用

问题描述:

这是我的代码:猛砸如果既节约了变量配置文件声明不起作用

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' 

的代码运行到哪里,它要求该链接的一部分。当我提供链接时,出现以下错误:

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

有没有人有任何想法可能会出错?

+0

是'say'一个unix命令吗? – Bill 2013-05-14 00:32:57

+0

@Bill这是一个mac命令。不知道在所有的Unix发行版中是否可用。 – Aslet 2013-05-14 00:36:00

+0

出于好奇...你有一个'./http:/www.ccidadefm.iol.pt/player/player.html'文件在你当前的目录中,从你运行脚本的地方('open $ station') – Bill 2013-05-14 00:41:36

主要问题是您在引号外使用$station。可能有一个&分解命令。

您似乎使用“站”变量名出于两个不同的目的。这很混乱。

另外,有点尴尬把所有的东西放进一个别名。我会使用一个功能

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" 
} 
+0

好吧,我认为这个问题可能与事实有关$站不是一个文件,而是一个字符串。 '-e'是检查字符串是否存在的正确方法? 感谢所有帮助 – Aslet 2013-05-14 01:19:39

+1

检查一个字符串是非空的,使用'if [[-n“$ string”]]' - 见http://www.gnu.org/software/bash/manual/ bashref.html#Bash-Conditional-Expressions – 2013-05-14 02:39:16

+0

感谢兄弟!你已经解决了我的问题! – Aslet 2013-05-14 22:25:34

WhatsWrongWithMyScript.com有益指出,“不”被终止单引号表达撇号。不用通过使用“don't”来解决这个问题,请使用函数来代替:

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 
}