Linux command: For loop

Source: Internet
Author: User

For loop:

loop operation, for Loop statement

1 . Command format:

for variable in list;

Loop body

Done

2 . Command function:

Execute Loop statement

3 . Command parameters:

for variable in list;

Loop body

Done

The list generation method for the For loop:

{N.. m}: Through curly braces, the middle is the variable traversal range, from N to m are variable traversal

' seq [ start number [step length]] end number ' SEQ 1 2 10 ' variable with a traversal value of 1 3 5 7 9 over 10 exit traversal

{1..100}

Declare-i sum=0 Declare declaring variable-I declares a variable to be an integral integer

Declare-x declaring variables as environment variables

4 . Command instance:

1, calculated from 1 to 100 and

#!/bin/bash

# program:

# calculates 1 to 100 of the and

# History Donggen 2016-10-21-20:30

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Declare-i sum=0


For I in {1..100}; Do #或者 for I in ' SEQ 1 100 '

Let sum= $SUM + $I

Done

echo "1+. +100= $SUM "

2. write a script:

1 , the value of the set variable file is/etc/passwd

2 , say hello to each user in/etc/passwd, and show each other's shell as follows:

Hello, Root, your Shell:/bin/bash

#!/bin/bash

#program:

# Say hello to the users inside/etc/passwd

#history 2016-10-21-21:30

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

lines= ' Wc-l/etc/passwd | Cut-d '-f1 '

For I in ' seq 1 $LINES '; Do

echo "Helo, ' head-n $I/etc/passwd | Tail-1 | cut-d:-f1 ' Your shell is: ' Head-n $I/etc/passwd | Tail-1 | cut-d:-f7 ' "

Done

This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1864425

Linux command: For loop

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.