【1119】shell(上)
5.29 shell脚本中的变量linux
5.30 shell中逻辑判断shell
5.31 if判断文件目录属性vim
5.32 if判断的一些特殊用法bash
5.33 case用法less
5.29 shell脚本中的变量优化
当脚本中使用某个字符串较频繁而且字符串长度很长时就应该使用变量代替。spa
- 使用条件语句时,常使用变量 if [ $a -gt 1 ]; then … ; fi
二、引用某个命令的结果时,用变量替代 n=`wc -l 1.txt`字符串
三、写和用户交互的脚本时,变量也是必不可少的 read -p "Input a number: " n; echo $n 若是没写这个n,能够直接使用 $REPLYinput
四、内置变量 $0, $1, $2… $0表示脚本自己,$1 第一个参数,$2 第二个 … $# 表示参数个数数学
五、数学运算 a=1;b=2; c=$(($a+$b))或者$[$a+$b]
5.30 shell中逻辑判断
格式1:if 条件;then 语句;fi
[root@alexis-01 ~]# vim 01.sh
#!/bin/bash
a=5
if [ $a -gt 3 ] //gt 大于
then
echo ok
fi
格式2:if 条件;then 语句;else 语句;fi
[root@alexis-01 shell]# vim 02.sh
#!/bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
else
echo not ok
fi
[root@alexis-01 shell]# sh -x 02.sh
+ a=1
+ '[' 1 -gt 3 ']'
+ echo not ok
not ok
格式3:if …;then …;elif …;then …;else …;fi
[root@alexis-01 shell]# vim 03.sh
#!/bin/bash
a=3
if [ $a -gt 4 ] //great than 大于
then
echo ">4"
elif [ $a -lt 6 ] //less than 小于
then
echo "<6 && >1"
else
echo not ok
fi
[root@alexis-01 shell]# sh -x 03.sh
+ a=3
+ '[' 3 -gt 4 ']'
+ '[' 3 -lt 6 ']'
+ echo '<6 && >1'
<6 && >1
[root@alexis-01 shell]# vim 03.sh
#/bin/bash
a=5
if [ $a -lt 4 ]
then
echo "<4"
elif [ $a -gt 6 ]
then
echo ">6"
else
echo not ok
fi
[root@alexis-01 shell]# sh -x 03.sh
+ a=5
+ '[' 5 -lt 4 ']'
+ '[' 5 -gt 6 ']'
+ echo not ok
not ok
逻辑判断表达式:
if [ $a -gt $b ]; -gt (>); 大于
if [ $a -lt 5 ]; -lt(<); 小于
if [ $b -eq 10 ]等 -eq(==); 等于
-le(<=);-ge(>=); -ne(!=) 注意处处都是空格
若是非要用 > < ,能够用(( ))
[root@alexis shell]# if (($a>1));then echo ok;fi
ok
能够使用 && || 结合多个条件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then 而且
if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或者
5.31 if判断文件目录属性
选项 含义
[ -f file ] 判断是不是普通文件,且存在
[ -d file ] 判断是不是目录,且存在
[ -e file ] 判断文件或目录是否存在
[ -r file ] 判断文件是否可读
[ -w file ] 判断文件是否可写
[ -x file ] 判断文件是否可执行
[ -f file ] 判断是不是普通文件,且存在
[root@alexis-01 shell]# vim 04.sh
#/bin/bash
f="/tmp/alexis"
if [ -f $f ]
then
echo $f exist.
else
touch $f
fi
[root@alexis-01 shell]# sh -x 04.sh
+ f=/tmp/alexis
+ '[' -f /tmp/alexis ']'
+ touch /tmp/alexis
[root@alexis-01 shell]# sh -x 04.sh
+ f=/tmp/alexis
+ '[' -f /tmp/alexis ']'
+ echo /tmp/alexis exist.
/tmp/alexis exist.
[ -d file ] 判断是不是目录,且存在
[root@alexis-01 shell]# vim file2.sh
#/bin/bash
f="/tmp/alexis"
if [ -d $f ]
then
echo $f exist.
else
mkdir $f
fi
[root@alexis-01 shell]# sh -x file2.sh
+ f=/tmp/arslinux
+ '[' -d /tmp/alexis ']'
+ mkdir /tmp/alexis
[ -e file ] 判断文件或目录是否存在
[root@arslinux-01 shell]# vim file3.sh
#/bin/bash
f="/tmp/arslinux"
if [ -e $f ]
then
echo $f exist.
else
touch $f
fi
[root@arslinux-01 shell]# sh -x file3.sh
+ f=/tmp/arslinux
+ '[' -e /tmp/alexis ']'
+ echo /tmp/alexis exist.
/tmp/alexis exist.
[ -r file ] 判断文件是否可读
[root@alexis-01 shell]# vim file4.sh
#/bin/bash
f="/tmp/alexis"
if [ -r $f ]
then
echo $f readable.
fi
[root@alexis-01 shell]# sh -x file4.sh
+ f=/tmp/alexis
+ '[' -r /tmp/alexis ']'
+ echo /tmp/alexis readable.
/tmp/alexis readable.
[ -w file ] 判断文件是否可写
[root@alexis-01 shell]# vim file4.sh
#/bin/bash
f="/tmp/alexis"
if [ -w $f ]
then
echo $f writeable.
fi
[root@alexis-01 shell]# sh -x file4.sh
+ f=/tmp/alexis
+ '[' -w /tmp/alexis ']'
+ echo /tmp/alexis writeable.
/tmp/alexis writeable.
[ -x file ] 判断文件是否可执行
[root@alexis-01 shell]# vim file4.sh
#/bin/bash
f="/tmp/alexis"
if [ -x $f ]
then
echo $f exeable.
fi
[root@arslinux-01 shell]# sh -x file4.sh
+ f=/tmp/alexis
+ '[' -x /tmp/alexis']'
简单写法
#!/bin/bash
f="/tmp/alex"
if [ -f $f ]
then
rm -f $f
fi
能够不用 if 判断写成
#!/bin/bash
f="/tmp/alex"
[ -f $f ] && rm -f $f
#!/bin/bash
f="/tmp/alex"
if [ -f $f ]
then
rm -f $f
else
touch $f
fi
能够反写为
#!/bin/bash
f="/tmp/alex"
if [ ! -f $f ]
then
touch $f
fi
能够不用 if 判断,写成
#!/bin/bash
f="/tmp/alex"
[ -f $f ] || touch $f
5.32 if判断的一些特殊用法
if [ -z “$a” ] 这个表示当变量 a 的值为空时会怎么样
#!/bin/bash
n=`wc -l /tmp/bababa`
if [ -z "$n" ]
then
echo error
else
if [ $n -gt 100]
then
echo ok
fi
fi
能够优化为:
#!/bin/bash
n=`wc -l /tmp/bababa`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100]
then
echo ok
fi
能够再优化:
#!/bin/bash
if [ ! -f /tmp/babala ]
then
echo "/tmp/babala isn't exist."
exit
fi
n=`wc -l /tmp/bababa`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100]
then
echo ok
fi
if [ -n “$a” ] 表示当变量 a 的值不为空
[root@alexis-01 shell]# if [ -n 01.sh ];then echo ok;fi
ok
[root@alexis-01 shell]# if [ -n "$b" ];then echo "$b";else echo "b is null";fi
b is null
命令能够做为判断结果
if grep -q ‘123’ 1.txt; then 表示若是 1.txt 中含有’ 123’ 的行时会怎么样
[root@alexis-01 shell]# if grep -w 'user1' /etc/passwd;then echo "user1 exist";fi
user1:x:1001:1001::/home/user1:/bin/bash
user1 exist
[root@alexis-01 shell]# if grep -wq 'user1' /etc/passwd;then echo "user1 exist";fi
user1 exist //grep -q 静默输出
if [ ! -e file ]; then 表示文件不存在时会怎么样
if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=这样的符号
5.33 case用法
格式:case 变量名 in
value1)
command
;;
value2)
command
;;
*)
commond
;;
esac
;; 表示一个判断结束,进入下一个判断
在 case 程序中,能够在条件中使用 |,表示或的意思, 好比
2|3)
command
;;
*)除以上全部以外的
shell 脚本案例
#!/bin/bash
read -p "Please input a number: " n //从标准输入读取输入并赋值给变量 n
if [ -z "$n" ]
then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed ‘s/[0-9]//g’` //从变量n中将数字替换为空,若是n1不为空,则不是纯数字,那么现实输入一个数字并退出
if [ -n "$n1" ]
then
echo "Please input a number."
exit 1
fi
if [ $n -lt 60 ] && [ $n -ge 0 ] //纯数字往下接着判断
then
tag=1 //标记tag
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 "great"
;;
4)
echo "brilliant"
;;
*)
echo "The number range is 0-100."
;;
esac
执行脚本看结果:
输入符合条件的纯数字
[root@alexis-01 shell]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: 77
+ '[' -z 77 ']'
++ echo 77
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 77 -lt 60 ']'
+ '[' 77 -ge 60 ']'
+ '[' 77 -lt 80 ']'
+ tag=2
+ case $tag in
+ echo ok
ok
输入含字母的数字组合
[root@alexis-01 shell]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: l33l
+ '[' -z l33l ']'
++ echo l33l
++ sed 's/[0-9]//g'
+ n1=ll
+ '[' -n ll ']'
+ echo 'Please input a number.'
Please input a number.
+ exit 1
输入大于 100 的数字
[root@alexis-01 shell]# sh -x case.sh
+ read -p 'Please input a number: ' n
Please input a number: 234
+ '[' -z 234 ']'
++ echo 234
++ sed 's/[0-9]//g'
+ n1=
+ '[' -n '' ']'
+ '[' 234 -lt 60 ']'
+ '[' 234 -ge 60 ']'
+ '[' 234 -lt 80 ']'
+ '[' 234 -ge 80 ']'
+ '[' 234 -lt 90 ']'
+ '[' 234 -ge 90 ']'
+ '[' 234 -le 100 ']'
+ tag=0
+ case $tag in
+ echo 'The number range is 0-100.'
The number range is 0-100.