Logical judgment in 20.5shell scripts 20.6 file directory attribute Judge 20.7-9if Special usage case judgment

Source: Internet
Author: User
Tags case statement

Logic Judgments in 20.5shell scripts


Format 1:if condition; Then statement; Fi
Most of the time it's used in this format


Format 2:if condition; Then statement; else statement; Fi


Format 2:if condition; Then statement; else statement; Fi

20.6 File Directory property judgment

[-F file] Determines if it is a normal file, and there is

Execution process

The above does not exist already created, and executing again already exists

[-D file] Determines if it is a directory and exists

-D to see if it is a directory, not just create

-E To see if there is

In the shell, touch is able to create directories and files, and if a directory or file already exists, it will change its 3 time

Whether it is readable

is executable

Front Plus! is to take the inverse [-f $f] | | [Touch $f = if [-f $f] then touch $f fi
|| Indicates that the following command is executed when the previous condition does not exist
&& indicates that the following command is executed when the previous condition exists

20.7 If special usage

If [-Z ' $a '] This indicates what happens when the value of variable A is empty

When it has no value, it will get an error.

The-Z representation is empty, and its usage


Exit exits when the previous conditions are met

Use of-N
Output when $b is not empty $b when null output B is null
The file is not enclosed in double quotes, and the variable is enclosed in double quotes.

-Q is not displaying filtered content-W more precise specified single character

20.8/20.9 Case Judgment

Vi/etc/init.d/network, there's a case statement.
#!/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
Case $tag in
1)
echo "Not OK"
;;
2)
echo "OK"
;;
3)
echo "Ook"
;;
4)
echo "Oook"
;;
*)
echo "The number range is 0-100."
;;
Esac


Read-p "Please input a number:" N read-p is the function of the previous assignment, n is how much

Execute the script, enter the number 101, and view the results of the execution

Logical judgment in 20.5shell scripts 20.6 file directory attribute Judge 20.7-9if Special usage case judgment

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.