Shell Script Introduction

Source: Internet
Author: User
Tags echo 7 readable

1. When a string is used more frequently in a script and the string length is long, you should use a variable instead

2. Variables are essential when using conditional statements

3. When referencing the result of a command, replace it with a variable

4. Variables are also necessary when writing scripts for user interaction


Built-in variables, $ ...

Mathematical Operation a=1;b=2; c=$ (($a + $b)) or $[$a + $b]


Test Sample: Write an interactive script:

[Email protected] shell]# vim 2.sh

#!/bin/bash

Read-p "Please input anumber:" Number

Echo $number


[Email protected] shell]# sh 2.sh #输入什么就输出什么

Please input anumber:99999

99999


[Email protected] shell]# vim 2.sh

#!/bin/bash

Read-t 3-p "Please input anumber:" Number # "-T 3" join wait 3 seconds

Echo $number


[Email protected] shell]# sh 2.sh #超时会跳出

Please input anumber:

[Email protected] shell]#

---------------------Split Line------------------------------

[Email protected] shell]# vim 3.sh

#!/bin/bash

##

##

Echo $ $

[[Email protected] shell]# sh 3.sh

3.sh



[Email protected] shell]# vim 3.sh

#!/bin/bash

##

##

echo "\$1=$1"

echo "\$2=$2"

echo "\$0=$0"

echo "\$3=$3"


[[Email protected] shell]# sh 3.sh

$1=

$2=

$0=3.sh

$3=


[[Email protected] shell]# SH 3.sh 11 22 33

$1=11

$2=22

$0=3.sh

$3=33


[Email protected] shell]# sh 3.sh #写两个值

$1=11

$2=22

$0=3.sh

$3=



Mathematical operations


[Email protected] shell]# a=1;b=2

[Email protected] shell]# c= $a + $b

[Email protected] shell]# echo $c

1+2

[Email protected] shell]# c=$[$a + $b] #数学运算应该这样写

[Email protected] shell]# echo $c

3

------------------------Split Line------------------------------

If statement


[Email protected] shell]# vim if.sh

#!/bin/bash


A=5

If [$a-gt 3] #-gt greater than < less than-lt = = equals-eq! = is not equal to-ne >= greater than or equal to-ge <= less than or equal to-le

Then

echo "A>3"

Fi


[Email protected] shell]# sh if.sh

A>3



[[email protected] shell]# sh-x if.sh #-X View detailed procedure

+ a=5

+ ' [' 5-gt 3 '] '

+ Echo ' a>3 '

A>3


[Email protected] shell]# vim if.sh

#!/bin/bash


A=5

If [$a-gt] #如果

Then

echo "A>10"

else #否则

echo "a<=10"

Fi

~

[Email protected] shell]# sh if.sh

a<=10


[Email protected] shell]# vim if.sh

#!/bin/bash


A=5

If [$a-gt 10]

Then

echo "A>10"

elif [$a-lt 4]

Then

echo "A<4"

Else

echo "4<a<10"

Fi


[Email protected] shell]# sh-x if.sh

+ a=5

+ ' [' 5-gt 10 '] '

+ ' [' 5-lt 4 '] '

+ Echo ' 4<a<10 '

4<a<10

-------------------------Split Line-----------------------

Several uses of if:

[-F file] Determines if it is a normal file, and there is

[-D file] Determines if it is a directory and exists

[-E file] to determine whether files or directories exist

[-R File] to determine if the document is readable

[-W file] Determines whether the file is writable

[-X file] Determines whether the file is executable


[[Email protected] shell]# if [-f 1.txt]; then echo OK; Fi

[[email protected] shell]# Touch 1.txt

[[Email protected] shell]# if [-f 1.txt]; then echo OK; Fi #判断是否存在1. txt

Ok


[[Email protected] shell]# if [-R 1.txt]; then echo OK; Fi #-R to determine if the file is readable

Ok


[Email protected] shell]# if [-d/tmp/]; then echo OK; Fi #判断是否存在 Directory

Ok


[Email protected] shell]# vim if2.sh

#!/bin/bash

Read-p "Please input a number:" N

M= ' echo $n |sed ' s/[0-9]//g '

If [-N ' $m '] #-n Indicates whether the variable is not empty

Then

echo "The character you input was not a number,please retry."

Else

Echo $n

Fi



[Email protected] shell]# sh if2.sh

Please input a NUMBER:IIIIIIIIII

The character you input is a number,please retry.

[Email protected] shell]# sh if2.sh

Please input a number:90909090

90909090



[Email protected] shell]# sh-x if2.sh #实例分解

+ read-p ' Please input a number: ' N

Please input a number:oiu00909

+ + echo oiu00909

+ + sed ' s/[0-9]//g '

+ M=oiu

+ ' ['-N Oiu '] '

+ Echo ' The character you input is not a number,please retry. '

The character you input is a number,please retry.


[Email protected] shell]# sh-x if2.sh #实例分解

+ read-p ' Please input a number: ' N

Please input a number:9909090909

+ + sed ' s/[0-9]//g '

+ + echo 9909090909

+ m=

+ ' ['-N '] '

+ Echo 9909090909

9909090909



[Email protected] shell]# vim if2.sh

#!/bin/bash

Read-p "Please input a number:" N

M= ' echo $n |sed ' s/[0-9]//g '

If [-Z ' $m '] #- Z Indicates whether the variable is empty and is the opposite of-N.

Then

Echo $n


Else

echo "The character you input was not a number,please retry."



Fi



[Email protected] shell]# sh-x if2.sh

+ read-p ' Please input a number: ' N

Please input a number:90909090

+ + sed ' s/[0-9]//g '

+ + echo 90909090

+ m=

+ ' ['-Z '] '

+ Echo 90909090

90909090

[Email protected] shell]# sh-x if2.sh

+ read-p ' Please input a number: ' N

Please input a number:jki0000

+ + sed ' s/[0-9]//g '

+ + echo jki0000

+ M=jki

+ ' ['-Z jki '] '

+ Echo ' The character you input is not a number,please retry. '

The character you input is a number,please retry.


---------------------Split Line-------------------------------

Case usage: not too common. More in startup scripts.




---------------------Split Line-------------------------------

For loop:


[Email protected] shell]# vim for.sh


#!/bin/bash

For i in ' SEQ 1 10 '

Do

Echo $i

Done


[Email protected] shell]# sh for.sh

1

2

3

4

5

6

7

8

9

10



[Email protected] shell]# vim for.sh

#!/bin/bash

Sum=0

For i in ' SEQ 1 10 '

Do

sum=$[$sum + $i]

Done

Echo $sum

~

[Email protected] shell]# sh-x for.sh

+ sum=0

+ + SEQ 1 10

+ for I in ' SEQ 1 10 '

+ Sum=1

+ for I in ' SEQ 1 10 '

+ sum=3

+ for I in ' SEQ 1 10 '

+ sum=6

+ for I in ' SEQ 1 10 '

+ sum=10

+ for I in ' SEQ 1 10 '

+ sum=15

+ for I in ' SEQ 1 10 '

+ sum=21

+ for I in ' SEQ 1 10 '

+ sum=28

+ for I in ' SEQ 1 10 '

+ sum=36

+ for I in ' SEQ 1 10 '

+ sum=45

+ for I in ' SEQ 1 10 '

+ sum=55

+ Echo 55

55

---------------------Split Line-------------------------------


While loop


[Email protected] ~]# vim while.sh

#!/bin/bash

N=0

While [$n-le 10]

Do

Echo $n

n=$[$n +1]

Done

~

[Email protected] ~]# sh-x while.sh #测试结果

+ n=0

+ ' [' 0-le 10 '] '

+ Echo 0

0

+ n=1

+ ' [' 1-le 10 '] '

+ Echo 1

1

+ n=2

+ ' [' 2-le 10 '] '

+ Echo 2

2

+ n=3

+ ' [' 3-le 10 '] '

+ Echo 3

3

+ n=4

+ ' [' 4-le 10 '] '

+ echo 4

4

+ n=5

+ ' [' 5-le 10 '] '

+ echo 5

5

+ n=6

+ ' [' 6-le 10 '] '

+ echo 6

6

+ n=7

+ ' [' 7-le 10 '] '

+ echo 7

7

+ n=8

+ ' [' 8-le 10 '] '

+ echo 8

8

+ n=9

+ ' [' 9-le 10 '] '

+ Echo 9

9

+ n=10

+ ' [' 10-le 10 '] '

+ Echo 10

10

+ n=11

+ ' [' 11-le 10 '] '

---------------------Split Line-------------------------------

Shell interrupt continues to exit:


[Email protected] shell]# vim for2.sh


#!/bin/bash

For i in ' SEQ 1 10 '

Do

Echo $i

If [$i-eq 4]

Then

Break

Fi

Echo $i

Done

~

[Email protected] shell]# sh-x for2.sh

+ + SEQ 1 10

+ for I in ' SEQ 1 10 '

+ Echo 1

1

+ ' [' 1-eq 4 '] '

+ Echo 1

1

+ for I in ' SEQ 1 10 '

+ Echo 2

2

+ ' [' 2-eq 4 '] '

+ Echo 2

2

+ for I in ' SEQ 1 10 '

+ Echo 3

3

+ ' [' 3-eq 4 '] '

+ Echo 3

3

+ for I in ' SEQ 1 10 '

+ echo 4

4

+ ' [' 4-eq 4 '] '

+ Break #退出the entire loop



[Email protected] shell]# vim for2.sh

#!/bin/bash

For i in ' SEQ 1 10 '

Do

Echo $i

If [$i-eq 4]

Then

continue #结束this cycle

Fi

Echo $i

Done

echo "for-Done"

~


[Email protected] shell]# sh for2.sh

1

1

2

2

3

3

4

5

5

6

6

7

7

8

8

9

9

10

10

For-Done



[Email protected] shell]# vim for2.sh

#!/bin/bash

For i in ' SEQ 1 10 '

Do

Echo $i

If [$i-eq 4]

Then

Exit

Fi

Echo $i

Done

echo "for-Done"


[Email protected] shell]# sh for2.sh

1

1

2

2

3

3

4

#此处无for done, exit the shell directly



This article is from the "Cbo#boy_linux Road" blog, make sure to keep this source http://20151213start.blog.51cto.com/9472657/1890665

Shell Script Introduction

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.