Logical judgments in shell scripts, file directory attribute judgments, if special usage, case judgments

Source: Internet
Author: User
Tags readable

Logical judgment in the shell

Format 1:if condition; Then statement; Fi
If it's what it is, then what happens?
Write a simple script that means when a=5, when a is greater than 3 o'clock print OK
#!/bin/bash
A=5
If [$a-GT 3]
Then
Echo OK
Fi
Execute script after saving exit
[Email protected] shell]# sh if1.sh
Ok
One of the GT's meanings in the shell is greater than the meaning. Note that there are spaces in the brackets, both sides and in the middle.

Format 2:if condition; Then statement; else statement; Fi
What it means is how to satisfy if not satisfied.
#!/bin/bash
A=1
If [$a-GT 3]
Then
Echo OK
Else
Echo Nook
Fi
Exit execution script after saving
[Email protected] shell]# sh if2.sh
Nook
This is where the condition is not greater than 3, the condition is not satisfied, so the Nook is printed.

Format 3:if ...; Then ...; Elif ...; Then ...; else ...; Fi
The comparison is made on the basis of satisfying the conditions. Elif was added a logical judgment, and he was followed by done, and this could be written a lot.
#!/bin/bash
A=3
If [$a-GT 4]
Then
echo ">1"
elif [$a-GT 6]
Then
echo "<6 && >1"

Else
Echo Nook
Fi
After saving, exit and execute.
[Email protected] shell]# sh-x if3.sh

    • A=3
    • ' [' 3-GT 4 '] '
    • ' [' 3-gt 6 '] '
    • Echo Nook
      Nook
      As can be seen, he carried out two logical judgments.

Logical judgment expression: if [$a-gt $b]; If [$a-lt 5]; If [$b-eq 10] etc.
-GT (greater than >);
-lt (less than <);
-ge (greater than or equal to >=);
-le (less than or equal to <=);
-eq (equals =);
-ne (Not equal to! =)
Note that there are spaces everywhere.

If you want to use symbols to write scripts, you need to use two (()) to enclose the condition, so that you can use the symbol.
For example:
#!/bin/bash
A=5
if (($a >3))
Then
Echo OK
Fi

can use && | | Combine multiple conditions
&& expressed and
|| Indicate or
If [$a-gt 5] && [$a-lt 10]; Then
If [$b-gt 5] | | [$b-lt 3]; Then

File Directory property judgment

[-F file] Determines whether the file is normal and exists.
#!/bin/bash
f= "/tmp/linletao"
If [-F $F]
Then
echo $f exist
Else
Touch $f
Fi
This script means to determine if the/tmp/linletao exists, if present, to print the path of f, or to create a file if it does not exist.
[Email protected] ~]# sh-x file1.sh

    • F=/tmp/llt
    • ' ['-f/tmp/llt '] '
    • Touch/tmp/llt (no file, then create file)

[Email protected] ~]# sh-x file1.sh

    • F=/tmp/llt
    • ' ['-f/tmp/llt '] '
    • Echo/tmp/llt exist
      /tmp/llt exist (file exists, print file path)

[-D file] Determines if it is a directory and exists
[Email protected] ~]# sh-x file2.sh

    • F=/tmp/llt
    • ' ['-d/tmp/llt '] '
    • Touch/tmp/llt (no file, then create file)
      This script means to determine if the file is a directory and if not, create it.

[-E file] to determine whether files or directories exist
#!/bin/bash
f= "/tmp/llt"
If [-e $f]
Then
Echo OK
Else
Touch $f
Fi
This script means to determine if the/tmp/llt exists, if there is a print OK, and if it does not exist, the file is created.
[Email protected] ~]# sh-x file2.sh

    • F=/tmp/llt
    • ' ['-e/tmp/llt '] '
    • Echo OK
      Ok (file exists)

[-R File] to determine if the document is readable
#!/bin/bash
f= "/tmp/llt"
If [-R $f]
Then
echo $f Readable
Fi
The meaning of this script is to see if the price is readable.
[Email protected] ~]# sh-x file2.sh

    • F=/tmp/llt
    • ' ['-r/tmp/llt '] '
    • Echo/tmp/llt readable (file readable)

[-W file] Determines whether the file is writable
[Email protected] ~]# sh-x file2.sh

    • F=/tmp/llt
    • ' ['-w/tmp/llt '] '
    • Echo/tmp/llt writeable
      /tmp/llt writeable (file writable)

[-X file] Determines whether the file is executable
#!/bin/bash
f= "/tmp/llt"
If [-X $f]
Then
Echo $f exeable
Else
Echo Nook
Fi
This script means whether it can be executed, and if not, print the Nook

If special usage

If [-Z ' $a '] this means that when the value of variable A is empty (the variable needs to be "", the file is not used)
For example:
#!/bin/bash
n= wc -l /tmp/lalala (Note that we're going to have to use an anti-quote here)
If [-Z "$n"]
Then
echo Error
Exit
elif [$n-GT 100]
Then
Echo OK
Fi
This script means that if $n is empty, it will print an error and then exit. If not empty, the cut is greater than 100 lines, then print OK
[Email protected] shell]# sh-x if4.sh
+ + wc-l/tmp/lalala
WC:/tmp/lalala: No file or directory

    • n=
    • ' ['-Z '] '
    • echo Error
      Error
    • Exit
      The run result is $n is empty, so print error, and then exit.

If [-n ' $a '] means that the value of variable A is not empty
[[Email protected] shell]# if [-N 1.sh]; then echo OK; Fi
Ok
It means that when 1.sh is not empty, print OK

In logical judgment, we can also use the result of a command as a criterion, such as determining whether a file contains certain strings.
For example:
If Grep-wq ' user '/etc/passwd; then echo OK; Fi
Ok
This script means no if the friend user in this file is the string, print OK. The result is that this file contains the user string, so print OK
if [!-e file]; Then what happens when the file doesn't exist! To take the opposite meaning.

if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ...
Symbols such as <,>,==,!=,>=,<= cannot be used in []

Case Judgment

Format case variable name in
value1)
Command
;;
value2)
Command
;;
*)
Commond
;;
If the variable is in valux1, he says, execute the following command, execute the following command at Value2, and so on. among them;; Represents the end of a judgment and enters the next judgment.

ESAC in a case program, you can use |, express or mean in a condition,
Like what:
2|3)
Command
;;

We write a script that case.sh
#!/bin/bash
Read-p "Please input a number:" N
If [-Z "$n"]
Then
echo "Please input a number."
Exit 1
Fi
n1= echo $n|sed ‘s/[0-9]//g‘
If [-N "$n 1"]
Then
echo "Please input a number."
Exit 1
Fi

If [$n-lt] && [$n-ge 0]
Then
Tag=1
elif [$n-ge] && [$n-LT 80]
Then
tag=2
elif [$n-ge] && [$n-LT 90]
Then
Tag=3
elif [$n-ge] && [$n-le 100]
Then
Tag=4
Else
Tag=0
Fi

Read-p command, you ask the user what to do, what the user enters, and finally captures the user's input as a condition of judgment.
For example:
[Email protected] ~]# read-p "Please input a number:" N
Please input a number:8
[Email protected] ~]# echo $n
8
where n is a name for your replenishment variable. What the user enters, the variable will eventually be assigned to what.
And then we execute this script

[Email protected] shell]# sh case.sh
Please input a number:30
Not OK

[Email protected] shell]# sh case.sh
Please input a number:60
Ok

[Email protected] shell]# sh case.sh
Please input a number:80
Ook

[Email protected] shell]# sh case.sh
Please input a number:100
Oook

[Email protected] shell]# sh case.sh
Please input a number:101
The number range is 0-100.

All five of these cases have results, indicating that the script was successful.

Implementation process, we take 80 as an example
[Email protected] shell]# sh-x case.sh

    • Read-p ' Please input a number: ' N
      Please input a number:80
    • ' ['-Z 80 '] '
      + + Echo 80
      + + sed ' s/[0-9]//g '
    • n1=
    • ' ['-N '] '
    • ' [' 80-lt 60 '] '
    • ' [' 80-ge 60 '] '
    • ' [' 80-lt 80 '] '
    • ' [' 80-ge 80 '] '
    • ' [' 80-lt 90 '] '
    • Tag=3
    • Case $tag in
    • echo ook
      Ook

Logical judgments in shell scripts, file directory attribute judgments, if special usage, 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.