Shell scripts, if judgments, case judgments

Source: Internet
Author: User
Tags ffi

20.5 logical judgments in shell scripts
    • If judgment
  #if If the 1:if condition of the Chinese language; fi[[email protected] ~]# a=5;if [$a-gt 3];then echo OK; fiok; The reference text format is as follows: # !/bin/basha=5if [$a-gt 3] #-gt means >= then echo OKFI; format 2:if condition; Then statement; else statement; Fi[[email protected] shell]# a=5;if [$a-gt 6];then echo ok;else echo no ok;fino OK; The reference text format is as follows: #!/bin/basha=5if [$a -GT 6]then Echo okelse echo no OKFI; format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi[[email protected] shell]# a=5;if [$a-gt];then echo 0k;elif [$a-gt 6];then echo $a \>6;else echo $a \< The 6;fi5<6; reference text format is as follows: [[email protected] shell]# sh-x if3.sh+ a=5+ ' [' 5-gt '] ' + ' [' 5-gt 6 '] ' + echo ' 5<6 ' 5&L    T;6[[email protected] shell]# cat if3.sh #!/bin/basha=5if [$a-gt]then echo ">10" elif [$a-gt 6]then echo $a ">6" else echo $a "<6" fi  

Logical Judgment Bar expression:
If [$a-gt $b];
If [$a-lt 5];
If [$b-eq 10] etc.
-GT (>); -lt (<); -ge (>=);
-le (<=); -eq (= =); -ne (! =)
Note that there are spaces everywhere.

#另一种写法:[[email protected] shell]# a=5;if (($a>10));then echo OK;else echo NO;fiNO[[email protected] shell]# a=5;if (($a>=5));then echo OK;else echo NO;fiOK[[email protected] shell]# a=5;if (($a>10));then echo OK;elif (($a>6));then echo $a">6";else echo $a"<6";fi5<6
    • can use && | | Combine multiple conditions

If [$a-gt 5] && [$a-lt 10]; Then
If [$b-gt 5] | | [[$b-lt 3]; then

20.6 File Directory property judgment
    • [-F file] Determines if it is a normal file, and there is
[[email protected] shell]# cat file.sh #!/bin/bashf="/tmp/taoyuan"if [ -f $f ]then    echo $f existelse    touch $ffi;没有文件,创建文件[[email protected] shell]# sh -x file.sh + f=/tmp/taoyuan+ ‘[‘ -f /tmp/taoyuan ‘]‘+ touch /tmp/taoyuan;文件已经存在[[email protected] shell]# sh -x file.sh + f=/tmp/taoyuan+ ‘[‘ -f /tmp/taoyuan ‘]‘+ echo /tmp/taoyuan exist/tmp/taoyuan exist
    • [-D file] Determines if it is a directory and exists
[[email protected] shell]# cat file1.sh #!/bin/bashf="/tmp/taoyuan"if [ -d $f ]then    echo $f existelse    mkdir $ffi[[email protected] shell]# sh -x file1.sh + f=/tmp/taoyuan+ ‘[‘ -d /tmp/taoyuan ‘]‘+ mkdir /tmp/taoyuan
    • [-E file] to determine whether files or directories exist
[[email protected] shell]# cat file2.sh #!/bin/bashf="/tmp/taoyuan"if [ -e $f ]then    echo $f exist        else    mkdir $ffi[[email protected] shell]# sh -x file2.sh+ f=/tmp/taoyuan+ ‘[‘ -e /tmp/taoyuan ‘]‘+ echo /tmp/taoyuan exist/tmp/taoyuan exist
    • [-R File] to determine if the document is readable
[[email protected] shell]# f="/tmp/taoyuan";if [ -r $f ];then echo $f read;else echo file reservation;fi/tmp/taoyuan read
    • [-W file] Determines whether the file is writable
[[email protected] shell]# f="/tmp/taoyuan";if [ -w $f ];then echo $f write;else echo $f File unable to write;fi/tmp/taoyuan write
    • [-X file] Determines whether the file is executable
[[email protected] shell]# f="/tmp/taoyuan";if [ -x $f ];then echo $f x file;else echo $f File no execution;fi/tmp/taoyuan x file
20.7 If special usage
    • What happens if [-Z ' $a '] This indicates that the value of equivalent A is empty?
[[email protected] shell]# cat if.sh #!/bin/bashf="/tmp/lalal"if [ ! -f $f ]then    echo "$f not exist."    exitfin=`wc -l $f`if [ -z "$a" ] #变量需要双引号then    echo error    exitelif [ $n -gt 100 ]then    echo OKfi[[email protected] shell]# sh if.sh /tmp/lalal not exist.
    • If [-n ' $a '] means that the value of variable A is not empty
[[email protected] shell]# if [ -n if.sh ]; then echo ok;fiok#可以判断一个文件是否为空[[email protected] shell]# if [ -n "$b" ]; then echo $b;else echo "b is null";fib is null
    • If Grep-q ' 123 ' 1.txt;then indicates what happens if the 1.txt contains ' 123 ' Rows
[[email protected] shell]# if grep -w ‘user‘ /etc/passwd; then echo "user exist"; fiuser:x:1000:1000:user:/home/user:/bin/bashuser exist[[email protected] shell]# if grep -wq ‘user‘ /etc/passwd; then echo "user exist"; fiuser exist#grep -q 不显示过滤的结果
    • if [!-e file];then indicates that the file does not exist
    • Symbols such as <,>,==,!=,>=,<= cannot be used in []
20.8-20.9 Case Judgment
    • Shellcase Judgment Script case
[[email protected] shell]# cat case.sh #!/bin/bashread -p "Please enter the score:" nif [ -z "$n" ]then    echo "For the input score."    exit 1fin1=`echo $n|sed ‘s/[0-9]//g‘`if [ ! -z "$n1" ]then    echo "Non-numeric input."    exit 1fiif [$n -lt 60 ] && [ $n -ge 0 ]then    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=4elsetag=0ficase $tag in    1)       echo "C"    ;;    2)       echo "B"    ;;    3)       echo "A"    ;;    4)       echo "A+"    ;;    *)      echo "Please enter a scale of 0-100."    ;;esac

Shell scripts, if judgments, case judgments

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.