Shell Script Advanced

Source: Internet
Author: User
Tags case statement

Shell script advanced One, Shell script advanced condition test

When you write a shell script, you often need to judge some conditions, and you can use Test command test to help complete the test process. The test command tests whether a particular expression is true, and when the condition is true, the return value after the command executes is 0, otherwise it is a different value.

The basic format for conditional testing is:

Format 1
Test EXPRESSION
Format 2
[EXPRESSION]
Note: When using square brackets, spaces must be added to both sides of the condition.

Common types of tests are

  • File test
  • Numerical test
  • String test
  • Combination Test
  • File test

File presence and category testing

-a FILE: Same-E
-e File: Test for existence of files, existence is true, otherwise false
-B File: Exists and is a block device file
-C file: exists and is a character device file
-D file: Exists and is a catalog file
-F file: exists and is a normal file
-H file or-L file: Existing and Symbolic link files
-P file: exists and is a named pipe file
-S file: exists and is a socket file

File Permission test

-R FILE: exists and is readable
-W FILE: exists and is writable
-X FILE: exists and is executable
-U FILE: Exists and has suid permissions
-G FILE: Exists and has Sgid permissions
-K FILE: Exists and has sticky permissions

File Properties Test

S FILE: exists and is not empty
-T FD:FD indicates whether the file descriptor is open and related to a terminal
-N File: whether the files have been modified since the last time they were read
-O File: Whether the current active user is a file owner
-G file: whether the current active user is a group of files
File1-ef whether File2:file1 and FILE2 point to the same inode on the same device
File1-nt File2:file1 is new to FILE2 (Mtime)
File1-ot File2:file1 is older than FILE2

Numerical test

-GT is greater than
-ge is greater than or equal to
-eq is equal to
-ne is not equal to
-lt is less than
-le is less than or equal to

String test

= = is equal to
is greater than (ASCII code)
< is less than
! = is not equal to
Whether the =~ can be matched by the pattern on the right (typically used in [[]])
-Z verifies whether the string is empty, empty is true, and non-empty is false
-O and-Z opposite

Combination Test

The first way:
COMMAND1 && COMMAND2 and
COMMAND1 | | COMMAND2 or
! COMMAND Non-
The second way:
Expression1-a EXPRESSION2 and
Expression1-o EXPRESSION2 or
! EXPRESSION Non-

Second, the Shell script advanced condition statement

? ? The shell has its own process Control statement, which includes conditional statements, loop statements, and so on.

Conditional Select if statement

Single branch:

if judgment condition; then
Branch code with true condition
Fi
Example:

#!/bin/bash
Use=df -hT | grep "/boot" | awk ‘{print $6}‘ | cut -d "%" -f1
If [$use-gt];then
echo "warning!! /boot disk is full "
Fi

Dual Branch:

if judgment condition; Then
Branch code with true condition
Else
The condition is a false branch code
Fi

Example:

If ping-c1-w2 station1 &>/dev/null; Then
Echo ' Station1 is up '
Else
Echo ' Station1 is unexpectedly down! '
Exit 1
Fi

Multi-branch:

if judgment condition 1; Then
Branch code with true condition
Elif judgment Condition 2; Then
Branch code with true condition
Elif judgment Condition 3; Then
Branch code with true condition
Else
The above conditions are false branch codes
Fi

Example:

#!/bin/sh
Scores=40
if [[$scores-GT 90]]; Then
echo "Very good!"
elif [[$scores-GT 80]]; Then
echo "good!"
elif [[$scores-GT 60]]; Then
echo "Pass!"
Else
echo "No pass!"
Fi

Conditional Judgment Case Statement

Case variable reference in
PAT1)
Branch 1
;;
PAT2)
Branch 2
;;
...
*)
Default Branch
;;
Esac

Example:

#!/bin/sh
Case $ in
Start | Begin
echo "Start Something"
;;
Stop | End
echo "Stop Something"
;;
*)
echo "Ignorant"
;;
Esac

Today's blog is here, next time to continue to organize the shell script knowledge.

Shell Script Advanced

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.