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

How to realize Linux Shell dynamic Generation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you how to achieve Linux Shell dynamic generation, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The following is mainly about the dynamic generation of array series. There should be many methods. Here I mainly take the problem of summation calculation as an example for analysis.

Topic: please use linux shell to write a script to achieve the sum of all even numbers from 1. 1000.

Method 1:

Get the desired results through the while loop:

Start=1

Total=0

While [$start-le 1000]; do

[[start%2)) = = 0]] & & total=total+$start))

Start=start+1))

Done

Echo $total

[chengmo@centos5 ~] $start=1;total=0;while [$start-le 1000]; do [[start%2)) = = 0]] & & total=total+ (($start+1)); done;echo $total

250500

The result of the above run is: 249500, in linux shell, ";" as the command line separator. If you do not quite understand the $(()) operation symbol, you can check out: linux shell implements four simple operations (integers and floating points). For the [[]] [] symbol, you can refer to another article, linux shell logical operators and logical expressions.

Method 2:

The result is obtained through the for loop:

Start=0

Total=0

For i in start 2 1000); do

Total=total+$i))

Done

Echo $total

[chengmo@centos5 ~] $start=0;total=0;for i in start 2 1000); * * do** total=total+$i)); done;echo$total

250500

The above statement is obviously better than method one in terms of code, and performs well in terms of performance. The following comparison shows:

Compare performance:

[chengmo@centos5 ~] $time (start=0;total=0;for i in start 2 1000); * * do**total=total+$i)); done;echo $total;) 250500

Real 0m0.016s

User 0m0.012s

Sys 0m0.003s

[chengmo@centos5 ~] $time (start=1;total=0;while [$start-le 1000]; do [[start%2)) = = 0]] & & total=total+ (($start+1)); done;echo $total;)

250500

Real 0m0.073s

User 0m0.069s

Sys 0m0.004s

Method 1 takes 6 times more time than method 2!

Seq uses:

Seq [OPTION]... LAST

Seq [OPTION]... FIRST LAST

Seq [OPTION]... FIRST INCREMENT LAST

[chengmo@centos5 ~] $seq 1000 'starts with a default of 1, and the interval defaults to 1

[chengmo@centos5 ~] $seq 2 1000 'interval defaults to 1

[chengmo@centos5] $seq 1 3 10 'from 1 to 10 with an interval of 3 the result is: 1 4 7 10

Note: the default interval is "space". If you want to change it to something else, you can take the parameter:-s

[chengmo@centos5] $seq-slots'1 3 10

1, 4, 7, 10 *

Application skills:

Generate a series of consecutive arrays:

[chengmo@centos5 ~] (seq 1 310))

[chengmo@centos5 ~] $echo ${a [1]}

four

[chengmo@centos5 ~] $echo ${a [@]}

1 4 7 10

Generate consecutive identical characters:

[chengmo@centos5 ~] $seq-s'#'30 | sed-e's / [0-9] * / / g'

# #

The above example: by adding the interval character'#', replace the number to generate the same consecutive character'#', which is still a lot of help in future writing.

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

On how to achieve dynamic generation of Linux Shell to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report