Get the App
SLTechnology News&Howtos  ›  Development  › 

Summary of array operations and bubble sorting implementation script tutorial in shell

Shulou Source: shulou.com Published: 2022-06-03 09:19:10 11月15日 Update

This article mainly explains "the summary of array operation and the method tutorial of bubbling sorting script in shell". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "array operation summary and bubble sorting in shell script method tutorial" it!

Introduction to the shell array:

The code is as follows:

/ / Array variable definition:

$arr= (1 3 6 8 2 5 9 0 7 4)

/ / get the first element by default

$echo $arr

/ / accessed through the subscript, the second element of the array is taken below. The subscript of the shell array starts at 0.

$echo ${arr [1]}

/ / access the entire array

$echo ${arr [@]} or echo ${arr [*]}

/ / get the length of the array (maximum subscript)

$echo ${# arr [@]}

/ / get the length of a single element string

$echo ${# arr [3]}

/ / get part of the array content by slicing

$echo ${arr [@]: 1:2}

/ / start with the second element

$echo ${arr [@]: 2}

/ / to the second element

$echo ${arr [@]:: 2}

Shell implements bubble sorting:

The code is as follows:

#! / bin/sh

# sorting following array

Echo "please input a number list:"

Read-an arr

For ((iTuno; ii; jmurf -))

Do

# echo $j

If [[${arr [j]}-lt ${arr [j-1]}]]

Then

Tasking ${arr [j]}

Arr [j] = ${arr [j-1]}

ARR [j-1] = $t

Fi

Done

Done

Echo "after sorting:"

Echo ${arr [@]}

[tech@ebs sqlee] $. / sorting.sh

Please inout a number list:

0 2 9 6 8 5 7 4 3 1

After sorting:

0 1 2 3 4 5 6 7 8 9

At this point, I believe that you have a deeper understanding of the "array operation summary and bubble sorting script tutorial in shell". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array element method sort summary tutorial script subscript content code length learning practical deeper maximum interest single variable character string Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information Shulou Technology vpn MariaDB