bash array】的更多相关文章

bash 仅支持一维数组. 而且数组下标是从0开始的为数组赋值:array=(1 4 7 2 5 8)     #以空格为分割符,()为数组str="this is test string"str_arr=($str);         #默认以空格分割数组遍历:for val in str_arr[*];do echo $val; donefor file in `ls`;do echo $file; done数组元素个数: ${#array}举例: 将一个字符串的每个字符分割为数组…
#定义array ptpArray=()while read linedo #将文件读取内容放到array中,注意作为字符串放,否则空格会被分隔成行ptpArray+=("$line")done < a #echo "${#ptpArray[@]}" #for line in "${ptpArray[@]}"…
files=(a b c d e f g h i j k l m n o p)cnt="${#files[@]}"let cnt1="($cnt+2)/3"let cnt2="$cnt1*2"let cnt3="$cnt-$cnt2"files_part1=( "${files[@]:0:$cnt1}" )files_part2=( "${files[@]:$cnt1:$cnt1}" )…
我的测试基本都是在Mac,及Unix环境下测试的,如无特别注明,默认就是Mac 不论你看到这篇随笔是被shell array的奇淫巧技,还是发现shell array就在一对{}里面就可以做那么多勾当,然而记不清楚了,当然有可能发现不管是用$*还是$@数组长度都是1,这可怎么办,还是小白,我就从我碰壁的过程中得到的碎屑整理一二,打通你的任督二脉 Shell是有很多种的 /bin/sh (已经被 /bin/bash 所取代) /bin/bash (就是 Linux预设shell,bash 主要兼容…
Hyperpolyglot Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh grammar | quoting and escaping | charactersvariables | variable expansion | brace, tilde, command, and pathname expansion | special variablesarithmetic and conditional expressionsarrays | associat…
复杂的数据结构和计算不是 bash 的特长.但它的确至少提供了数组和算术运算. 1.算术运算 所有的 bash 变量的值都是字符串,所以 bash 在赋值的时候并不区分数字 1 和 字符串 "1" .不同之处在于如何使用变量.下面几行代码展示出了其中的差异: #!/bin/bash a= b=$(()) c=$a+$b d=$(($a+$b)) echo "$a + $b = $c \t(plus sign as string literal)" echo &quo…
Shell数组例子 循环打印数组,并统计数组的个数: [root@slavedb array]# cat a.sh #!/bin/bash array=( freddy freddie tang sheng wei ) ;i<${#array[@]};i++));do echo "This is num $i,then content is ${array[$i]}" #$i是下标 done echo "-----------------" echo &quo…
Shell函数的简单应用 在脚本内给函数传参: #!/bin/bash . /etc/init.d/functions CheckUrl (){ curl -I -s $ | head - } CheckUrl joy4you.com CheckUrl www.baidu.com 练习,取出数组中的三个元素: 方法一: #!/bin/bash n= array=( freddy1 freddy2 freddy3 ) #;i<${#array[*]};i++));do for i in ${arr…
#!/bin/bash # array variable to function test function testit { local newarray newarray=("$@") echo "The new array value is: ${newarray[*]}" } myarray=( ) echo "The original array is ${myarray[*]}" testit ${myarray[*]} 注意: fu…
1 shell函数的定义及其调用 shell函数有两种格式: function name { commands } name() { commands } 其中,name为函数名,commands为函数体中执行的语句块.定义函数后,可以简单地通过函数名name对其进行调用. 注: 如果出现两个同名的函数定义,那么后者会覆盖前者,而不是报错. 2 函数返回值 shell函数在运行结束时返回状态码,有三种方式: 1 默认情况下,返回函数中最后一条命令的状态码,可以用$?来获取函数的退出状态码 #!…