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

Output:

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

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

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 = 循环的退出命令

输出:

输出相差 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 = 循环的退出命令

输出:

这个 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 '全部完成!'

只要 distro 的值等于 CentOS,循环就会终止,并打印输出。循环运行直到不再满足条件。
由于 CentOS 在值列表中排名第三,因此循环将运行两次迭代,然后打印最终输出全部完成!

在 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!

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

手机远程linux工具有:1、JuiceSSH,是一款功能强大的安卓SSH客户端应用,可直接对linux服务进行管理;2、Termius,可以利用手机来连接Linux服务器;3、Termux,一个强大的远程终端工具;4、向日葵远程控制等等。

linux中,lsb是linux标准基础的意思,是“Linux Standards Base”的缩写,是linux标准化领域中的标准;lsb制定了应用程序与运行环境之间的二进制接口,保证了linux发行版与linux应用程序之间的良好结合。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)