Shell instance (i)

Source: Internet
Author: User

Shell instance (i)

    • Example one:

equals-eq (equal)

Unequal-ne (not equal)

Greater than-GT (greater than)

Smaller than-lt (less than)

Greater than or equal to-ge (greater than or equal)

Less than or equal to-le (lesser than or equal)

Examples are as follows:

#! /bin/bash

d= ' Date '

Echo $d

A=1

If [$a-lt 3] ==if (($a < 3))

Then

echo "This is OK"

Fi

The results are as follows:

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8F/3F/wKiom1jYp4XRi3ZAAAAUHjVtdT0847.png-wh_500x0-wm_ 3-wmp_4-s_3209411729.png "title=" SS. PNG "alt=" Wkiom1jyp4xri3zaaaauhjvtdt0847.png-wh_50 "/>

    • Example two:

Determine whether the number entered is even or odd

#!/bin/bash

Read-p "Please enter a number:" N

n1=$[$n%2]

If [$n 1-eq 0]

Then

echo "Number entered is even"

Else

echo "Number entered is odd"

Fi

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8F/3D/wKioL1jYrBHihEFlAAAvuHvNoug831.png-wh_500x0-wm_ 3-wmp_4-s_2925740865.png "title=" HH. PNG "alt=" Wkiol1jyrbhiheflaaavuhvnoug831.png-wh_50 "/>

    • example Three: for the above example extension, if the input is not a pure number, what should be done?

#!/bin/bash

Read-p "Please enter a number:" N

N2= ' echo $n |grep-c ' [^0-9] '


If [$n 2-eq 1]

Then

echo "You are not entering a pure number, please re-enter"

Exit 1

Fi

n1=$[$n%2]

If [$n 1-eq 0]

Then

echo "The number you entered is even"

Else

echo "The number you entered is odd"

Fi

Note: When you exit 0, in the call environment echo $? Returns 0, which means that the calling environment thinks your program executes correctly; When you exit 1, it's usually a mistake to define the 1, or it can be another number, many system programs this error number is the meaning of the Convention. but not 0 means that the program is running in error. The calling environment allows you to determine whether the program is running OK based on the return value.

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/8F/3E/wKioL1jYtpOi3FYAAABFSg_lt9c345.png-wh_500x0-wm_ 3-wmp_4-s_796291046.png "title=" EE. PNG "alt=" Wkiol1jytpoi3fyaaabfsg_lt9c345.png-wh_50 "/>

    • Example four:

#if [-E 1.sh];then echo "1.sh exist"; fi = = #[-e 1.sh] && echo "1.sh exist"

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8F/40/wKiom1jYugvzafrwAAANPymR1uk880.png-wh_500x0-wm_ 3-wmp_4-s_2591801816.png "Title=" LL. PNG "alt=" Wkiom1jyugvzafrwaaanpymr1uk880.png-wh_50 "/>

    • Example five:

#!/bin/bash

d= ' Date +%f '

exec >/tmp/$d. log 2> &1 "&1 stands for tmp/$d. Log"

echo "begin at ' Date '"

Ls/tmp/sdade

cd/shdwduw/

echo "End at ' Date '"

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8F/42/wKiom1jY1q2CewgOAAAWh1ZgwSA667.png-wh_500x0-wm_ 3-wmp_4-s_2437407526.png "title=" shell. PNG "alt=" Wkiom1jy1q2cewgoaaawh1zgwsa667.png-wh_50 "/>

This article is from the "Hand of the Paladin Control" blog, please make sure to keep this source http://wutengfei.blog.51cto.com/10942117/1910829

Shell instance (i)

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.