Shell scripts from getting started to complex six (conditional judgment and arithmetic operations)

Source: Internet
Author: User
Tags arithmetic logical operators

First, condition judgment

Classification:

Condition test Type

Numerical test

String test

File test


1. Conditional test Expressions

Format:

[Expression]

[[Expression]]

Test expression


2. Numerical test

Parameters Description
-eq Equals is True
-ne Not equal to True
-gt is greater than true
-ge Greater than or equal to true
-lt is less than true
-le is less than or equal to true

Case 1:

Determine if there is a user's default shell for bash on the current system, and if so, how many of these users are displayed; otherwise, no such user is shown

# VI determine1.sh

num = ' awk-f: ' ($7== "/bin/bash" {print $}) '/etc/passwd | Wc-l '

if [[$num-gt 0]];then

echo "System sum $num user Have/bin/bash"

Else

echo "System no user Have/bin/bash"

Fi


Note that there is a space between the contents of the [[]] inside and the brackets


Case 2:

Given a user, determine if the user ID and group ID are the same

# VI determine2.sh

#!/bin/bash

Read-p "Enter Username:" Name

Userid= ' Id-u $name '

Groupid= ' Id-g $name '

If [$userid-eq $groupid];then

echo "Yes,is some"

Else

echo "No,is defferent"

Fi


3. String test

Parameters Description
= Equals is True
!= Not Equal is true
-Z String True if the length of the string is zero
-N String True if the length of the string is nonzero


Case:

Given two strings, determine whether two strings are the same

# determine3.sh

#!/bin/bash

Read-p "Enter The string:" Str1 str2

if [$str 1 = $str 2];then

echo "The string is Same"

Else

echo "The string is Defferent"

Fi


4. File test

Parameters Description
-E File name True if the file exists
-R file name True if the file exists and is readable
-W file name True if the file exists and is writable
-X file name True if the file exists and is executable
-S file name True if the file exists and has at least one character
-D file name True if the file exists and is a directory
-F file name True if the file exists and is a normal file
-C file name True if the file exists and is a character-specific file
-B file Name True if the file exists and is a block special file


Case:

Given a filename, determine if the file exists

# VI determine4.sh

#!/bin/bash

Read-p "Enter filename:" Name

If [-e $name];then

echo "File is exits"

Else

echo "File is not exits"

Fi


5. The relation character

In addition, the shell is provided with (-a), or (-O), non (!) Three logical operators are used to connect test conditions with a priority of: "!" Highest, "-a" followed, "-O" the lowest.

Case:

Determine if the number of parameters is greater than 1 and less than 3

If [$#-gt 1-a $#-le 3]


Whether the number of parameters is greater than 1 or less than 3

If [$#-gt 1] && [$#-le 3]


Second, arithmetic operation

Operator Description Example
+ Addition ' Expr $a + $b ' result is 30
- Subtraction ' Expr $a-$b ' result is 10
* Multiplication ' Expr $a \* $b ' result is 200
/ Division ' Expr $b/$a ' result is 2
% Take surplus ' Expr $b% $a ' result is 0
= Assign value A= $b will assign the value of variable B to a
== Equal. Used to compare two numbers, the same returns true [$a = = $b] returns false
!= Not equal. Used to compare two numbers, not the same returns true [$a! = $b] Returns True


Case summation:


There are several ways to assign values


Num1=5

Num2=6


1) Let-arithmetic expressions


Let sum= $num 1+ $num 2


2) $[arithmetic expression]


sum=$[$num 1+ $num 2]


3) $ (())


sum=$ (($num 1+ $num 2))


4) An arithmetic expression of expr, with spaces between the operands and operators in the expression, and the use of a command reference


sum= ' expr $num 1+ $num 2 '


Shell scripts from getting started to complex six (conditional judgment and arithmetic operations)

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.