shell如何把字符串切割为数组
展开全部
root@localhost:~/xly# sh 1 hello
abc
shell
haha
1 hello abc
2 shell
3 haha
root@localhost:~/xly# vim 1
#!/bin/bash
string="hello abc,shell,haha"
OLD_IFS="$IFS"
IFS=","
array=($string)
IFS="$OLD_IFS"
for var in ${array[@]}
do
echo $var
done
echo $string | awk '{split($0,arr,",");for(i in arr) print i,arr[i]}'
用awk更加精准
abc
shell
haha
1 hello abc
2 shell
3 haha
root@localhost:~/xly# vim 1
#!/bin/bash
string="hello abc,shell,haha"
OLD_IFS="$IFS"
IFS=","
array=($string)
IFS="$OLD_IFS"
for var in ${array[@]}
do
echo $var
done
echo $string | awk '{split($0,arr,",");for(i in arr) print i,arr[i]}'
用awk更加精准
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询