Shell programming for loops __HTML5 under Linux

Source: Internet
Author: User

Linux for loops can use break and continue keywords to jump out of loops, consistent with Java usage

a common for loop structure

#语法一 for
variable in value 1 value 2 value 3.
   Do
     program block done

# #语法二 The for
variable ' command '
   do
     program block

# #语法三 for
(initial value; Cycle control; Variable) do
    program block done
  

Two common test structure

1. Output 3 times uptime

#!/bin/bash for

i in 1 2 3 4 5 
does
echo "$i-->$ (uptime)"
done


2. Bulk extract all the. tar.gz files under the current folder

#!/bin/bash for

i in ' ls/*.tar.gz ' doing  
     tar-zxvf $i >/dev/null
   Done


3. To seek 1~100 and

#!/bin/bash

#注意变量赋值的时候, = must not have spaces on either side
sum=0 for
((i=1; i<=100; i++)) do  
   sum=$ (($sum + $i))
  Done

echo "1+2+3+...+100= $sum"







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.