Shell Script Basics (iii)

Source: Internet
Author: User

One, for loop

The For loop structure is a very frequent loop structure used in daily operation and maintenance work.

1. For loop specific format:
for 变量名 in 循环条件; do       commanddone

The "loop condition" here can be a set of strings with numbers (separated by spaces) or a command's execution result.

2. For loop instance 1: Calculates the sum of 1 to 5
[[email protected] shell]# vim for01.sh#! /bin/bashsum=0for i in `seq 1 5`do    echo $i    sum=$[$sum+$i]doneecho $sum[[email protected] shell]# sh for01.sh 1234515
Example 2: Listing /etc/Include words in the directory yumThe directory.
[[email protected] shell]# vim for2.sh#! /bin/bashcd /etc/for i in `ls /etc/`; do   if [ -d $i ]; then     ls -ld $i | grep -w ‘yum‘   fidone[[email protected] shell]# sh for2.sh drwxr-xr-x. 6 root root 100 3月  16 04:04 yumdrwxr-xr-x. 2 root root 248 4月  12 14:53 yum.repos.d
Second, while loop 1, while loop format 1:
while 条件; do        commanddone
Format 2: Dead loop
# 冒号代替条件while : ; do        command                sleep 30done
2, while loop instance 1: When the system load is greater than 10, send mail, every 30 seconds to execute
[[email protected] shell]# vim while01.sh#!/bin/bashwhile :do    load=`w|head -1 |awk -F ‘load average: ‘ ‘{print $2}‘| cut -d . -f1`    if [ $load -eq 0 ]    then       python /usr/lib/zabbix/alertscripts/mail.py [email protected] "load is high:$load" "$load"    fi    sleep 30done##python行表示使用邮件脚本发送负载状况,这里为了实验,把load设为等于0,mail.py脚本在之前zabbix实验中设置#‘while :‘表示死循环,也可以写成while true,意思是“真”#Attention:awk -F ‘load average: ‘此处指定‘load average: ‘为分隔符,注意冒号后面的空格#如果不加该空格,过滤出来的结果会带空格,需要在此将空格过滤掉

Experimental results:

Note: If you do not manually stop the script, it will continue to loop (at the end of CTRL + C), with screen in the actual environment.

Example 2: In interactive mode, the user enters a character to detect whether the character meets the criteria, such as: empty, non-numeric, numeric. The characters are judged separately, and then different responses are made.
[[email protected] shell]# vim while02.sh #!/bin/bashwhile truedo  read -p "Please input a number:" n  if [ -z "$n" ]  then       echo "You need input some characters!"      continue  fi  n1=`echo $n|sed ‘s/[-0-9]//g‘`  if [ -n "$n1" ]  then      echo "The character must be a number!"      continue  fi  breakdoneecho $n#continue:中断本次while循环后重新开始;#break:表示跳出本层循环,即该while循环结束
Iii. break, continue and exit usage

Break jumps out of the loop
Continue end of this cycle
Exit exits the entire script

[[email protected] shell]# vim break.sh #!/bin/bashfor i in `seq 1 5`do  echo $i  if [ $i -eq 3 ]  then      break           #此处课替换continue和exit,执行脚本查看区别  fi  echo $idoneecho $i

Shell Script Basics (iii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.