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

Detailed introduction of shell function and array

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains the "detailed introduction of shell function and array". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the detailed introduction of shell function and array".

Shell function

1. Write the sequence of commands together in format

two。 Reusable command sequence

3.Shell function definition (with ending functions that can be displayed by return or exit)

[function] function name () {Command sequence [return x]}

4. The method of calling a function

Function name [parameter 1] [parameter 2]

Application demonstration of Shell function

Summation of two numbers

1. Define a function through sum () {}

two。 Use the read command to interactively enter two numbers and sum them

[root@localhost ~] # vim demo02.ShroupBinxample () {read-p "Please enter the first number:" num1read-p "Please enter the second number:" num2echo "the sum of the two numbers: $num1 and $num2" SUM=$ ((num1+$num2)) echo "is: $SUM"} sum [root@localhost ~] # sh demo02.sh Please enter the first number: 2 Please enter the second number: 6 the sum of 2 and 6 is: 8

Demonstration of Shell Custom function

The system version recognizes and calls the function to start the corresponding service

#! / bin/bash# custom function service_index () {echo "servicectl" return 1} service_version () {grep "CentOS.* 7." / etc/centos-release > / dev/null & & echo "centos7" grep "CentOS.* 6." / etc/centos-release > / dev/null & & echo "centos6" grep "CentOS.* 5." / etc/centos-release > / dev/null & echo "centos5"} # before calling Two functions servicectl () {[[- z $1 | |-z $2]] & & service_index [$(service_version) = "centos7"] & & systemctl $2 ${1} .service | | service $1 $2}

Validate script

Shell array

Application scenarios include

1. Get the length of the array

two。 Get element length

3. Ergodic element

4. Element slicing

5. Element substitution

6. Element deletion

.

Array definition method

Method one

Array name = (value0 value1 value2...)

Method two

Array name = ([0] = value [1] = value [2] = value...)

Method three

List name = "vlaue0 vlaue1 vlaue2.." Array name = ($list name)

Method 4

Array name [0] = "value" array name [1] = "value" array name [2] = "value".

The data types included in the array

1. Numerical type

two。 Character type

Use the "" or''definition

Shell array operation

1. Get the length of the array

$# {Array name [@ / *]} (@ or *)

two。 Read a subscript assignment

${Array name [subscript]}

3. Array traversal

[root@localhost ~] # vim demo01.shrouxinxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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

Servers

Wechat

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

12
Report