Basic use of Shell statements

Source: Internet
Author: User
Tags case statement

1 if statement

echo "Please input X"

Read X

echo "Please input Y"

Read Y

If [$x-gt $y]; In the [] condition, there must be a space before the [only space is followed]. Only a semicolon can be available, or there can be no semicolon

Then

echo "$x Dayu $y"

elif [$x-lt $y];

Then

echo "$x Xiayu $y"

Else

echo "$x Dengyu $y"

Fi

2. addition (multiplication)

sum= ' expr $x + $y ' integer calculation, to add expr, and to have spaces on both sides of the +

echo "The result is $sum"

If it's a multiplication. ' Expr $x * $y ' need ' for translation

3. Save the return code of the previous command

["$x" = "$y"]

echo $?? can be used to save the return code of the previous command. Correct return of 0, wrong return 1

4.for statement

Total=0

The For ((i=1;i<=100;i++)) is followed by a double bracket (()). The inside of a general (()) is used for some computational

Todo

Total= ' expr $total + $i '

Done

echo "The result is $total"

5.while statement

1) total=0; Num=0 on the same line, but separated by semicolons

2) Total=0 Num=0 agreed to the line, but no separator these three kinds of effects are the same

3) total=0

Num=0 is written in two lines.

while (NUM<100) a while is also a double bracket (())

Todo

num= ' expr $num + 1 '

Total= ' expr $total + $num '

Done

In 6.until, the condition is True end loop

Until [$num-ge] until is [] to judge, here can not use <, if-gt

Todo

num= ' expr $num + 1 '

Total= ' expr $total + $num '

Done

7. For loop there is a conditional judgment of if

For ((i=1;i<=10;i++)) This, for the inside easy to forget to write do and done

Todo

if ((i%2==1))

Then

Rcho "Jishu is $i"

Else

echo "Oushu is $i"

Fi

Done

8. Case statement

Read X

A= $x

Case $a in

1)

echo "The NUM is 1";; At the end of each sentence there are two semicolons;; this is necessary.

2)

echo "The NUM is 2"; echo "Hehhehe";; Output two lines, and is a newline, between the two sentences with a semicolon link;

3|4)

echo "The NUM is 3 or 4";; A condition has multiple values, this time with a "|" to separate

*)

echo "The NUM is other";; For other possible conditions, use "*" to indicate

Esac

9 Functions and calling

Add () function body

{

sum= ' expr $x + $y '

echo "The sum is $ sum"

}

echo "Please input X"

Read X

echo "Please input Y"

Read Y

Add $x $y function call

10. Call to Script

In script 1, if you call script 2. Add a line "./script2" directly to script 1

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.