Logical judgment File directory attribute in shell script determine if special usage case judgment

Source: Internet
Author: User
Tags readable


I. Logical judgments in shell scripts

In the shell script, many will logically judge, judge a certain value, judge a file, or a directory, we do some action against the result of the judgment, if there is no judgment, there is no way to do some operations
Format 1:if condition; then statement; fi
Example:
[[Email protected] ~]# if [$a-ge 3]//branch Write it like this




Then
Echo OK
Fi
Ok
[Email protected] ~]# if [$a-ge 3]; then echo OK; FI//This is a one-line write format
Explanation:-gt means greater than the meaning, the format is special: square brackets must have a space on both sides of the-GT must have a space on both sides, conditional statements are everywhere space
We can write the above command as a script:
[Email protected] ~]# CD shell/
[Email protected] shell]# VI if1.sh
#!/bin/bash
A=5
If [$a-GT 3]
Then
Echo OK
Fi
[[Email protected] shell]# sh if1.sh//Execute script to view script execution results
Ok
Most scripts use this format, using logic to determine if, then


Format 2:if condition; then statement; Else statement; fi
Example:
[email protected] shell]# CP if1.sh if2.sh
[Email protected] shell]# VI if2.sh
#!/bin/bash
A=1
If [$a-GT 3]
Then
Echo OK
Else
Echo Nook
Fi
[[Email protected] shell]# sh if2.sh//execution added the judgment of the Else statement, output Nook
Nook
[[email protected] shell]# sh-x if2.sh//-x can see the detailed execution of the script


    • A=1
    • ' [' 1-gt 3 '] '//1 and 3 comparison is not greater than 3, the judgment is else
    • Echo Nook
      Nook


Format 3:if ... ; ; elif. ; ; else ... ; Fi
Example:
[[[email protected] shell]# cp if2.sh if3.sh
[[email protected] shell]# vi if3.sh
#!/bin /bash
a=5
If [$a-gt 1]//If A is greater than 1
then
echo ">1"//Output >1
Elif [$a-lt 6]//Less than 6,elif on the basis of greater than 1 this condition can be judged Write multiple
Then
echo "<6 && >1"//Output <6 && >1
Else//else to indicate conditions other than the above two conditions
Echo Nook
fi
[[email protected] shell]# sh-x if3.sh//Use the-X option to view the script verbose execution process


    • A=5
    • ' [' 5-GT 1 '] '
    • Echo ' >1 '

      1
      Above this if3.sh the logic of this script may be problematic and needs to be improved
      Logical judgment Expression:
      If [$a-gt $b] indicates if a>b;
      If [$a-lt 5] indicates if a<5;
      If [$b-eq 10] indicates if b=10;
      If [$b-ne 10] Indicates if B is not = 10;
      -GT (>)
      -lt (<)
      -ge (>=)
      -le (<=)
      -eq (= =)
      -ne (! =)
      If you want to use the greater than sign directly, you can write in a different format
      [Email protected] shell]# a=3
      [[Email protected] shell]# if (($a >1)); then echo OK; FI//using two brackets, but cumbersome to use
      Ok
      can use && | | Combine multiple conditions,&& representation and, | | Indicate or
      If [$a-gt 5] && [$a-lt 10]; then//If A is greater than 5 and less than 10,then
      If [$b-gt 5] | | [$b-lt 3]; then//if B is greater than 5 or less than 3,then


Second, the file directory attribute judgment

1, [-F file] to determine whether the files are ordinary files, and there is
Example: Creating a file1.sh Script
[Email protected] shell]# VI file1.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-F $f]
Then
echo $f exist
Else
Touch $f
Fi
[[email protected] shell]# sh-x file1.sh//view script execution process


    • F=/tmp/aminglinux//View file path
    • ' ['-f/tmp/aminglinux '] '//To compare files that do not exist
    • Touch/tmp/aminglinux//Does not exist to create
      [[email protected] shell]# sh-x file1.sh//re-execute script hint file already exists
    • F=/tmp/aminglinux
    • ' ['-f/tmp/aminglinux '] '
    • Echo/tmp/aminglinux exist
      /tmp/aminglinux exist


2, [-D file] to determine whether the document is a directory, and there is
Instance:
[[email protected] shell]# CP file1.sh file2.sh//Copy under File1,sh script to modify
[Email protected] shell]# VI file2.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-D $f]//change here to-D, judging is not
Then
echo $f exist
Else
Touch $f
Fi
[Email protected] shell]# sh-x file2.sh


    • F=/tmp/aminglinux
    • ' ['-d/tmp/aminglinux '] '//comparison is not a directory
    • Touch/tmp/aminglinux//Create a directory


3. [-e file] Determines whether a file or directory exists
Instance
[Email protected] shell]# VI file2.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-e $f]//-E determine whether it is a file or directory
Then
echo $f exist
Else
Touch $f
Fi
[Email protected] shell]# sh-x file2.sh


    • F=/tmp/aminglinux
    • ' ['-e/tmp/aminglinux '] '
    • Echo/tmp/aminglinux exist
      /tmp/aminglinux exist
      directories and files can be touch, if the touch when the file or directory is present, then you can change the file of three time, respectively, Atime,ctime,mtime


4, [-R File] to determine whether the document is readable
Instance:
[Email protected] shell]# VI file2.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-R $f]
Then
echo $f Readable
Fi
[[Email protected] shell]# sh file2.sh//execute script, prompt readable
/tmp/aminglinux readable



5, [-W file] to determine whether the file is writable
Instance:
[Email protected] shell]# VI file2.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [w $f]
Then
Echo $f writeable
Fi
[[Email protected] shell]# sh file2.sh////Execute script, prompt to write
/tmp/aminglinux writeable
That it determines that readable writable is actually the current user who is judging the execution of this shell script, we are executing the script as root



6, [-X file] to determine whether the file is executable
Instance:
[Email protected] shell]# VI file2.sh
#!/bin/bash
f= "/tmp/aminglinux"
If [-X $f]
Then
Echo $f exeable
Fi
[[Email protected] shell]# sh file2.sh//Because there is no execute permission, our script does not define else, so there is no output information



This script is modified below
The most used in the work is: usually first determine that the file exists, if it exists, it will delete the file,&& means that the previous command after the successful execution of the subsequent command
So [-F $f] && rm-f $f This line of command is equivalent to the following four lines of command
If [-F $f]
Then
Rm-f $f
Fi
[-F $f] | | Touch $f indicates that the following touch $f command is performed if the $f file is not executed successfully
[-F $f] | | Touch $f is equivalent to the following four lines
if [!-F $f]
Then
Touch $f
Fi



Third, if special usage

1. If [-Z "$a"] This indicates what happens when the value of variable A is empty
Example:
[Email protected] shell]# VI if4.sh
#!/bin/bash
n=wc -l /tmp/lalal
If [-Z "$n"]
Then
echo Error
Exit
elif [$n-GT 100]
Then
Echo abcdef
Fi
If [-Z ' $n '] means when the value of the variable n is empty
[[email protected] shell]# sh-x if4.sh//Execute script to view the execution process
+ + wc-l/tmp/lalal
WC:/tmp/lalal:no such file or directory


    • n=
    • ' ['-Z '] '
    • echo Error
      Error
    • Exit


Complete the if4.sh script again:
[Email protected] shell]# VI if4.sh
#!/bin/bash
if [!-f/tmp/lalal]
Then
echo "/tmp/lalal not exist."
Exit
Fi
n=wc -l /tmp/lalal
If [-Z "$n"]
Then
echo Error
Exit
elif [$n-GT 100]
Then
Echo abcdef
Fi
[[Email protected] shell]# sh if4.sh//Execute script, if the/tmp/lalal file does not exist, exit the script directly
/tmp/lalal not exist.



2, if [-N "$a"] means that the value of variable A is not empty
Instance:
[[Email protected] shell]# if [-N 01.sh]; then echo OK; FI//If 01.sh is not empty, output OK
Ok
[Email protected] shell]# if [-N "$b"]; then echo $b; else echo "B is null"; Fi
B is null
If the variable b value is not empty, the output is $b, otherwise the output "B is null"
Note that if [-N 01.sh] is a file, you do not need to use quotation marks, if it is a variable in [-N "$b"], you need to use quotation marks



3, if Grep-q ' 123 ' 1.txt; Then what if the 1.txt contains a ' 123 ' row?
Example:
Requirements: In the script, you can use the results of a command as a condition to determine whether a file contains a certain string, such as the user in the system to determine whether there is user1 this user, we can use the following command to determine
[[email protected] shell]# grep-w ' user1 '/etc/passwd//-w option to match more precisely
User1:x:1000:1000::/home/user1:/bin/bash
With such a way of thinking, you can judge
[Email protected] shell]# if Grep-wq ' user1 '/etc/passwd; Then echo "User1 exist"; Fi//-q option to not display filtered content
User1 exist



4, if [!-e file]; Then what happens when the file doesn't exist?
5, if (($a <1)); Then ... Equivalent to if [$a-lt 1]; Then ...
6, [] can not use <,>,==,!=,>=,<= such a symbol



Iv. Case Judgment

A double semicolon indicates the end of the first judgment and enters the next judgment
Shell Script instance:
[Email protected] shell]# VI case.sh
#!/bin/bash
Read-p "Please input a number:" N//Ask the user to enter a digit, n as a captured variable what output value does the user enter?
If [-Z ' $n]//If the value of n is null, enter a number
Then
echo "Please input a number."
Exit 1//Exit to capture the value of variable 1
Fi



n1=echo $n|sed ‘s/[0-9]//g‘//Determine if the number entered contains letters or all letters, use sed to empty all numbers
if [!-Z $n 1]//This line or use if [-N ' $n 1 "] instead, if the judgment is not empty, exit to prompt the user to enter a number
Then
echo "Please input a number."
Exit 1
FI//If empty, the input is valid, continue to execute the following script content



If [$n-lt] && [$n-ge 0]//If it is a number, you need to determine the range of the numbers
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



Logical judgment File directory attribute in shell script determine if special usage case judgment


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.