Shell编程基础进阶(5)

expr1 初值expr2 终值expr3 步长值

st=>start: Start op=>operation: 命令 op1=>operation: expr1 op3=>operation: expr3 cond=>condition: expr2为真 ? e=>end st->op1->cond cond(yes)->op->op3->cond cond(no)->e

IFS

#!/bin/bash data='a,b,c,d' IFS=, for i in $data;do echo $i done while while 条件表达式 do 命令 done st=>start: Start op=>operation: 命令 cond=>condition: 条件表达式为真 ? e=>end st->cond cond(yes)->op->cond cond(no)->e

把a.txt文件中的内容倒腾到b.txt文件中

cat a.txt
1
2
3
4
5
6
7
8
9
10

cat b.txt
10
9
8
7
6
5
4
3
2

1 #!/bin/bash touch b.txt while [ -n "$(cat a.txt)" ] do if [ -z "$(cat b.txt)" ];then head -1 a.txt > b.txt else sed -i "1i `head -1 a.txt`" b.txt fi sed -i 1d a.txt done

while read

while read -p "请输入:" do if [[ $REPLY =~ ^[0-9]+$ ]];then echo "输出:$((REPLY*=${REPLY}00))" elif [[ "$REPLY" = "q" ]];then break fi done

while读取文件三种方式

1、 cat ./a.txt | while read LINE do echo $LINE done 2、 while read LINE do echo $LINE done < ./a.txt 3、 exec < ./a.txt while read LINE do echo $LINE done break

break n 表示跳出循环的层数

省略n 表示跳出整个循环

continue
continue n 退到第n层继续循环

省略n 表示跳出本次循环,继续下一次循环

select

#PS3=[$USER@$0]# select VAR in var1 var2 quit do case $VAR in var1) echo 1 ;; var2) echo 2 ;; quit) break ;; esac done   函数 函数定义 function 函数名(){ 命令 } 函数参数

$1
$2
$3
$#
$*

$@

函数调用

函数名 参数1 参数2 参数3

函数返回

返回数字作为函数执行状态 返回给$? 取值范围 0-255 (使用 return)

返回数字作为函数执行结果 (使用 echo)

返回字符串,作为函数执行结果 (使用 echo)

系统函数库

系统函数库

/etc/init.d/functions

action函数

action "xxxxx" /bin/trueaction "xxxxx" /bin/false

功能函数库 # 脚本初始化 function scripts_init(){ prog=`basename $0 .sh` LockFile=/var/lock/subsys/${prog}.lock # 使用锁文件 LogFile=/var/log/${prog}.log # 脚本记录日志 PidFile=/var/run/${prog}.pid # 记录进程号,可以管理脚本 [ -f $LockFile ] && echo "There $LockFile is exist!!" && exit 1 ||touch $LockFile [ ! -f $LogFile ] && touch $LogFile [ -f $PidFile ] && echo "There $PidFile is exist!!" && exit 2|| echo $$ > $PidFile } # 记录日志 function writelog(){ Date=$(date "+%F_%T") ShellName=`basename $0` Info=$1 echo "$Date : ${ShellName} : ${Info}" >> ${LogFile} } # 脚本退出扫尾 function closeout(){ [ -f $LockFile ] && rm -f $LockFile [ -f $PidFile ]&& rm -f $PidFile } # 判断输入是整数 function int_judge(){ fun_a=$1 expr $fun_a + 1 &>/dev/null RETVAL=$? return $RETVAL } # 判断输入非空 function input_judge(){ RETVAL=0 fun_a=$1 [ ${#fun_a} -eq 0 ]&& RETVAL=1 return $RETVAL }  数组

数组名代表首地址

下标是从0开始的整数

普通数组(索引数组)

定义数组 array=(a b c) 获取所有元素 ${array[*]} 获取元素下标 ${!a[@]} 获取数组长度 ${#array[*]} 获取一个元素的长度 ${#name[0]} 获取第一个元素 ${array[0]} 获取第二个元素 ${array[1]} 获取多个元素 array=(1 2 3 4 5) echo ${array[@]:1} 2 3 4 5 #array[@]:下标:截取元素个数 echo ${array[@]:1:3} 2 3 4 添加元素 array[3]=d 添加多个元素 array+=(e f g) 删除第一个元素 unset array[0] # 删除会保留元素下标 删除数组 unset array 数组切片 ${name[4]:0:7} 关联数组

关联数组的下标是字符串

定义关联数组 declare -A M 关联数组赋值 M=([a]=11 [b]=22) 获取关联数组元素 echo ${M[a]} 11 获取关联数组元素个数 echo ${#M[@]} 2 获取关联数组下标 echo ${!M[*]} a b 获取关联数组所有元素 echo ${M[@]} 11 22 添加元素 M[c]=33 添加多个元素 M+=([d]=33 [e]=44) 综合题

找出一个网络中最大的空闲ip地址段

#!/bin/bash SUB_NET="10.0.0." for NUM in {1..254} do { if ping -c 1 ${SUB_NET}${NUM} &> /dev/null;then echo $NUM >> ip.txt fi }& done wait sort -n ip.txt -o ip.txt if [ $(tail -1 ip.txt) -ne 254 ];then echo 254 >> ip.txt fi while read LINE do array+=($LINE) done < ip.txt for ((i=0;i<${#array[*]}-1;i++)) do DIFF=$((${array[$((i+1))]}-${array[$i]})) echo -e "${SUB_NET}${array[$((i+1))]}\t${SUB_NET}${array[$i]}\t$DIFF" >> result.txt done sort -rnk3 result.txt rm *.txt -f #!/bin/bash SUB_NET="10.0.0." for NUM in {1..254} do { if ping -c 1 ${SUB_NET}${NUM} &> /dev/null;then echo $NUM >> ip.txt fi }& done wait sort -n ip.txt -o ip.txt if [ $(tail -1 ip.txt) -ne 254 ];then echo 254 >> ip.txt fi awk -v net=$SUB_NET '{if(NR==1){last=$1}if(NR>1){printf "%s%d\t%s%d\t%d\n",net,$1,net,last,$1-last;last=$1}}' ip.txt |sort -nrk 3 rm ip.txt -f

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/189721ecd4cb9ca7dabc34afde2ff454.html