linux下shell脚本编程2推荐

1、 if 判断一些特殊用法

if [ -z $a ] 这个表示当变量a的值为空时会怎么样

if [ ! -e file ]; then 表示文件不存在时会怎么样

if (($a then …等同于 if [ $a -lt 1 ]; then… [ ] 中不能使用 , ,==,!=, =, =这样的符号

$3不存在,所以n为空;判断是否为空;

[root@yong~]#n=`wc-l/etc/passwd|awk'{print$3}'`[root@yong~]#echo$n[root@yong~]#if[-z$n];thenecho"\$nisnull";fi$nisnull

if grep -q ‘123’ 1.txt; then 表示如果1.txt中含有’123’的行时会怎么样?

1.txt中含有123;grep -q ‘123’ 1.txt 匹配OK,返回值为真;

[root@localhostshell]#cat1.txt[root@localhostshell]#ifgrep-q"123"1.txt;thenechokong;fikong

2、 shell中的case判断

格式:case变量名invalue1)command;;value2)command;;*)commond;;esac在case程序中,可以在条件中使用|,表示或的意思,比如command;;当变量为2或者3时,执行该部分命令。

/etc/init.d/naginx 里面有case语句;匹配输入的第一个参数是否为start stop reload restart conifgtest,输入其他字符则返回

Usage: /etc/init.d/nginx {start|stop|reload|restart|configtest}

case"$1"instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo$"Usage:$0{start|stop|reload|restart|configtest}"RETVAL=1esac

举例,输入的是字母,提示输入一个纯数字,输入数字判断是偶数或奇数;

[root@localhost0618]#catcase1.sh#!/bin/bash#要求输入的是数字,判断奇数或偶数;非数字则提示输入数字,然后退出;read-p"pleaseinputanumber:"nn1=`echo$n|sed's/[0-9]//g'`#输入为数字则sed替换为空,返回值为空;输入为字母则返回值不为空;if[!-z$n1]thenecho"pleaseinputanumber"exit1n2=$[$n%2]case$n2in0)echo"偶数";;1)echo"奇数";;*)echo"不存在";;esac
[root@localhost0618]#sh-xcase1.sh+read-p'pleaseinputanumber:'npleaseinputanumber:++echode2++sed's/[0-9]//g'+n1=de+'[''!'-zde']'+echo'pleaseinputanumber'pleaseinputanumber+exit1[root@localhost0618]#shcase1.shpleaseinputanumber:234[root@localhost0618]#shcase1.shpleaseinputanumber:wepleaseinputanumber

case实验2:输入为空则提示输入数字然后退出;输入的数字在0-100内判断成绩;输入为非数字则提示输入数字然后退出;

输入负数和大于100的都会提示输入的数字为0-100;

#!/bin/bash#case实验,输入为空提示输入数字然后退出;输入的数字在0-100内判断成绩;输入的非数字提示输入数字然后退出;read-p"pleaseinputanumber:"nif[-z$n]thenecho"Pleaseinputanumber"exit1n1=`echo$n|sed's/[-0-9]//g'`#sed替换加了-表示负数;if[!-z$n1]echo"pleaseinputanumber"exit1if[$n-ge0] [$n-lt60]thentag=1elif[$n-ge60] [$n-lt80]tag=2elif[$n-ge80] [$n-lt90]tag=3elif[$n-ge90] [$n-le100]tag=4tag=0case$tagin1)echo"不及格";;2)echo"及格";;3)echo"良好";;4)echo"优秀";;*)echo"输入的数字为0-100";;esac
[root@localhost0618]#shcase2.shpleaseinputanumber:-200输入的数字为0-100[root@localhost0618]#shcase2.shpleaseinputanumber:101输入的数字为0-100

3、 shell脚本中的循环

for循环 语法结构:for 变量名 in 条件; do … done

while 循环语法结构: while 条件; do … done 死循环用:表示

break直接结束本层循环; continue忽略continue之下的代码,直接进行下一次循环

exit 直接退出shell

for循环实验:列出/etc目录下所有的目录

[root@yong~]#catfor.sh#!/bin/bashforfin`ls/etc/`if[-d/etc/$f]ls-d"/etc/$f"done

while循环实验:判断负载的循环;

[root@yong~]#catload.sh#!/bin/bash#监测负载的脚本,取w负载1分钟内的负载值如果大于10,则30秒发一次邮件;while:load=`w|head-1|awk-F"loadaverage:"'{print$2}'|cut-d.-f1`if[$load-gt10]top|mail-s"loadishigh:$load"xxx@163.comexit0sleep30done

while循环实验:

如果输入为空,提示要求输入东西,如果输入字符提示输入纯数字,输入纯数字打印数字,退出;

[root@localhost0618]#catwhile.sh#!/bin/bash#输入为空,提示输入东西一直到输入不为空结束;如输入的是字母则提示只能输入一个纯数字,直到输入纯数字为止,打印数字结束;while:read-p"pleaseinputanumber:"nif[-z$n]thenecho"请输入一个东西"continuen1=`echo$n|sed's/[-0-9]//g'`if[!-z$n1]thenecho"请输入一个纯数字"continuebreakecho$n

continue 退出本次循环;循环内部继续,不执行循环后面的了;

break跳出整个循环,循环后面的还会执行。

exit的话退出整个脚本;

break实验:条件匹配的话退出整个循环;

[root@localhost0618]#catbreak.sh#!/bin/bash#break实验;条件匹配的话退出整个循环;循环后面的还会执行;foriin`seq15`echo$iif[$i==3]thenbreakecho$iechoOK
[root@localhost0618]#shbreak.shOK

continue实验;条件匹配的话退出本次循环,继续执行循环;

#!/bin/bashforiin`seq15`echo$iif[$i==3]thencontinuefiecho$iechoOK
[root@localhost0618]#shcontinue.shOK

exit实验;条件匹配的话退出整个脚本;

#!/bin/bashforiin`seq15`echo$iif[$i==3]thenexit1fiecho$iechoOK
[root@localhost0618]#shbreak.sh3

4、shell中的函数

函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可。

格式: function f_name() {

command

}

函数必须要放在最前面

函数里可以export 全局变量;

函数实验1:定义input函数,输入一个字符,则打印一个字符;

#!/bin/bashinput(){echo$1}inputyonglinux[root@yong~]#sh1.shyonglinux

函数实验2:定义sum函数,进行求和运算;

#!/bin/bashsum(){s=$[$1+$2]echo$ssum12[root@localhost0618]#sh2.sh3

查看ip地址的函数;

#!/bin/bash#查看ip地址的函数;输入一个网卡名,输出网卡对应的ip地址;$1为交互时输入的网卡名;ip(){ifconfig|grep-A1"$1"|tail-1|awk'{print$2}'|awk-F':''{print$2}'read-p"pleaseinputtheethname:"emyip=`ip$e`echo"$eaddressis$myip"
[root@localhost0618]#sh2.shpleaseinputtheethname:eth0eth0addressis192.168.11.100[root@localhost0618]#sh2.shpleaseinputtheethname:eth1eth1addressis192.168.20.100[root@localhost0618]#sh2.shpleaseinputtheethname:loloaddressis127.0.0.1

旅行要学会随遇而安,淡然一点,走走停停,

linux下shell脚本编程2推荐

相关文章:

你感兴趣的文章:

标签云: