我是 shell 编码的新手。我的意图是接受来自命令行的参数,以 CSV 格式,将输入解析为一个数组。就像是,
sh scriptname.sh 123,345,456,789...
123,345,456..应该存储到一个数组中。到目前为止,我已经实现了以下目标,
#!/bin/ksh
#get the argument to be parsed
RID=$1
#Parse the argument, remove the space and store into and variable
str=`echo $RID | sed 's/,/\ /g'
#Assign the value to a variable
set -A RIDS $str
#Get the count of arguments in the array
num=${#RIDS[@]}
echo $num
#Display the elements in the array
i=0
while [ $num -gt 0 ] do
echo ${A_RIDS[i]}
i=`expr $1 + 1`
num=`expr $num - 1`
done
但它会引发错误为“错误替换”(在第 7 行)
或者,我尝试了以下方式(一次完成),如下所示
set -A A_RID $(echo $RID | sed 's/,/\ /g')
代替
str=`echo $RID | sed 's/,/\ /g'
set -A RIDS $str
这次它在
set -A A_RID $(echo $RID | sed 's/,/\ /g').
你能告诉我哪里做错了吗?提前致谢!