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

Source: Internet
Author: User

1. Logical judgment in shell scripts

Format 1:if condition; then statement; Fi
Format 2:if condition; then statement; Else statement; Fi
Format 3:if ...; Elif ...; Then ...; else ...; Fi
Logical judgment expression:if [$a-gt $b], if [$a-lt 5], if [$b-eq 10]-gt (>);-lt (<);-ge (>=);-le (<=);-eq (= =); n E (! =) Note that there are spaces everywhere
can use && | | Combine multiple conditions
If [$a-gt 5] && [$a-lt 10]; Then
If [$b-gt 5] | | [$b-lt 3]; Then

Single-Branch judgment:

Single-Branch judgment 2:

Multiple judgments:

#!/bin/bash
A=$1
If [$a-gt 5];then
echo "More than 5"
elif [$a-eq 5];then
echo "number entered is equal to 5"
Else
echo "$ Five Less"
Fi

2. File directory attribute judgment

[-F file] Determines if it is a normal file, and there is
[-D file] Determines if it is a directory and exists
[-E file] to determine whether files or directories exist
[-R File] to determine if the document is readable
[-W file] Determines whether the file is writable
[-X file] Determines whether the file is executable

3. Special use of If

If [-Z ' $a '] This indicates what happens when the value of variable A is empty
If [-n ' $a '] means that the value of variable A is not empty
If Grep-q ' 123 ' 1.txt; Then what happens if the 1.txt contains a ' 123 ' row
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 []

4. Case Judgment
Usage:

Case $tag in
1)
echo "Not OK"
;;
2)
echo "OK"
;;
3)
echo "Ook"
;;
4)
echo "Oook"
;;
*)
echo "The number range is 0-100."
;;
Esac

Case:

Then use case to judge the script:

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