Home  >  Article  >  System Tutorial  >  Linux Bash Shell-Small loop, big use

Linux Bash Shell-Small loop, big use

王林
王林forward
2024-02-11 13:00:03541browse

This is a deeply pseudo-original version: Why run the same code repeatedly in a shell script when you can use a for loop to execute the same code?

Looping is an innate skill that makes your job easier and helps you automate repetitive tasks with ease.

Imagine if you need to update a series of numbers or text, instead of doing it manually, let the system do it for you. This is the power of circulation and the benefits it brings to you.

Loops as a feature are available in almost all programming languages. Bash for Linux is no exception.

This article is a guide that explains how to use for loops in shell scripts.

for loop structure

Using for loops in shell scripts is fairly simple and you can manipulate the structure to achieve different goals.

The basic structure is as follows:

for item in [LIST]
do
 [COMMANDS]
done

Using loops, you can cycle between numeric and character values ​​as time requires.

This is the structure of the for loop in the shell script:

for VARIABLE in 1 2 3 4 5 .. N
do
 command1
 command2
 commandN
done

You can define the number of iterations on the first line. This way, you will mention the starting value and the ending value.

The number of iterations is determined by the value you specify, and the code after the do statement is the generated loop value.

Creating and running for loops in Linux Bash

Open a Linux terminal and start writing code.

A text editor is used to store a shell script that prints the desired results when executed. For illustration purposes, the commands in this guide are written in the nano text editor.

Enter nano at the terminal command line to open a text editor, and then enter the shell script name.

linuxmi@linuxmi:~/www.linuxmi.com$ nano LinuxMi.com.sh

You can change the name of the shell script to anything you like. The extension is sh because you will be storing a shell script.

Use a for loop to print integers

In this section, the following code will demonstrate how to print integer values ​​in different ways. To print integers using a for loop in a shell script, you can try some of these code examples.

1. Loop code prints a set of numbers

Once the editor opens, it’s time to write code.

#!/usr/bin/bash
for i in 1 2 3 
do
 echo "Current # $i"
done
Linux Bash Shell-小循环大用处

Output:

Linux Bash Shell-小循环大用处

illustrate:

  • i = variable name to store the iteration value
  • 1 2 3 = number of iterations of for loop in shell script
  • do = a command that performs a specific set of actions
  • echo = print the result defined next to it
  • done = end of loop

Press Ctrl X to save the code in the text editor. Save and exit the script.

You must change the permissions of the shell script before executing the code.

Enter chmod x followed by your shell script file name:

linuxmi@linuxmi:~/www.linuxmi.com$ chmod +x LinuxMi.com.sh

After granting permission, run the for loop in the shell script by typing:

linuxmi@linuxmi:~/www.linuxmi.com$ ./LinuxMi.com.sh

The output will be printed in the terminal window.

2. Alternative method of printing a set of numbers

There are other ways to define for loops in shell scripts. You can also use curly braces to specify the start and end values ​​of the loop iteration.

The following is the code structure:

for i in {1..3} # for循环定义了一个变量,以及要通过一个循环进行多少次迭代
do
 echo "当前值 # $i: 示例 2"
done
Linux Bash Shell-小循环大用处

The loop will run 3 times and the value will be printed as follows:

Linux Bash Shell-小循环大用处

3. Loop code using step value

If you want to move discontinuously in the iteration, you can define the step value in the loop. Depending on the specified value, the output will have a fixed gap.

For example:

for i in {1..10..2}
do
 echo "Number = $i"
done

illustrate:

  • i = 存储迭代的变量
  • 1..10 = 运行循环的迭代次数
  • 2 = 阶跃值
  • do = 命令打印输出
  • echo = 打印命令
  • done = 循环的退出命令
Linux Bash Shell-小循环大用处

输出:

Linux Bash Shell-小循环大用处

输出相差 2,这是在 step 语句中指定的。

使用 for 循环打印字符值

shell 脚本中的 for 循环不仅限于整数。在 Bash 中,你可以使用 for 循环来有效地遍历字符和字符串值。

1. 遍历字符串

这是一个基本示例,说明如何遍历一些字符串值(在 for 语句中定义):

for name in LinuxMi linuxmi.com www.linuxmi.com
do
 echo "My name is $name"
done

说明:

  • name = 存储字符串值的变量
  • do = 命令打印输出
  • echo = 打印命令
  • done = 循环的退出命令
Linux Bash Shell-小循环大用处

输出:

Linux Bash Shell-小循环大用处

这个 for 循环将迭代 3 次,因为在for语句中只指定了三个字符串值。

2. 循环遍历有条件的字符串

如果你想传递一些逻辑条件来中途终止循环怎么办?为此,你可以使用逻辑语句,例如IF语句。IF 语句控制循环的工作方式以及结果将打印什么输出。

for distro in LinuxMi Debian CentOS Ubuntu; do

 if [[ "$distro" == 'CentOS' ]]; then

 break

 fi

 echo "distro: $distro"

done

echo '全部完成!'
Linux Bash Shell-小循环大用处

只要 distro 的值等于 CentOS,循环就会终止,并打印输出。循环运行直到不再满足条件。

由于 CentOS 在值列表中排名第三,因此循环将运行两次迭代,然后打印最终输出全部完成!

Linux Bash Shell-小循环大用处

在 Linux Bash 中运行循环

循环是 Linux shell 结构中必不可少的一部分,它可以极大地增强 Linux 脚本的功能。

如果你必须打印重复的输出,没有什么比 Bash 脚本中的循环更好的了。正如我们前面提到的,几乎所有编程语言都可以使用循环,Python 也不例外。减少重复并遵循 DRY(不要重复自己)代码。

The above is the detailed content of Linux Bash Shell-Small loop, big use. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete