Linux-based shell programming (4)-for loop

Source: Internet
Author: User
Tags shebang

For loop

Usage:

forindo    循环体done
So how do you build the list?
{1..100}

This will generate the list.
===========================================
Cases:

#!/bin/bash#forin {1..100};do    echo$Idone
Seq

You can also generate lists with the SEQ command

Usage:

seq [起始数 [步进长度]] 结束数

Cases:

[Root@iZ28g26851kZ~]# seq 1123456789Ten[Root@iZ28g26851kZ~]# seq 1 213579[Root@iZ28g26851kZ~]# seq123456789Ten[Root@iZ28g26851kZ~]# 
The application of SEQ in the For loop
#!/bin/bash#forin1210`;do    echo$Idone

Remember to use anti-single quotes to cause yo ~

Example: for 1 to 100 and

#!/bin/bash#sum=0forin100`;do   let sum=$sum+$Idoneecho"sum=${sum}"
Declare

As I've said before, in bash, variables are all character types by default.

How can a variable be declared as an integral type directly?

Declare can declare variables directly as integral type

#!/bin/bash#declare -i sum=0forin100`;do   let sum=$sum+$Idoneecho"sum=${sum}"

Example: Say hello to every user in the system

#!/bin/bash#forin-d-f1`;do    echo"hello ${NAME}~"done

Example: Say hello to every user in the system and show their shell~

#!/bin/bash#forin-d-f1`;do    echo"hello ${NAME}~...........`cat /etc/passwd | grep ^$NAME | cut -d: -f7`"done

Example: Only the user question mark for the shell in the system is bash,

#!/bin/bash# forNAMEinch' CAT/ETC/PASSWD | Cut- D:- F1 '; DoShell_name= ' cat/etc/passwd | grep ^$NAME| Cut- D:- F7 | Sed-r' [Email protected]^/.*/(. *) @\[email protected] '`if[$SHELL _name=="Bash"]; Then        Echo "Hello ${name}~ ..... ' CAT/ETC/PASSWD | grep ^$NAME | cut-d:-f7 ' "    fi Done

Linux-based shell programming (4)-for loop

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.