shell数组脚本
#!/bin/bash array=(1 2 3 4 5) for ((i=0;i<${#array[*]};i++)) do echo ${array[i]} done
脚本2
#!/bin/bash array=(1 2 3 4 5) for i in ${array[*]} do echo $i done
脚本3
#!/bin/bash array=(1 2 3 4 5) i=0 while ((i<${#array[*]})) do echo ${array[i]} ((i++)) done
脚本4
#!/bin/bash array=( orange apple banana ) for ((i=0;i<${#array[*]};i++)) do echo "This is num $i , then content is ${array[$i]}" done echo ------------- echo "array len:${#array[*]}"
静态数组 | array=(1 2 3) |
动态数组 | array=($(ls)) |
为数组赋值 | array[3]=4 |
打印所有元素 | ${array[*]} 或${array[@]} |
打印数组长度 | ${#array[*]}或${#array[@]} |
打印单个元素 | ${array[i]} i是数组下标 |
利用bash for循环打印下面这句话中字母数不大于6的单词
template is a good man (计算变量长度的方法 echo ${#char}、echo $char |wc -l 、 expr length $char 、 echo $char | awk '{print length($0)}')
#!/bin/bash arr=(template is a good man) for ((i=0;i<${#arr[*]};i++)) do if [ `expr length ${arr[$i]}` -le 6 ];then echo ${arr[$i]} fi done
-------------------------------------------------
chars="template is a good man"
echo $chars |awk 'for(i=1;i<=NF;i++) if (length($i)<=6) print $i}'