bash中的数组声明问题?

时间:2015-12-24 21:27:46

标签: bash shell

尝试创建一个bash脚本来选择一个随机的无线电IP来播放mplayer:

#!/bin/sh
#A simple script to run random radio channels on mplayer

radio = (http://162.253.40.181:8808 http://195.154.69.121:8000 http://108.163.197.114:8103 http://216.245.201.73:9910 http://5.63.145.172:7090)

current = echo $[ 1 + $[ RANDOM % 5 ]]

#echo $current

mplayer $radio{[current]}

我认为我的数组声明是错误的,因为脚本会抛出以下错误:

  

语法错误:“(”意外

1 个答案:

答案 0 :(得分:2)

shell中的赋值不能在=的两边都有空格字符,因此你需要

 radio=(http://162.253.40.181:8808 http://195.154.69.121:8000 http://108.163.197.114:8103 http://216.245.201.73:9910 http://5.63.145.172:7090)

修好后,你的行

 current = echo $[ 1 + $[ RANDOM % 5 ]] 

有一些问题

尝试

 current=$(( $RANDOM % 5 ))

或者如果你真的需要1+,那么

 current=$(( 1+ $RANDOM % 5 ))

并且,根据您的评论/问题,您需要

mplayer $radio{[$current]}

IHTH