$?
[ ]
整数判断
-eq equal [ 1 -eq 1 ]
-ne not equal
-lt less than
-le less and equal
-gt greater than
-ge greater and equal
#echo $?
0
[test]#[ 1 -eq 2 ]
[test]#echo $?
1
#[ 2 -ne 3 ]
You have new mail in /var/spool/mail/root
[test]#echo $?
0
字符串判断
[ string1 == string2 ] 是否相同
[ string1 != string2 ] 是否不相同
[ string ] 判断字符串是否不为空
[ -z string ] 判断字符串长度是否为零
[ -n string ] 判断字符串长度是否不为零
[test]#[ abc == def ]
You have new mail in /var/spool/mail/root
[test]#echo $?
1
[test]#[ abc != def ]
[test]#echo $?
0
[test]#[ abc ]
You have new mail in /var/spool/mail/root
[test]#echo $?
0
[test]#[ "" ]
[test]#echo $?
1
[test]#[ -z abc ]
You have new mail in /var/spool/mail/root
[test]#echo $?
1
[test]#[ -z "" ]
[test]#echo $?
0
[test]#[ -n abc ]
You have new mail in /var/spool/mail/root
[test]#echo $?
0
[test]#[ -n "" ]
[test]#echo $?
1
逻辑判断
-a [ expr1 -a expr2 ] // and 两个条件同时满足
-o [ expr1 -o expr2 ] // or 两个条件成立一个就行
! [ ! expr ] //取反
[test]#[ 2 -gt 1 -a 3 -gt 2 ]
[test]#echo $?
0
[test]#[ 2 -gt 1 -a 3 -gt 4 ]
[test]#echo $?
1
[test]#[ 2 -gt 1 -o 3 -gt 4 ]
[test]#echo $?
0
[test]#[ ! 2 -gt 1 ]
[test]#echo $?
1
-------------
[[ patten ]]
[[ patten1 && patten2 ]]
[[ patten1 || patten2 ]]
[test]#[[ abc == [ab]bc ]]
You have new mail in /var/spool/mail/root
[test]#echo $?
0
[test]#[[ bbc == [ab]bc ]]
[test]#echo $?
0
[test]#[[ cbc == [ab]bc ]]
[test]#echo $?
1
[test]#[[ abc == [ab]bc && bbc == [ab]bc ]]
You have new mail in /var/spool/mail/root
[test]#echo $?
0
[test]#[[ abc == [ab]bc || cbc == [ab]bc ]]
You have new mail in /var/spool/mail/root
[test]#echo $?
0
-------------------
文件的判断
-a file 如果文件存在,那么为真
[test]#[ -a /etc/passwd ]
You have new mail in /var/spool/mail/root
[test]#echo $?
0
练习:
把/etc/*.conf 打包压缩以后,备份到 /backup 目录,gzip格式,一当天的日期加上etc2bak 20130828etc2bak.tar.gz
# vim sh09.sh
#!/bin/bash
if [ ! -d /backup ]
then
mkdir /backup
fi
date=`date "+%Y%m%d"`
echo $date
if [ ! -e /backup/${date}etc2bak.tar.gz ]
then
tar zcf /backup/${date}etc2bak.tar.gz /etc/*.conf
else
exit 1
fi
# chmod +x sh09.sh
# ./sh09.sh
练习:
输入一个整数, 现判断一下是否是分数。如果不是分数 显示“请输入分数”。 如果分数100~90,显示“优秀”,如果分数89~80, 显示“中等”,如果分数79~70,显示“加油”,剩余的“要好好努力才行啊”
#vim s10.sh
#!/bin/bash
read -p "请输入分数:" num
if [ $num -le 100 -a $num -ge 0 ]
then
if [ $num -le 100 -a $num -ge 90 ]
then
echo "优秀"
elif [ $num -lt 90 -a $num -ge 80 ]
then
echo "中等"
elif [ $num -lt 80 -a $num -ge 70 ]
then
echo "加油"
else
echo "要好好努力才行啊"
fi
else
echo "请输入分数"
fi
-------------------
case 多分支判断
格式
case $var in
value1)
command;
command;
;;
value2)
command;
command;
;;
value3)
command;
command;
;;
*)
command;
command;
;;
esac
[test]#vim s11.sh
#!/bin/bash
read -p "请输入用户名:" username
case $username in
root)
echo "你好 root";
;;
mike)
echo "你好 mike";
;;
tom)
echo "你好 tom";
;;
*)
echo "请回吧~"
;;
esac
[test]#chmod +x s11.sh
[test]#./s11.sh
请输入用户名:root
你好 root
[test]#./s11.sh
请输入用户名:tom
你好 tom
[test]#./s11.sh
请输入用户名:mike
你好 mike
[test]#./s11.sh
请输入用户名:john
请回吧~
[test]#vim s12.sh
#!/bin/bash
read -p "请输入用户名:" username
read -s -p "请输入密码:" password
case $username,$password in
root,redhat)
echo "欢迎光临,管理员";
;;
*)
echo "请离开这里,私人领域";
;;
esac
# chmod +x s12.sh
#./s12.sh
请输入用户名:root
请输入密码:
欢迎光临,管理员
-------------------------
循环
for
语法
for 变量 in 取值范围(一组值)
do
command
command
done
取值范围
1) 直接写
#!/bin/bash
for i in a b c d e f g h
do
echo $i
done
[test]#./s13.sh
a
b
c
d
e
f
g
h
2) 位置参数传递
[test]#vim s14.sh
#!/bin/bash
for i in "$*"
do
echo $i
done
echo "-----------"
for j in "$@"
do
echo $j
done
[test]#./s14.sh a b c d e f g
a b c d e f g
-----------------
a
b
c
d
e
f
g
3) 省略的写法
[test]#vim s15.sh
#!/bin/bash
for i in {1..9}
do
echo $i
done
[test]#chmod +x s15.sh
[test]#./s15.sh
1
2
3
4
5
6
7
8
9
4) 使用系统命令
# vim s16.sh
#!/bin/bash
for i in `seq 1 10`
do
echo $i
done
#chmod +x s16.sh
[test]#./s16.sh
1
2
3
4
5
6
7
8
9
10
# vim s16.sh
#!/bin/bash
for i in `seq 1 2 10`
do
echo $i
done
# vim s17.sh
#!/bin/bash
for i in `awk -F: '{print $1}' /etc/passwd`
do
echo $i
done
# vim s18.sh
#!/bin/bash
for username in `awk -F: '{print $1}' /etc/passwd`
do
uid=`id $username|awk -F "=" '{print $2}'|awk -F "(" '{print $1}'`
#uid=`id $username|awk -F "[=(]" '{print $2}'`
if [ $uid -eq 0 ]
then
echo "$username 超级用户"
elif [ $uid -gt 0 -a $uid -lt 500 ]
then
echo "$username 系统用户"
else
echo "$username 普通用户"
fi
done
#./s18.sh
root 超级用户
bin 系统用户
daemon
adm 普通用户
lp
sync
shutdown
halt
mail
练习:找出局域网中存活的ip个数,把存活的ip追加到/test/ip文件中
# vim s19.sh
#!/bin/bash
touch /test/ip
num=0
wd=`ifconfig|grep 'Bcast'|grep '255\.255\.255\.0'|awk -F: '{print $2}'|awk -F. '{print $1"."$2"."$3"."}'`
for ip in `seq 1 254`
do
ping -c 1 $wd$ip &>/dev/null
if [ $? -eq 0 ]
then
echo $wd$ip >> /test/ip
num=$[$num+1]
fi
done
echo "存活的ip个数是 $num"
练习: 99乘法表
# vim s20.sh
#!/bin/bash
for i in `seq 1 9`
do
for j in `seq 1 $i`
do
echo -n $i*$j"="$[$i*$j]" "
fi
done
echo
done
------------------------------------
while
语法
while [ 条件表达式 ]
do
commands //当条件表达式为真,执行命令;假的话,结束循环
变量的更新
done
# vim s22.sh
#!/bin/bash
i=1
while [ $i -le 3 ]
do
id q$i &>/dev/null
if [ $? -ne 0 ]
then
useradd q$i
fi
i=$[$i+1]
echo $i
done
# chmod 777 s22.sh
# ./s22.sh
------------
until
语法
until [ 条件表达式 ]
do
command
变量更新
done
# vim s25.sh
#!/bin/bash
i=1
until [ $i -gt 3 ]
do
id q$i &>/dev/null
if [ $? -ne 0 ]
then
useradd q$i
fi
i=$[$i+1]
echo $i
done