loop control and status return values

Source: Internet
Author: User
Tags echo command

1.break, continue, exit, return the difference

Break, continue is used in conditional statements and loop statements (for, while, if, and so on) for the direction of the control program; While exit terminates all statements and exits the current script, exit can also return the execution state value of the last program or command to the current shell; Ruturn is similar to exit, except that return returns the status value of the function execution inside the function. The basic instructions are as follows:

2.break, continue, Exit Function execution flowchart

Examples are in the while loop and for loop. The break function executes the flowchart as shown:

The Continue function execution flowchart is as follows:

The execution flowchart for the Exit function is as follows:

3.break, continue, exit, return command example

Example: Verify the functionality of the break, continue, exit, and return commands with the following script

[[email protected] jiaobenlianxi]# cat xunhuankongzhi.sh#!/bin/bashif [ $# -ne 1 ];then   如果参数不为执行if语句里面的echo命令echo $"Usage:$0 {break|continue|exit|return}" 分别传入4个命令作为参数exit 1  退出脚本fifunction test(){  定义测试函数for((i=0;i<=5;i++))doif [ $i -eq 3 ];then$*;         用于接收函数体外的参数fiecho $idoneecho "I am in func." 执行for循环外的输出提示}test $*    调用test函数,实现脚本传参func_ret=$?    接收并测试函数的返回值if [ `echo $*|grep return|wc -l` -eq 1 ] 如果传参是return执行难,if语句中echo命令thenecho "return‘s exit status:$func_ret" 如果传参是return,打印函数的返回值fiecho "ok"             函数体外的输出提示

(1) Verify the break command and execute the result as follows

[[email protected] jiaobenlianxi]# sh xunhuankongzhi.sh Usage:xunhuankongzhi.sh {break|continue|exit|return}[[email protected] jiaobenlianxi]# sh xunhuankongzhi.sh break012I am in func.ok

As you can see from the results, when I=3 executes the break command loop, subsequent commands are not executed, but the echo command outside the loop executes, jumps out of the IF and for loop when it executes to break, then executes the echo command in the test function and the echo command outside the test function body.

(2) Verify the Continue command and execute the result as follows

[[email protected] jiaobenlianxi]# sh xunhuankongzhi.sh continue01245I am in func.ok

It can be seen that when the i=3, the loop is not executed, the other loops are all OK, the echo outside the loop is also executed, which means that the execution to continue, the end of the cycle, and continue the next cycle until the loop ends normally, and then executes all statements outside the loop.

(3) Verify the Exit command, in the next shell you can use "$?" Receives the return value of exit N and executes the result as follows:

[[email protected] jiaobenlianxi]# sh xunhuankongzhi.sh "exit 100"012[[email protected] jiaobenlianxi]# echo $?100

It can be seen from the results that when the "Exit 100" is encountered after entering the IF statement in the loop, the leg is immediately out of the program, not only the number behind the loop body 3 is not output, and the For loop body done outside of the echo and the function of the OK also does not output, directly exited the program. Also because the program exits with 100 specified, so execute the script with "$?" Getting the return value returns 100.

(4) Verify the return command and execute the result as follows

[[email protected] jiaobenlianxi]# sh xunhuankongzhi.sh "return 100"012return‘s exit status:100ok[[email protected] jiaobenlianxi]# echo $?0

When executed to return 100, there is no 31 number, indicating that return jumps out of the loop body, the For loop body done after the ECHO is not executed but directly executed after the function body outside of the content, you can see the function of return is to exit the current function. With return returns the number 100 as the function's return value to the function body, the value of the return value $func_ret equals 100. The return value printed after the script is executed is 0 because the program prints the echo command after the left, execution succeeds, and the last return value of the script execution is 0.

4. Case studies

(1) Case 1: Development shell script implementation for the server temporarily configured multiple IPs, and can not temporarily revoke the configuration of all Ip,ip address range is: 192.168.136.220-192.168.136.225, where 192.168.136.223 cannot be set.

To set an alias for an IP using ifconfig:

ifconfig eth0:0 192.168.136.224/24 up  添加别名ifconfig eth0:0 192.168.136.224/24 down  删除别名

To set an alias for an IP using IP addr:

ip addr add 192.168.136.224/24 dev eth0 label eth0:0  添加别名ip addr del 192.168.136.224/24 dev eth0 label eth0:0  删除别名

The code is as follows:

  [[[email protected] jiaobenlianxi]# cat ipalias.sh #!/bin/bash[-F "/etc/init.d/functions"] && . /etc/init.d/functionsretval=0export path= "/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/ Sbin:/usr/bin:/usr/local/curl/bin:/root/bin "function Add () {for IP in {220..225}doif [$ip-eq 223];THENCONTINUEFIIP Addr Add 192.168.136. $ip/24 Dev eth0 label eth0: $ip &>/dev/nullretval=$?if [$RETVAL-eq 0];thenaction "Add $ip"/ Bin/trueelseaction "Add $ip"/bin/falsefidonereturn $RETVAL}function del () {for IP in {225..220}do if [$ip-eq 223];then Continue Fiip addr del 192.168.136. $ip/24 Dev eth0 label eth0: $ip &>/dev/nullretval=$?if [$RETVAL-eq 0];thenacti On "del $ip"/bin/trueelseaction "del $ip"/bin/falsefidonereturn $RETVAL}function Main () {case "$" instart) Add retval=  $? ;;  Stop) del retval=$?;;   Restart) Del sleep 2 add retval=$? ;; *) printf "usage:$0 {start|stop|restart}\n" Esac}main $1exit $RETVAL  

(2) Case 2: Analyze Apache access logs, add the number of fields corresponding to the number of bytes per row in the log, and calculate the amount of traffic. Implemented with a while loop. When the friends do the test, if there is no access log, you can download an access log online.

[[email protected] jiaobenlianxi]# cat apachefangwen.sh #!/bin/bashsum=0RETVAL=0byte="1024"b="/home/linzhongniao/tools/access_2013_05_30.log"exec <$bwhile read line do   size=`echo  $line|awk ‘{print $10}‘|grep -v "-"|tr ‘\r‘ ‘ ‘`     expr $size + 1 &>/dev/null   if [ $? -ne $RETVAL ];thencontinue   fi   ((sum+=size))doneecho "${b}:total:${sum}bytes = `expr ${sum} / $byte`KB"

(3) Case 3: Crack all numbers in the range of random number "0~32767" generated by random with md5sum before encryption?

The resolution process, the 0-32767 range of all the numbers through md5sum encryption, and the encrypted string and before the encryption of the number corresponding to the write to the log file, the script is as follows:

[[email protected] jiaobenlianxi]# cat RANDOM.sh #!/bin/bashfor n in {0..32767}doecho "`echo $n|md5sum` $n" >>/tmp/zhiwen1.logdone

To view the execution results:

[[email protected] jiaobenlianxi]# head /tmp/zhiwen.log 897316929176464ebc9ad085f31e7284  - 0b026324c6904b2a9cb4b88d6d61c81d1  - 126ab0db90d72e28ad0ba1e22ee510510  - 26d7fce9fee471194aa8b5b6e47267f03  - 348a24b70a0b376535542b996af517398  - 41dcca23355272056f04fe8bf20edfce0  - 59ae0ea9e3c9c6e1b9b6252c8395efdc1  - 684bc3da1b3e33a18e8d5e1bdd7a18d7a  - 7c30f7472766d25af1dc80b3ffc9a58c7  - 87c5aba41f53293b712fd86d08ed5b36e  - 9

Decrypt the encrypted string 3ae5dc2d, and all the strings inside the fingerprint library use md5sum encrypted string to match the number before the output is encrypted.

[[email protected] jiaobenlianxi]# cat md5pojie.sh   #!/bin/bash#for n in {0..32767}#do#   echo "`echo $n|md5sum` $n" >>/tmp/zhiwen.log#doneexec </tmp/zhiwen.logmd5char="3ae5dc2d"while read linedo  if [ `echo "$line"|grep "$md5char"|wc -l` -eq 1 ];then  echo "$line"  break  fidone

Execution result: The number before 3AE5DC2D encryption is 10000

[[email protected] jiaobenlianxi]# sh md5pojie.sh 154773ae5dc2d36d8b9747e5d3dbfc36  - 10000

loop control and status return values

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.