Getting Started with Shell programming

Source: Internet
Author: User
Tags function definition

Getting Started with Shell programming

One, variable assignment and arithmetic operation

Demo Sample code:

A=1echo $alet b= $a +1echo $bc =$[a+b]echo $cd =$[c*2]echo $dlet e=d*2echo $ef =$[e%5]echo $f


Note the "=" number must not have spaces on either side
The code snippet above will output:
1
2
3
6
12
2

Two, array

1, assign Value

Demo Sample Code

Declare-a arr1= (1 2 3 4) echo ${arr1[0]}arr2= ("China" "Japan" "Korea") echo ${arr2[0]}arr3[0]=100echo ${arr3[0]}


The code snippet above will output:
1
China
100

2. Iterating through an array

Demo Sample Code

Arr2= ("China", "Japan" "Korea") for V in ${arr2[@]};d o echo ${v}done



The code snippet above will output:
China
Japan
Korea

Note: the @ symbol in ${arr2[@]} represents the list of elements


Three. Conditional inference

1. Integer comparison

-lt, less than
-le, less than or equal to
-eq. Equals
-ge, greater than or equal to
-GT, greater than
-ne. Not equal to

Demo Sample code:

a=1b=2if [$a-lt $b];then echo "A<b is true" else echo "A<b is false" fic=3if [$a-lt $b] && [$c-gt $b] And then echo "A<b && c>b are true" else echo "A<b && C>b is false" fi


The code snippet above will output:
A<b is True
A<b && C>b is True


2. string comparison

S1 = s2
S1! = S2
S1 > S2
S1 < S2
-N S1. S1 NOT null, length greater than 0
-Z s1. S1 is null. Length is zero

Demo Sample code:

s1= "China" s2= "China" if [$s 1= $s 2];then echo ' s1=s2 is true ' else echo ' s1=s2 is False ' fi


The code snippet above will output:
S1=s2 is True


3, File attribute inference

-A file1:file1 exists
-D File1:file1 exists and is a folder
-e file1:file1 exists, same-a
-F File1:file1 exists and is a regular file (not a folder or other special type of file)
-R file1: Read permission
-S file1: file exists and is not empty
-W file1: Write permission
-X file1: Have permissions to run or have search permissions for the folder
-N file1: The file has been modified since the last time it was read
-O file1:own files that belong to
-G file1:group files that belong to
File1-nt File2:file1 is newer than file2 and is subject to last updated time
File1-ot File2:file1 is older than file2, whichever is the last update time

Demo Sample code:

If [-D "/home"];then echo "/home dir exists" else echo "/home dir NOT exists" fi


The code snippet above will output:
S1=s2 is True
/Home dir is exists


Four, function definition

The function parameters indicate:
$ A: Indicates the function name
$: 1th Number of parameters
$: 2nd number of references

Demo Sample code:

A=1function func1 () {local b=2 return $[a+b+$1]}func1 3b=$?

Echo $b;


The code snippet above will output:
6

Attention:
The return value of the Shell language function differs from the traditional language, and the return value in the shell typically refers to the return value after the command is run, Success 0, and failure 1.
The demo sample code above has a return statement. The description function defines the return value itself. So can you use $?

View this return value.
A is a global variable and B is a local variable.

Getting Started with Shell programming

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.