[root@hf-01 ~]# for i in `seq 1 5`; do echo $i;done
1
2
3
4
5
[root@hf-01 ~]#
[root@hf-01 ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5
[root@hf-01 ~]#
[root@hf-01 shell]# vim if1.sh
#! /bin/bash
a=5
if [ $a -gt 3 ]
then
echo OK
fi
[root@hf-01 shell]# sh 03.sh
OK
[root@hf-01 shell]#
[root@hf-01 shell]# cp if1.sh if2.sh
[root@hf-01 shell]# vim if2.sh
[root@hf-01 shell]# sh -x if1.sh
+ a=1
+ '[' 1 -gt 3 ']'
+ echo nook
nook
[root@hf-01 shell]# cat if2.sh
#! /bin/bash
a=1
if [ $a -gt 3 ]
then
echo OK
else
echo nook
fi
[root@hf-01 shell]#
[root@hf-01 shell]# vim if3.sh
[root@hf-01 shell]# cat if3.sh
#! /bin/bash
a=6
if [ $a -lt 5 ]
then
echo "<5"
elif [ $a -gt 5 ] && [ $a -lt 9 ]
then
echo "5<a<9"
else
echo ">9"
fi
[root@hf-01 shell]# sh -x if3.sh
+ a=6
+ '[' 6 -lt 5 ']'
+ '[' 6 -gt 5 ']'
+ '[' 6 -lt 9 ']'
+ echo '5<a<9'
5<a<9
[root@hf-01 shell]#
[root@hf-01 shell]# vim file1.sh
[root@hf-01 shell]# cat file1.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
[root@hf-01 shell]# sh -x file1.sh 第一次执行,会创建该文件
+ f=/tmp/hanfeng
+ '[' -f /tmp/hanfeng ']'
+ touch /tmp/hanfeng
[root@hf-01 shell]# sh -x file1.sh 第二次执行,会提示该文件已存在
+ f=/tmp/hanfeng
+ '[' -f /tmp/hanfeng ']'
+ echo /tmp/hanfeng exist
/tmp/hanfeng exist
[root@hf-01 shell]#
[root@hf-01 shell]# vim file2.sh
[root@hf-01 shell]# cat !$
cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -d $f ]
then
echo $f exist
else
mkdir $f
fi
[root@hf-01 shell]# sh -x file2.sh
+ f=/tmp/hanfeng
+ '[' -d /tmp/hanfeng ']'
+ mkdir /tmp/hanfeng
[root@hf-01 shell]#
[root@hf-01 shell]# vim file2.sh
[root@hf-01 shell]# sh -x file2.sh
+ f=/tmp/hanfeng
+ '[' -e /tmp/hanfeng ']'
+ echo /tmp/hanfeng exist
/tmp/hanfeng exist
[root@hf-01 shell]#
[root@hf-01 shell]# cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -r $f ]
then
echo $f readable
fi
[root@hf-01 shell]# sh file2.sh 会看到文件可读的
/tmp/hanfeng readable
[root@hf-01 shell]#
[root@hf-01 shell]# cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -w $f ]
then
echo $f writeable
fi
[root@hf-01 shell]# sh file2.sh
/tmp/hanfeng writeable
[root@hf-01 shell]#
[root@hf-01 shell]# cat file2.sh
#! /bin/bash
f="/tmp/hanfeng"
if [ -x $f ]
then
echo $f exeable
fi
[root@hf-01 shell]# sh file2.sh
/tmp/hanfeng exeable
f="/tmp/aminglinux"
[ -f $f ] && rm -f $f //前一条命令执行成功才会继续执行之后的命令
等同于下面的表达方式
if [ -f $f ]
then
rm -rf $f
fi
f="/tmp/aminglinux"
[ -f $f ] || touch $f //前面命令不成功时,执行后面的命令
if [ ! -f $f ] // “!”表示了如果这条命令不成功,就往下执行
then
touch $f
fi
[root@hf-01 shell]# vim file1.sh
[root@hf-01 shell]# cat !$
cat file1.sh
#! /bin/bash
n=`wc -l /tmp/lala`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo djsjdd
fi
[root@hf-01 shell]# sh -x file1.sh
++ wc -l /tmp/lala
wc: /tmp/lala: 没有那个文件或目录
+ n=
+ '[' -z '' ']'
+ echo error
error
+ exit
[root@hf-01 shell]#
[root@hf-01 shell]# vim file1.sh
[root@hf-01 shell]# cat !$
cat file1.sh
#! /bin/bash
if [ ! -f /tmp/lala ]
then
echo "/tmp/lala not exit."
exit
fi
n=`wc -l /tmp/lala`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo djsjdd
fi
[root@hf-01 shell]# sh file1.sh
/tmp/lala not exit.
[root@hf-01 shell]#
[root@hf-01 shell]# if [ -n 01.sh ]; then echo ok; fi
ok
[root@hf-01 shell]# echo $b
[root@hf-01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null
[root@hf-01 shell]#
[root@hf-01 shell]# grep -w 'zabbix' /etc/passwd
zabbix:x:998:995:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
[root@hf-01 shell]# if grep -wq 'zabbix' /etc/passwd; then echo "zabbix exist"; fi
zabbix exist
[root@hf-01 shell]#
- 一个等于号= 是赋值
变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
2|3)
command
;;
[root@hf-01 shell]# read -p "dfd" z
dfdgb
[root@hf-01 shell]# read -p "dfd: " z
dfd: fgdg
[root@hf-01 shell]#
#!/bin/bash
#判断是否输入有数值,空直接结束整个文本
read -p "Please input a number: " n
#read 让用户输出一些字符串;赋值给最后一个变量;这里的赋值是“n”
if [ -z "$n" ] //变量n 为空
then
echo "Please input a number."
exit 1 // 知识点 1
fi
#n1将输入的数值清空数字,检查变量是否为空,如果不为空,就证明输入有其他的字符,告知用户,请输入一个数字
n1=`echo $n|sed 's/[0-9]//g'` //确定,n变量是否为数字
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
tag=4
else
tag=0
fi
case $tag in
1)
echo "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The input value exceeds the calculation range.The number range is 0-100."
;;
esac