[TOC]
Shell Programming (ii) One, the logical judgment in Shell Script 1.1 judgment Statement if1.1.1 format 1:
if 判断语句;then commandfi
Example 1
# vim if01.sh //判断数值大小第一种方法用[],注意前后空格#!/bin/basha=5if [ $a -gt 3 ]then echo okfi[[email protected] ~]# sh if01.shok
- []-GT: Greater Than,
- []-lt: Less Than,
- []-ge: greater than or equal to,
- []-le: Less than or equal to,
- []-eq: Equals,
- []-ne: Not equal to
1.1.2 Format 2:
格式:if 判断语句;then commandelse commandfi
[[email protected] ~]# vim if02.sh#!/bin/basha=2if [ $a -gt 3 ]then echo okelse echo nookfi
View the execution process
[[email protected] ~]# sh -x if02.sh+ a=2+ ‘[‘ 2 -gt 3 ‘]‘+ echo nooknook
1.1.3 Format 3:
if 判断语句1;then commandelif 判断语句2;then commandelse commandfi
Example:
#!/bin/bashread -p "请输入考试分数:" aif [ $a -lt 60 ]then echo "重考,未通过考试"elif [ $a -gt 60 ] && [ $a -lt 85 ]then echo "通过考试,成绩良好"else echo "通过考试,成绩优秀! "fi
[[email protected] ~]# sh if03.sh请输入考试分数:80通过考试,成绩良好[[email protected] ~]# sh if03.sh请输入考试分数:97通过考试,成绩优秀! [[email protected] ~]# sh if03.sh请输入考试分数:54重考,未通过考试
Second, the file directory attribute judgment
- [] [-F file] to determine whether it is a normal file, and there is
- [] [-D file] Determines if it is a directory, and there is
- [] [-e file] Determines whether a file or directory exists
- [] [-R File] to determine whether the document is readable
- [] [-W file] Determines whether the file is writable
- [] [-X file] Determines whether the file is executable
-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d filename 如果 filename为目录,则为真 [ -d /tmp/mydir ]-f filename 如果 filename为常规文件,则为真 [ -f /usr/bin/grep ]-L filename 如果 filename为符号链接,则为真 [ -L /usr/bin/grep ]-r filename 如果 filename可读,则为真 [ -r /var/log/syslog ]-w filename 如果 filename可写,则为真 [ -w /var/mytmp.txt ]-x filename 如果 filename可执行,则为真 [ -L /usr/bin/grep ]
[[email protected] ~]# sh -x 1.sh+ f=/tmp/xavilinux+ ‘[‘ -f /tmp/xavilinux ‘]‘+ touch /tmp/xavilinux[[email protected] ~]# sh -x 1.sh+ f=/tmp/xavilinux+ ‘[‘ -f /tmp/xavilinux ‘]‘+ echo /tmp/xavilinux exist/tmp/xavilinux exist
#!/bin/bashf="/tmp/xavilinux"if [ -r $f ]then echo $f readablefi
[[email protected] shell]# sh file02.sh/tmp/xavilinux readable
* Common syntax
[[email protected] shell]# vim file.sh#!/bin/bashf="/tmp/xavilinux" [ -f $f ] && rm -f $f等同于以下:if [ -f $f ]then rm -f $ffi
Third, if special usage
If [-Z "$a"]?? This indicates what happens when the value of variable A is empty
Develop good habits, be sure to add "double quotation marks" to the value of the judgment; if "file" can be omitted
If [-n ' $a '] means that the value of variable A is not empty
[[email protected] ~]# if [ -n iftest1.sh ]; then echo ok; fiok[[email protected] ~]# if [ -n "$b" ]; then echo $b; else echo[[email protected] ~]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fib is null
If Grep-q ' 123 ' 1.txt; Then?? What happens if the 1.txt contains a ' 123 ' row
Grep-w W means word, word
GREP-WQ plus Q do not filter users, directly display the results
[[email protected] ~]# grep -w ‘xavi‘ /etc/passwdxavi:x:1000:1000:xavi,xavi‘s office,62580558,62589906:/home/xavi:/bin/bashxavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bash[[email protected] ~]# if grep -w ‘xavi‘ /etc/passwd; then echo "xavi exist"; fixavi:x:1000:1000:xavi,xavi‘s office,62580558,62589906:/home/xavi:/bin/bashxavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bashxavi exist[[email protected] ~]# if grep -wq ‘xavi‘ /etc/passwd; then echo "xavi exist"; fixavi exist
if [!-e file]; Then what happens when the file doesn't exist?
if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ...
Symbols such as <,>,==,!=,>=,<= cannot be used in []
Iv. Case Judgment 4.1 format
case 变量名 in value1) command ;;value2) command ;;*) commond ;;esac
If one of the value in the case is the same, we can write this:
在case程序中,可以在条件中使用 |,表示或的意思, 比如2|3) command ;;
Script case
#!/bin/bashread -p "Please input a number: " n //让用户输入一个数字if [ -z "$n" ] //判断用户有没有输入then echo "Please input a number." exit 1fin1=`echo $n|sed ‘s/[0-9]//g‘` //检查用户输入的是不是全部是数字,不是数字就置空if [ -n "$n1" ]then echo "Please just input a number, without any other words." exit 1fiif [ $n -lt 60 ] && [ $n -ge 0 ] //经过如上的筛选,我们来判断输入数字属于哪个范围,并且把值交给tagthen tag=1elif [ $n -ge 60 ] && [ $n -lt 80 ]then tag=2elif [ $n -ge 80 ] && [ $n -lt 90 ]then tag=3elif [ $n -ge 90 ] && [ $n -le 100 ]then tag=4else tag=0 //大于100的情况ficase $tag in //根据如上得到的值,进行判断 1) echo "you didn‘t pass the exam!" ;; 2) echo "good!" ;; 3) echo "very good!" ;; 4) echo "perfect!!!" ;; *) echo "Pls input a number range 0-100." ;; esac
Shell Programming (ii)-IF judgment and special usage, file directory attribute judgment, case judgment