原文链接:http://1985wanggang.blog.163.com/blog/static/776383320121745626320/ a="one,two,three,four" 要将$a分割开,可以这样: OLD_IFS="$IFS" IFS="," arr=($a) IFS="$OLD_IFS" for s in ${arr[@]} do echo "$s" done 上述代码会输出 one
Reference: http://saiyaren.iteye.com/blog/1943207 1. Shell 读取文件和写文件 for line in $(<top30000.url.utf-8.http_server_front_hphp.txt); do tmp_port=8080; for((i=0;i<=7;i++));do echo ${line/192\.168\.12\.63/192\.168\.12\.63:$tmp_port} >>top3000
shell 分割字符串存至数组 shell编程中,经常需要将由特定分割符分割的字符串分割成数组,多数情况下我们首先会想到使用awk但是实际上用shell自带的分割数组功能会更方便.假如a=”one,two,three,four” 要将$a分割开,可以这样:OLD_IFS=”$IFS”IFS=”,”arr=($a)IFS=”$OLD_IFS”for s in ${arr[@]}doecho “$s”done 上述代码会输出onetwothreefour arr=($a)用于将字符串$a分割到数组$
shell切分字符串到数组 问题: 对于’aa,bb,cc,dd,ee’这样的字符串输出采用,分隔开的aa bb cc dd ee aa:bb is ok:/home/work按照":"分割开来的aa bb is ok /home/work 解决方法1: #!/bin/bash var=’aa,bb,cc,dd,ee’ var=${var//,/ } #这里是将var中的,替换为空格 for element in $var do echo $element done
#!/bin/bash tmp="test,girl,boy,love" OLD_IFS="$IFS" IFS="," arr=($a) IFS="$OLD_IFS" echo "arr[0] is: ${arr[0]}" echo "arr len: ${#arr[@]}" for s in ${arr[@]}do echo "$s"done 存储老的分隔符 OLD
test.sh #!/bin/bash echo "enter the string:" read filename if test -z $filename ; then echo "the length is 0" else echo "the length is not 0" fi 执行 sudo chmod +x test.sh./test.sh 输出 enter the string: the length 执行 ./test.sh 输
代码:test.sh #!/bin/bash a="one,two,three,four" #要将$a分割开,可以这样: OLD_IFS="$IFS" IFS="," arr=($a) IFS="$OLD_IFS" for s in ${arr[@]} do echo "$s" done shell编程中,经常需要将由特定分割符分割的字符串分割成数组,多数情况下我们首先会想到使用awk但是实际上用shell