Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell fundamentals-variables, judgments, loops

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

1. The basic format and variables of shell

# add an interpreter to the shell script #! / bin/bash

# print out hello world with script

#! / bin/bash

Echo "hello world"

Exit

Parameter: a parameter is an entity that stores real values, which is generally referenced in the script

# using variables to echo hello world, the following example echoes two hello world.

#! / bin/bash

A = "hello world"

Echo $a

Echo "hello world"

Exit

If something else is added after the parameter, you need to use {}

#! / bin/bash

A = "hello world"

Echo ${a}, i am coming

Exit

Echo result:

# calculate string length, hello world with spaces, a total of 11

2. Script exit

# the return value of a script that executes successfully is 0. If it is not successful, it is not 0.

[root@localhost script] # clear

[root@localhost script] # sh hello.sh

Hello world,i am coming

eleven

[root@localhost script] # echo $?

0

# determine whether there is a file in a directory. If so, echo file exsit. If not, return 1.

[root@localhost script] # cat test.sh

#! / bin/bash

Cd / home

If [- f file]; then

Echo "file exsit"

Else

Exit 2

Fi

[root@localhost script] # sh test.sh

[root@localhost script] # echo $?

two

[root@localhost script] #

3. Test statement, if statement

Whether the test-d xx directory exists

Whether the test-f xx file exists

Whether test-b xx is a block file

Is test-x xx an executable file

A-eq B judges whether they are equal or not.

A-ne B judgment is not equal

A-le B is less than or equal to

A-lt B less than

A-gt B is greater than

# if conditional judgment statement

If []; then

.

Else

.

Fi

# if statement nesting

If []; then

.

Elif []; then

.

Elif []; then

.

Else

.

Fi

# test gives an example to determine whether the test file exists, and if so, back up it. If the message is not echoed

# example: enter a value to determine which interval it is in. Less than 80, greater than 100, between 80 and 100, using if nesting!

[root@localhost script] # cat if.sh

#! / bin/bash

Read-p "plz input a $num:" num

If [$num-le 80]; then

Echo "num is less 80"

Elif [$num-ge 80] & & [$num-le 100]; then

Echo "num between 80 and 100"

Else

Echo "num is greater than 100"

Fi

4. Circular statements for, while

For var in $var1 $var2 $var3 var4... $var

Do

.

.

Done

# example: print 1 to 10 numbers

[root@localhost home] # cat for1.sh

#! / bin/bash

For var in {1..10}

Do

Echo "$var"

Sleep 2

Done

[root@localhost home] # sh for1.sh

one

two

three

four

five

six

seven

eight

nine

ten

[root@localhost home] #

# print box content for nesting

[root@localhost home] # sh for2.sh

*********

*********

*********

*********

*********

*********

*********

*********

*********

[root@localhost home] # cat for2.sh

#! / bin/bash

For ((iTun1)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report