设为首页 收藏本站
查看: 782|回复: 0

How to Use the BASH "for" Loop in Shell Scripts-Arnold

[复制链接]

尚未签到

发表于 2018-8-26 13:51:46 | 显示全部楼层 |阅读模式
  BASH (which stands for Bourne Again Shell) is a scripting language utilized by most Linux and UNIX-based operating systems.
  You can run BASH commands within a terminal window one after the other or you can add the commands to a text file to produce a shell script.
  The great thing about writing shell scripts is that you can run them again and again. For example imagine you need to add a user to a system, set their permissions and manage their starting environment.
  You can either write down the commands on a piece of paper and run them as you add new users or you can write a single script and just pass parameters into that script.
  Scripting languages such as BASH have similar programming constructs as other languages. For instance, you can use import parameters to get input from the keyboard and store them as variables. You can then get the script to perform a certain action based on the value of the input parameters.
  A key part of any programming and scripting language is the ability to run the same piece of code again and again.
  There are a number of ways to repeat code (also known as loops). In this guide, you will be shown how to write a "for" loop.
  A for loop repeats a certain section of the code over and over. They're useful so that a series of commands can keep running until a particular condition is met, after which they'll stop.
  In this guide, you will be shown five ways to use the for loop within a BASH script.
Before Getting Started
  Before you get started with the for loop examples, you need to open a terminalwindow and follow these steps:

  •   Enter mkdir scripts (learn more about mkdir here)
  •   Enter cd scripts (this changes the directory to scripts)
  •   Enter nano examplen.sh (where n is the example you're working on)

  •   Enter the script
  •   PressCTRL+O to save and CTRL+X to exit
  •   Run bash examplen.sh (again, with n being the example you're working with)
How to Loop Through a List
  #!/bin/bash
  for number in 1 2 3 4 5
  do
  echo $number
  done
  exit 0
  The BASH way of using "for" loops is somewhat different to the way most other programming and scripting languages handle "for" loops. Let's break the script down...
  In a BASH "for" loop all, the statements between do and done are performed once for every item in the list.
  In the above example, the list is everything that comes after the word in (i.e. 1 2 3 4 5).
  Each time the loop iterates, the next value in the list is inserted into the variable specified after the word "for". In the above loop, the variable is called number.
  The echo statement is used to displayed information to the screen.
  Therefore, this example takes the numbers 1 through 5 and outputs them one by one to the screen:

  •   1
  •   2
  •   3
  •   4
  •   5
How to Loop Between a Start and End Point
  The trouble with the above example is that if you want to process a bigger list (say 1 to 500), it would take ages to type all the numbers in the first place.
  This brings us to the second example which shows how to specify a start and end point:
  #!/bin/bash
  for number in {1..10}
  do
  echo "$number "
  done
  exit 0
  The rules are basically the same. The values after the word "in" make up the list to iterate through and each value in the list is placed in the variable (i.e. number), and each time the loop iterates, the statements between do and done are performed.
  The main difference is the way the list is formed. The curly brackets {} basically denotes a range, and the range, in this case, is 1 to 10 (the two dots separate the start and end of a range).
  This example, therefore, runs through each number between 1 and 10 and outputs the number to the screen as follows:

  •   1
  •   2
  •   3
  •   4
  •   5
  •   6
  •   7
  •   8
  •   9
  •   10
  The same loop could have been written like this, with syntax identical to the first example:
  for number in 1 2 3 4 5 6 7 8 9 10
How to Skip Numbers in a Range
  The previous example showed how to loop between a start and end point, so now we'll look at how to skip numbers in the range.
  Imagine you want to loop between 0 and 100 but only show every tenth number. The following script shows how to do just that:
  #!/bin/bash
  for number in {0..100..10}
  do
  echo "$number "
  done
  exit 0
  The rules are basically the same. There is a list, a variable, and a set of statements to be performed between do and done. The list this time looks like this: {0..100..10}.
  The first number is 0 and the end number is 100. The third number (10) is the number of items in the list that it will skip.
  The above example, therefore, displays the following output:

  •   0
  •   10
  •   20
  •   30
  •   40
  •   50
  •   60
  •   70
  •   80
  •   90
  •   100
A More Traditional Looking For Loop
  The BASH way of writing for loops is slightly strange when compared to other programming languages.
  You can, however, write a for loop in a similar style to the C programming language, like this:
  #!/bin/bash
  for ((number=1;number < 100;number++))
  {
  if (( $number % 5 == 0 ))
  then
  echo "$number is divisible by 5 "
  fi
  }
  exit 0
  The loop starts by setting the variable number to 1 (number = 1). The loop will keep iterating whilst the value of a number is less than 100 (number < 100). The value of number changes by adding 1 to it after each iteration (number++).
  Everything between the curly braces is performed through each iteration of the loop.
  The bit between the braces checks the value of a number, divides it by 5, and compares the remainder to 0. If the remainder is 0 then the number is divisible by 5 and is then displayed on the screen.
  For example:

  •   5 is divisible by 5
  •   10 is divisible by 5
  •   15 is divisible by 5
  If you want to change the step>number=number+2, number=number+5, or number=number+10 etc.
  This can be further reduced to number+=2 or number+=5.
A Practical Example
  For loops can do more than iterate lists of numbers. You can actually use the output of other commands as the list.
  The following example shows how to convert audio files from MP3 to WAV:
  #!/bin/bash
  for file in ./*.mp3
  do
  mpg -w ./wavs/"${file}".wav "$file"
  done
  The list in this example is every file with the .MP3 extension in the current folder and the variable is a file.
  The mpg command converts the MP3 file into WAV. However, you probably need to install this using your package manager first.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-556902-1-1.html 上篇帖子: 算法及shell脚本编程基础 下篇帖子: 自动获取文件里IP的shell脚本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表