1 条件判断式语句
判断语句 两种方式 :
[ -e /etc/passwd ]
test -e /etc/passwd
[root@aliyun247 ~]# test -e /etc/passwd
[root@aliyun247 ~]# echo $?
0
[root@aliyun247 ~]# test -e /etc/passwdddddd
[root@aliyun247 ~]# echo $?
1
[root@aliyun247 ~]# test -e /etc/passwdddddd && echo "yes" || echo "no"
no
[root@aliyun247 ~]# test -e /etc/passwd && echo "yes" || echo "no"
yes
[root@aliyun247 ~]# [ -e /etc/passwd ] && echo "yes" || echo "no"
yes
[root@aliyun247 ~]# [ -e /etc/passwddddd ] && echo "yes" || echo "no"
no
判断文件是否有读写权限
[root@aliyun247 ~]# [ -w /etc/passwddddd ] && echo "yes" || echo "no"
no
[root@aliyun247 ~]# [ -w /etc/passwd ] && echo "yes" || echo "no"
yes
[root@aliyun247 ~]# [ -w test/1.txt ] && echo "yes" || echo "no"
yes
[root@aliyun247 ~]#
[root@aliyun247 ~]# [ -r test/1.txt ] && echo "yes" || echo "no"
yes
创建硬链接 硬链接的inode 节点是一致的
[root@aliyun247 ~]# ln test/1.txt dxm/1.txt
[root@aliyun247 test]# ll -i
total 63552
1092102 -rw-r--r-- 1 root root 1353 Mar 26 13:57 11.txt
1092147 -rw-r--r-- 1 root root 2706 Mar 26 14:49 12.txt
1054888 -rw-r--r-- 2 root root 131 May 25 15:00 1.txt
root@aliyun247 dxm]# ll -i
total 4
1054888 -rw-r--r-- 2 root root 131 May 25 15:00 1.txt
[root@aliyun247 dxm]# [test/1.txt -ef dxm/1.txt ] && echo "yes" || echo "no"
bash: [test/1.txt: No such file or directory
no
[root@aliyun247 ~]# [ /test/1.txt -ef dxm/1.txt ] && echo "yes" || echo "no"
no
应该是yes才对啊 ,
[root@aliyun247 ~]# [ 22 -ge 33 ] && echo "yes" || echo "no"
no
[root@aliyun247 ~]# [ 34 -ge 33 ] && echo "yes" || echo "no"
yes
[root@aliyun247 ~]# [ 34 -gt 33 ] && echo "yes" || echo "no"
yes
[root@aliyun247 ~]# [ 34 -lt 33 ] && echo "yes" || echo "no"
no
[root@aliyun247 ~]# [ 34 -le 83 ] && echo "yes" || echo "no"
yes
[root@aliyun247 ~]# [ 34 -eq 83 ] && echo "yes" || echo "no"
no
[root@aliyun247 ~]# [ 83 -eq 83 ] && echo "yes" || echo "no"
yes
[root@aliyun247 ~]# ["$aa" == "$bb"]&& echo "yes" ||echo "no"
bash: [22: command not found
no
[root@aliyun247 ~]# ["$aa" -eq "$bb"]&& echo "yes" ||echo "no"
bash: [22: command not found
no
[root@aliyun247 ~]# [ "$aa" -eq "$bb" ] && echo "yes" ||echo "no"
yes
[root@aliyun247 ~]# [ "$aa" == "$bb" ] && echo "yes" ||echo "no"
yes
注意:多次测试 开始输入内容是错误的,调整格式 ,多次调整格式之后才发现 输出正确。[] ,== ,eq等符号与 变量之间 要有空格
a and
o or
!
root@aliyun247 ~]# [ -n "$aa" -a "$bb" ge "13" ] && echo "yes" || echo "no"
bash: [: too many arguments
no
[root@aliyun247 ~]# [ -n "$aa" -a "$bb" -ge "13" ] && echo "yes" || echo "no"
yes
[root@aliyun247 ~]# [ -n "$aa" -a "$bb" -ge "23" ] && echo "yes" ||echo "no"
no
注意:在语法格式上有错时,把 -ge 写成 ge 时 报 too many arguments 并且输出 no ,没办法 错误的方式只能得到错误的结果。
2 单分支 if 语句
建立编程思想
1 熟悉基本liunx命令 规范 语法和shell编程语法
2 操练 ,背的滚瓜烂熟
3 能动手写出。
if [ 条件判断 ] ; then
程序
fi
或者
if [ 条件判断 ]
then
程序
fi
注意点 1 if 语句使用fi 结尾,和一般语言使用大括号结尾 不一样。
2 [ 条件判断式 ] 就是使用test 命令式判断 ,所以 中括号 和条件判断式 之间必须有空格,
3 then 后面跟符合条件之后的 执行程序 ,也可以放在 [ ] 之后 ,用;分隔。 也可以换行写入 就不需要;了。
#!/bin/bash
test=$( env |grep "USERNAME" |cut -d "=" -f 2)
if [ "$test" == "root" ]
then
echo "current user is root"
fi
~
[root@aliyun247 test]# env |grep "USER" |cut -d "=" -f 2
root
[root@aliyun247 test]# vim if2.sh
[root@aliyun247 test]# chmod 755 if2.sh
[root@aliyun247 test]# ./if2.sh
/ is full ,please deal with it
[root@aliyun247 test]# cat if2.sh
#!/bin/bash
test=$(df -h|grep "/dev/xvda1" |awk '{print $5}' |cut -d "%" -f 1 )
if [ "$test" -ge "20" ]
then
echo "/ is full ,please deal with it"
fi
[root@aliyun247 test]#
3 双分支if语句
if [ 条件判断式 ]
then
条件成立 执行一个程序
else
条件不成立 执行一个程序
fi
[root@aliyun247 test]# cat if3.sh
#!/bin/bash
read -t 30 -p "please input a dir " dir
if [ -d "$dir" ]
then
echo "it is a dir"
else
echo "it not is a dir"
fi
判断apache 服务是否启动?
ps aux 命令 查看系统中所有的进程
[root@aliyun247 ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 19232 1512 ? Ss 2015 8:15 /sbin/init
root 2 0.0 0.0 0 0 ? S 2015 0:01 [kthreadd]
root 3 0.0 0.0 0 0 ? S 2015 0:44 [migration/0]
root 4 0.0 0.0 0 0 ? S 2015 0:31 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S 2015 0:00 [migration/0]
root 6 0.0 0.0 0 0 ? S 2015 0:53 [watchdog/0]
4 多分枝if 条件语句
判断用户输入的是什么文件 ?
-f 普通文件
-d 目录文件
-l 连接文件
.......其他文件
5 多分支 case
[root@aliyun247 home]# vi cash.sh
#!/bin/bash
echo "fengjie: qing shuru 1"
echo "furong:qing shuru 2"
echo "cangls:qing shuru 3"
read -t 30 -p "qing shuru xuanze:" cho
case "$cho" in
"1")
echo "fengjie genni zou"
;;
"2")
echo "furong genni zou"
;;
"3")
echo "cangls genni zou"
;;
*)
echo "qing shuru zhengque xuanze"
;;
esac
6 for循环
语法一:
for 变量 in 值1 值2 值3
do
程序
done
批量添加指定数量的用户?
语法二:
for ((初始值; 循环控制条件;变量变化 ))
do
程序
done
[root@aliyun247 home]# vim for_sum.sh
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
done
s=0
for(( i=1;i<=100;i=i+1 ))
do
s=$(( $s+$i ))
done
echo $s
cat /etc/passwd |grep /bin/bash |grep -v root | cut -d ":" -f1
7 while 循环和until 循环
while [ 条件判断式 ]
do
程序
done
until [ 条件判断式 ]
do
程序
done
[root@aliyun247 home]# vim while.sh
#!/bin/bash
i=1
s=0
while [ $i -le 100 ]
do
i=$(( $i+1 ))
s=$(( $s+$i ))
done
echo "this sum is: $s "
j=1
m=0
until [ $j -gt 100 ]
do
j=$(( $j+1 ))
m=$(( $m+$j ))
done
echo "this sum m=$m "
8 课程总结