1 测试
基本语法
1) test
2) [ ]
1.1 数字
[ 3 -eq 3 ] && echo "en" || echo "ene~~"
en
[ 3 -gt 3 ] && echo "en" || echo "ene~~"
ene~~
[ 3 -ge 3 ] && echo "en" || echo "ene~~"
en
[ 3 -lt 3 ] && echo "en" || echo "ene~~"
ene~~
[ 3 -le 3 ] && echo "en" || echo "ene~~"
en
[ 3 -ne 3 ] && echo "en" || echo "ene~~"
ene~~
test 3 -eq 4 && echo "en" || echo "ene~~"
ene~~
1.2 字符串
test str1 == str2 测试字符串是否相等
test str1 != str2 测试字符串是否不相等
test str1 测试字符串是否不为空 !=null 【 str 】 【 $str 】 如果为空返回false (null!=str){}
test -n str1 测试字符串是否不为空,不为空,true,false
test -z str1 测试字符串是否为空
[ $name ] && echo "shi" || echo "buhsi"
shi
[ abc ] && echo "shi" || echo "buhsi"
shi
[ $name ]
[ $name == mayun ] && echo "shi" || echo "buhsi"
shi
[ $name != mayun ] && echo "shi" || echo "buhsi"
buhsi
test -e /a.txt && echo "shi" || echo "buhsi"
buhsi
1.3 文件
[ -e /1.txt ] && echo "shi" || echo "buhsi"
shi
[ -e /1.txt ] && echo "shi" || echo "buhsi"
shi
[ -f /1.txt ] && echo "shi" || echo "bushi"
shi
[ -d /1.txt ] && echo "shi" || echo "bushi"
bushi
[ -e /1.txt ] && echo "shi" || echo "bushi"
shi
[ -r /1.txt ] && echo "shi" || echo "bushi"
shi
[ -w /1.txt ] && echo "shi" || echo "bushi"
shi
[ -x /1.txt ] && echo "shi" || echo "bushi"
bushi
[ -L /1.txt ] && echo "shi" || echo "bushi"
bushi
1.4 多重条件
多重条件
-a and
-o or
[ -r ./first.sh -a -w ./first.sh ] && echo "en" || echo "enen~~~"
en
[ -r ./first.sh -o -x ./first.sh ] && echo "en" || echo "enen~~~"
en
[ -r ./first.sh -a -x ./first.sh ] && echo "en" || echo "enen~~~"
enen~~~
&& 和 ||
&& 逻辑与 条件满足,才执行后面的语句
[ -z “$name” ] && echo invalid || echo ok
|| 逻辑或,条件不满足,才执行后面的语句
test “$name” == ”yangmi” && echo ok || echo invalid
2 流程控制之if
if [ ]
then
else
fi
if [ ]
then ...
elif [ ]
then ...
else
.....
fi
if [ ]
then
if [ ]
then ...
else ...
fi
else ....
fi
#!/bin/bash
# 注释 测试if流程控制
name=zss
if [ $name == lss ]
then echo "你好李四"
else
echo "你好张三"
fi
----------------------------------
#!/bin/bash
# $1 特殊的变量 接收执行脚本的参数的 $1 $2 $3 ... $9 ${10}
name=$1
age=$2
echo $name
echo $age
----------------------------------------
#!/bin/bash
#
name=$1
if [ $name == caocao ]
then echo "weiguo"
elif [ $name == sunquan ]
then echo "wuguo"
else
echo "shuguo"
fi
------------------------------------------
#!/bin/bash
#
name=$1
age=$2
if [ $age -gt 18 ]
then
if [ $name == zss]
then echo "老男人"
elif [ $name == banzhang ]
then echo "不太老的男人"
else echo "老娘们"
fi
else
echo "game over"
fi
3 流程控制之for
#!/bin/bash
for (( i=1 ; i<=10 ;i++ ))
do
# 求i的平方
echo $(($i*$i))
done
-----------------------------
#!/bin/bash
for i in 1 2 3 4 5
do
echo $i
done
---------------------------------
#!/bin/bash
for i in 1 2 3
do
echo linux0$i
done
----------------------------------
#!/bin/bash
for hostname in linux01 linux02 linux03
do
echo $hostname
done
---------------------------------
#!/bin/bash
for name in `cat /doit18/hosts.txt`
do
echo $name
done
------------------------------------写一个脚本 启动集群中所有的tomcat
#!/bin/bash
for hostname in linux01 linux02 linux03
do
ssh $hostname "source /etc/profile ;/opt/apps/apache-tomcat-7.0.47/bin/startup.sh;exit"
done
-------------------------停止集群中所有的tomcat
#!/bin/bash
for hostname in linux01 linux02 linux03
do
ssh $hostname "source /etc/profile ;/opt/apps/apache-tomcat-7.0.47/bin/shutdown.sh;exit"
done
4流程控制之while
5 case语法 特殊的分支语句 (if的简写)
#!/bin/bash
case $1 in
"zss")
echo "zhangsansan"
;;
"lss")
echo "lisisi"
;;
"abc")
echo "ABC"
;;
*)
echo "qita"
esac