shell编程输入输出和控制结构
一,shell输入和输出
1,echo
[root@test3 tmp]# vim a.sh#!/bin/bashecho "sss\n" 原样输出,但是不输出“”echo {sss\n} 原样输出,但是不输出\echo -n "sss\n" 输出不换行,默认换行echo -e "sss\n" 解释转义字符,并且显示echo nihao dajia 原样输出echo ccc * 输出ccc 和当前目录下的文件[root@test3 tmp]# chmod +x a.sh[root@test3 tmp]# ./a.shsss\n{sssn}sss\nsssnihao dajiaccc a a.sh io.sh keyring-KY2TBC keyring-p0MelO memcached.pid mysql.sock orbit-gdm orbit-root pear pulse-0uxLzihOzSmN pulse-1NOdSg3wjC4T sess_fuka99ealkkr93ppoafkupknu4geag5l sess_tf00c1g2e2bncte688o776c9ilc96eq5 sh-thd-1378728692 virtual-root.8TPViI virtual-root.9HGFqQ virtual-root.Dx7gP2 virtual-root.izuZCV virtual-root.okJZiV virtual-root.rtOlcM virtual-root.tIZkT5 virtual-root.Ulx4o4 virtual-root.vD4M2H virtual-root.VJwEGD virtual-root.wQyQR8 virtual-root.y1nv6k VMwareDnD vmware-root vmware-root-3794476792 xdg-screensaver-root--0.0
2,read
偶
如果read只指定一个变量,那么将会把所有的输入付给该变量,直到遇到回车或者是文件结束符。 如果read指定多个变量,那么将按照顺序将输入付给变量,以空格作为这些变量的分隔符[root@test3 tmp]# vim b.sh#!/bin/bashread param1read param2read param3 param4 echo "first: $param1"echo "second: $param2"echo "third: $param3"echo "forth: $param4"[root@test3 tmp]# chmod +x b.sh[root@test3 tmp]# ./b.shfirstsecondthird 输入到这里回车就显示了很明显param4没有接受参数first: firstsecond: secondthird: thirdforth:[root@test3 tmp]#[root@test3 tmp]# ./b.shaabb cc ddee 这个时候param4任然没有接受参数first: aasecond: bb cc ddthird: eeforth:[root@test3 tmp]# ./b.shaabb cc ddee ff 这时候param接收到了参数first: aasecond: bb cc ddthird: eeforth: ff
3,管道
将磁盘的消息打印到文本文件c.txt[root@test3 tmp]# df -h|awk "{print $1}" 双引号是错的Filesystem Size Used Avail Use% Mounted on/dev/sda2 19G 4.1G 14G 24% /tmpfs 495M 276K 495M 1% /dev/shm/dev/sda1 291M 30M 246M 11% /boot[root@test3 tmp]# df -h|awk '{print $1}' 单引号是对的Filesystem/dev/sda2tmpfs/dev/sda1[root@test3 tmp]# df -h|awk '{print $1}'|grep -v Filesystem/dev/sda2tmpfs/dev/sda1[root@test3 tmp]# df -h|awk '{print $1}'|grep -v Filesystem|tee c.txt tee是将标准输出输入到文件/dev/sda2tmpfs/dev/sda1[root@test3 tmp]# cat c.txt/dev/sda2tmpfs/dev/sda1[root@test3 tmp]# df -h|awk '{print $1}'|grep -v Filesystem|tee -a c.txt tee -a 是将标准输出追加输入到文件/dev/sda2 tmpfs/dev/sda1[root@test3 tmp]# cat c.txt/dev/sda2tmpfs/dev/sda1/dev/sda2tmpfs/dev/sda1
4,重定向
[root@test3 tmp]# vim d.txt[root@test3 tmp]# cat d.txtnihaoma 屏幕的输入通过命令cat追加到d.txt 当遇到EOF就停止[root@test3 tmp]# cat >> d.txt << EOF> this is test> i l u> EOF[root@test3 tmp]# cat d.txtnihaomathis is testi l u[root@test3 tmp]# cat >> d.txt << EOF> $LOGNAME 环境变量被解释> EOF[root@test3 tmp]# cat d.txtnihaomathis is testi l uroot d.txt定向输入到sort的输入进行排序,然后输出到e.txt[root@test3 tmp]# sort < d.txt > e.txt[root@test3 tmp]# cat e.txti l unihaomarootthis is test
二,控制结构
1,if语句
shell test用法1)判断表达式if test (表达式为真)if test !表达式为假test 表达式1 –a 表达式2 两个表达式都为真test 表达式1 –o 表达式2 两个表达式有一个为真2)判断字符串test –n 字符串 字符串的长度非零test –z 字符串 字符串的长度为零test 字符串1=字符串2 字符串相等test 字符串1!=字符串2 字符串不等3)判断整数test 整数1 –eq 整数2 整数相等test 整数1 –ge 整数2 整数1大于等于整数2test 整数1 –gt 整数2 整数1大于整数2test 整数1 –le 整数2 整数1小于等于整数2test 整数1 –lt 整数2 整数1小于整数2test 整数1 –ne 整数2 整数1不等于整数24)判断文件test File1 –ef File2 两个文件具有同样的设备号和i结点号test File1 –nt File2 文件1比文件2 新test File1 –ot File2 文件1比文件2 旧test –b File 文件存在并且是块设备文件test –c File 文件存在并且是字符设备文件test –d File 文件存在并且是目录test –e File 文件存在test –f File 文件存在并且是正规文件test –g File 文件存在并且是设置了组IDtest –G File 文件存在并且属于有效组IDtest –h File 文件存在并且是一个符号链接(同-L)test –k File 文件存在并且设置了sticky位test –b File 文件存在并且是块设备文件test –L File 文件存在并且是一个符号链接(同-h)test –o File 文件存在并且属于有效用户IDtest –p File 文件存在并且是一个命名管道test –r File 文件存在并且可读test –s File 文件存在并且是一个套接字test –t FD 文件描述符是在一个终端打开的test –u File 文件存在并且设置了它的set-user-id位test –w File 文件存在并且可写test –x File 文件存在并且可执行
系统登录
[root@test3 tmp]# vim a.sh[root@test3 tmp]# ./a.shplease input username:rootplease input password:123#!/bin/bashecho -n "please input username:"read usernameecho -n "please input password:"read passwordif [ $username != "root" ] [] 之间要有空格 then echo "your username error" elif [ $password != "123" ] then echo "your password error" else echo " login successful"fi
cp命令
[root@test3 tmp]# cat b.sh#!/bin/bashif cp my.file my.file.back 判断这个命令是否成功 then echo "successful" else echo " copy file fail" >&2fi[root@test3 tmp]# ./b.shcp: cannot stat `my.file': No such file or directory copy file fail
2,case语句
[root@test3 tmp]# ./c.shEnter a number from 1 to 3:3you select 3[root@test3 tmp]# cat c.sh#!/bin/bashecho -n "Enter a number from 1 to 3:"read ANScase $ANS in1) echo "you select 1" ;;2) echo "you select 2" ;;3) echo "you select 3" ;;*) echo "error" >&2 exit ;;esac
3,for语句
[root@test3 tmp]# chmod +x e.sh[root@test3 tmp]# ./e.sh123456[root@test3 tmp]# cat e.sh#!/bin/bashfor loop in 1 2 3 4 5 6do echo -n "$loop"done[root@test3 tmp]# cat f.sh#!/bin/bashfor ss in red black orage 这里是将red black orage依次输入给 ssdo echo "$ss"donefor ww in "red black orage" 这里是将red black orage作为一个整体给wwdo echo "$ww"done[root@test3 tmp]# chmod +x f.sh[root@test3 tmp]# ./f.shredblackoragered black orage以文件的内容作为for的值,文件中的字符以空格或者回车作为分割[root@test3 tmp]# cat g.sh#!/bin/bashfor file in `cat /tmp/dd.txt`do echo $filedone[root@test3 tmp]# ./g.shparam1param2param3param4param5[root@test3 tmp]# cat dd.txtparam1 param2 param3param4param5
4,until语句
监控磁盘
监控磁盘的使用情况,如果大于10%那么就给root用户发邮件[root@test3 tmp]# vim a.sh#!/bin/bashsda1=`df|grep sda1|awk '{print $5}'|sed 's/%//g'`sda2=`df|grep sda2|awk '{print $5}'|sed 's/%//g'`until [ $sda1 -gt "10" ] || [ $sda2 -gt "10" ]dosda1=`df|grep sda1|awk '{print $5}'|sed 's/%//g'`sda2=`df|grep sda2|awk '{print $5}'|sed 's/%//g'` sleep 60 echo "your filesystem is full" |mail rootdone
5,while语句
如果命令为假就停止循环也可以break来退出循环
[root@test3 tmp]# cat b.sh#!/bin/bashwhile read linedo echo $lineechodone < bb.txt 将一个文件内容作为while的输入[root@test3 tmp]# cat bb.txtredblackoragewhite[root@test3 tmp]# ./b.shredblackoragewhite
三,函数
函数不能的返回值不能赋值给一个变量
[root@test3 tmp]# cat fun.sh#!/bin/bashfunction hello(){ echo "HELLO,WELCOME,today is `date +%Y-%m-%d`" echo "number of parameters is $#" 传参的个数 echo "name is $1" 第一个参数 echo "sex is $2" 第二个参数 echo "country is $3" 第三个参数 echo "string is $*" 所有参数的集合 if [ $? == 0 ] 为0 表示程序成功执行 then echo "status of function is good" else echo "error" fi}echo "now going function"hello chen man china 调用函数echo "function end"[root@test3 tmp]# ./fun.shnow going functionHELLO,WELCOME,today is 2013-09-15number of parameters is 3name is chensex is mancountry is chinastring is chen man chinastatus of function is goodfunction end