您的位置:首页 > 运维架构 > Shell

shell编程输入输出,控制结构,函数

2013-09-15 19:29 761 查看
shell编程输入输出和控制结构
一,shell输入和输出
1,echo
[root@test3 tmp]# vim a.sh
#!/bin/bash
echo  "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.sh
sss\n
{sssn}
sss\nsss
nihao dajia
ccc 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/bash
read param1
read param2
read 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.sh
first
second
third    输入到这里回车就显示了很明显param4没有接受参数
first:  first
second: second
third:  third
forth:
[root@test3 tmp]#
[root@test3 tmp]# ./b.sh
aa
bb cc dd
ee            这个时候param4任然没有接受参数
first:  aa
second: bb cc dd
third:  ee
forth:
[root@test3 tmp]# ./b.sh
aa
bb cc dd
ee ff              这时候param接收到了参数
first:  aa
second: bb cc dd
third:  ee
forth:  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/sda2
tmpfs
/dev/sda1
[root@test3 tmp]# df -h|awk '{print $1}'|grep -v Filesystem
/dev/sda2
tmpfs
/dev/sda1
[root@test3 tmp]# df -h|awk '{print $1}'|grep -v Filesystem|tee c.txt      tee是将标准输出输入到文件
/dev/sda2
tmpfs
/dev/sda1
[root@test3 tmp]# cat c.txt
/dev/sda2
tmpfs
/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/sda2
tmpfs
/dev/sda1
/dev/sda2
tmpfs
/dev/sda1


4,重定向
[root@test3 tmp]# vim d.txt
[root@test3 tmp]# cat d.txt
nihaoma
屏幕的输入通过命令cat追加到d.txt 当遇到EOF就停止
[root@test3 tmp]# cat >> d.txt << EOF
> this is test
> i l u
> EOF
[root@test3 tmp]# cat d.txt
nihaoma
this is test
i l u
[root@test3 tmp]# cat >> d.txt << EOF
> $LOGNAME           环境变量被解释
> EOF
[root@test3 tmp]# cat d.txt
nihaoma
this is test
i l u
root
d.txt定向输入到sort的输入进行排序,然后输出到e.txt
[root@test3 tmp]# sort < d.txt > e.txt
[root@test3 tmp]# cat e.txt
i l u
nihaoma
root
this 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大于等于整数2
test 整数1 –gt 整数2                 整数1大于整数2
test 整数1 –le 整数2                 整数1小于等于整数2
test 整数1 –lt 整数2                 整数1小于整数2
test 整数1 –ne 整数2                 整数1不等于整数2
4)判断文件
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               文件存在并且是设置了组ID
test –G File               文件存在并且属于有效组ID
test –h File              文件存在并且是一个符号链接(同-L)
test –k File              文件存在并且设置了sticky位
test –b File               文件存在并且是块设备文件
test –L File             文件存在并且是一个符号链接(同-h)
test –o File              文件存在并且属于有效用户ID
test –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.sh
please input username:root
please input password:123
#!/bin/bash
echo -n "please input username:"
read  username
echo -n "please input password:"
read  password
if [ $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/bash
if cp my.file my.file.back  判断这个命令是否成功
then
echo  "successful"
else
echo " copy file fail" >&2
fi
[root@test3 tmp]# ./b.sh
cp: cannot stat `my.file': No such file or directory
copy file fail


2,case语句




[root@test3 tmp]# ./c.sh
Enter a number from 1 to 3:3
you select 3
[root@test3 tmp]# cat c.sh
#!/bin/bash
echo -n "Enter a number from 1 to 3:"
read ANS
case $ANS in
1)
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.sh
123456[root@test3 tmp]# cat e.sh
#!/bin/bash
for loop in 1 2 3 4 5 6
do
echo -n "$loop"
done
[root@test3 tmp]# cat f.sh
#!/bin/bash
for  ss in red black orage  这里是将red black orage依次输入给 ss
do
echo "$ss"
done
for  ww in  "red black orage" 这里是将red black orage作为一个整体给ww
do
echo "$ww"
done
[root@test3 tmp]# chmod +x f.sh
[root@test3 tmp]# ./f.sh
red
black
orage
red black orage
以文件的内容作为for的值,文件中的字符以空格或者回车作为分割
[root@test3 tmp]# cat g.sh
#!/bin/bash
for file in  `cat /tmp/dd.txt`
do
echo $file
done
[root@test3 tmp]# ./g.sh
param1
param2
param3
param4
param5
[root@test3 tmp]# cat  dd.txt
param1 param2 param3
param4
param5


4,until语句



监控磁盘
监控磁盘的使用情况,如果大于10%那么就给root用户发邮件
[root@test3 tmp]# vim a.sh
#!/bin/bash
sda1=`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" ]
do
sda1=`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 root
done


5,while语句



如果命令为假就停止循环也可以break来退出循环

[root@test3 tmp]# cat b.sh
#!/bin/bash
while read line
do
echo $line
echo
done  < bb.txt    将一个文件内容作为while的输入
[root@test3 tmp]# cat bb.txt
red
black
orage
white
[root@test3 tmp]# ./b.sh
red
black
orage
white


三,函数
函数不能的返回值不能赋值给一个变量
[root@test3 tmp]# cat fun.sh
#!/bin/bash
function 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.sh
now going function
HELLO,WELCOME,today is 2013-09-15
number of parameters is 3
name is chen
sex is man
country is china
string is chen man china
status of function is good
function end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell
相关文章推荐