Shell Programming---Character testing and for loops

Source: Internet
Author: User

A. Character test

= =: Test for equality, equality is true, not equal to False

! =: Test Whether it is unequal, not equal to true, etc as false

>

<

-N String: Tests whether the specified string is empty, empty is true, or false

-Z String: Tests whether the specified string is not empty, is not empty, and the null is False

[$username = = $useername 1] There must be spaces at both ends of the equals sign

1.bc

echo "SCALE=2;111/22;" | BC calculates two number of reserved two decimal places

Exercise: Write a script

Pass a parameter (single-character on line) to the script, such as Q, Q, quit, or quit, to exit the script, otherwise, the user's parameters are displayed;

#!/bin/bash

#

if [$ = ' Q '];then

echo "Quiting ..."

Exit 1

elif [$ = ' Q '];then

echo "Quiting ..."

Exit 2

elif [$ = ' Quit '];then

echo "Quiting ..."

Exit 3

elif [$ = ' Quit '];then

echo "Quiting ..."

Exit 4

Else

echo $

Fi

2.for Cycle

For I in {1...10}; Do

Addition operation

Done


Write a script:

Calculates the sum of all odd and all and all even numbers within 100, respectively, showing the

#/bin/bash

#

Declare-i oddsum=0

Declare-i evensum=0

Declare-i s

For I in {1..100};d o

Let s=$[$I%2]

If [$s-eq 0];then

Let oddsum=$[$oddsum + $I]

elif [$s-eq 1];then

Let evensum=$[$evensum + $I]

Fi

Done

Echo ${oddsum}---${evensum}


Combination test conditions

-A: With relation

-O: or relationship

!: Non-relational

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

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








This article is from "Luo Chen's blog" blog, please be sure to keep this source http://luochen2015.blog.51cto.com/9772274/1637233

Shell Programming---Character testing and for loops

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.