Variable functionality in the shell

Source: Internet
Author: User

Local Variables :

Set Varname=value: scope for the entire bash process

Local Variables :

Local Varname=value: Scope is the current code snippet

Environment Variables :

Export Varname=value

or Varname=value export VARNAME

Positional variables :

$ ...

Special Variables :

$? : The execution status return value of the previous command

program execution, there may be two types of return values: 1. Program execution results, 2 program status return code (0-255), where 0 is executed correctly, 1-255: Error execution, 1,2,127 system reserved.

Common system Commands:

    1. Undo Variable: Unset VARNAME

    2. View variables in the current shell: Set view all variables (with environment variables and custom variables)

    3. To view the environment variables for the current shell: printenv env Export

Keyboard reads and arrays of variables and declarations:

READ:-p to be followed by a prompt

DECLARE-[AIXR] Variable

A: Define a variable with the following name variable as an array type

I: Define a variable with the following name variable as an integer type

X: Define a variable with the following name variable as an environment variable

R: Set the variable to the readonly variable

#!/bin/bash
###
# #算奇数和偶数的和
var=100
Even=0
Odd=0
Declare-i i=0
While [$I-le $VAR]
Do
If [$ ($I%2))-EQ 0];then
even=$ (($EVEN + $I))
Else
odd=$ (($ODD + $I))
Fi
i=$ (($I + 1))
Done
echo "The odd number is: $ODD"
echo "The even number is: $EVEN"

Because strings are easier to handle in the shell, calculations for numeric values are not very easy to write. Simply Remember

For numeric processing, direct use of var=$ (OP content)

${array variable for the value of the array}

The following is a simple script to write a graphic:

#!/bin/bash
#绘制特殊图形
I=0
J=0
num=11
While [$I-le $NUM];
Do
If [$I-lt $ ((NUM/2))];then
tmp=$ (($NUM/2))
For ((t=0;t<$[$TMP-$I];t=t+1))
Do
Echo-n ""
Done
For ((k=0;k< $I; k=k+1))
Do
Echo-n "*"
Done
Echo
Else
For ((t=0;t<$[$I-$TMP];t=t+1))
Do
Echo-n ""
Done
For ((k=0;k<$[2* $TMP-$I];k=k+1))
Do
Echo-n "*"
Done
Echo
Fi
Let "I+=1"
Done

Loop structure

1.while [Condition]

Do

Done

2.until [Condition]

Do

Done

3.for var in con1 Con2 Con3

Do

Done

4.for (initial value; limit value; execution step))

Do

Done

#!/bin/bash
function Test1 () {
echo "The first way of looping"
Local i=0
Local sum=0
While [$I-le 100];
Do
sum=$[$SUM + $I]
Let "i++"
Done
The value of echo "SUM is:" $SUM
}
function Test2 () {
echo "Second way to Loop"
Local i=0
Local sum=0
Until [$I-le 100];
Do
sum=$ (($SUM + $I))
i=$[$I +1]
Done
The value of echo "SUM is:" $SUM
}
function Test3 () {
echo "The Third Way of looping"
Local i=0
Local sum=0
For I in ' SEQ 1 100 '
Do
sum=$ (($SUM + $I))
Done
echo "SUM value is $sum"

}
function Test4 () {
echo "Fourth cycle mode"
Local i=0
Local sum=0
For ((i=0;i<=100;i++))
Do
sum=$[$SUM + $I]
Done
The value of echo "SUM is:" $SUM
}
Test1
Test2
Test3
Test4


Variable functionality in the shell

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.