BASH programming: Looping statements

Source: Internet
Author: User

Looping: Performing a loop body code 0, 1, or more

Entry conditions: Conditions for entering the cycle

Exit Condition: Condition of the circular termination

For loop:

For VARIABLE in LIST; Do

Loop body

Done


Example 1: Write a script, create 20 users, and give the user a user name as a password

#!/bin/bash#for user in user{1..10}; do if ID $user; Then echo, "User is exists." else Useradd $user echo "Add user $user finished." Fidone


Example 2: Write a script to see the types of all files in a directory

Cat for1.sh#!/bin/bash#for filename in/var/*; Do file $filenamedone


Example 3: Write a script to view the user's primary group

#!/bin/bash#for user in $ (cut-d:-F1/ETC/PASSWD); Do echo "$user Primary Group: $ (Id-n-G $user)." Done


While loop:

While Condtion; Do

Loop body

Done

Entry condition: When condition is "true"

Exit Condition: When condition is "false"


Example: Write a script that asks for the sum of all positive integers within 100

#!/bin/bash#declare-i sum=0declare-i i=1while [$i-le 100]; Do let sum+= $i let I++doneecho "sum: $sum"


Example: Write a script that prints a 99 multiplication table

#!/bin/bash#declare-i i=1declare-i J=1while [$j-le 9]; Dowhile [$i-le $j]; Do echo-e-n "${i}x${j}=$[$i * $j]\t" let I++done echo let I=1 let J++done


UNITL Cycle:

Until CONDITION; Do

Loop body

Modified expressions for cyclic control variables

Done

Entry condition: When condition is "false"

Exit Condition: When condition is "true"


Example: Write a script that creates a file if the file does not exist

#!/bin/bash#file=/root/testfile until [-f $file]; Do touch $file echo ' file $file is touch finished ' done


Loop control:

Continue [n]: End the cycle in advance, and go directly to the next round;

Break [n]: End cycle prematurely;


Example: Write a script that asks for the sum of all even numbers within 100

#!/bin/bash#declare-i sum=0declare-i i=0while [$i-le 100]; Do let i++if [$[$i%2]-eq 1]; Then echo "A odd" Continuefi let sum+= $idoneecho "sum: $sum"


Example: see if a user is logged in at a certain time

#!/bin/bash#username=$1declare-i Count=0while true; do if who | grep "^ $username" &>/dev/null;   Then echo, "$username is logged." Break else let count++ echo "$count: $username was not login" fi sleep 3done


Special usage of the while loop:

Traverse each line of the file:

while read VARIABLE; Do

Loop body

Done </path/from/some_file


Example: Find all users whose UID is even, display their user name and ID number;

#!/bin/bash#while read line; douserid=$ (echo $line | cut-d:-F3) If [$[$userid%2]-eq 0]; then echo $line | Cut-d:-f1,3fidone </etc/passwd


Special usage for the FOR loop:

For ((EXPR1;EXPR2;EXPR3)); Do

Loop body

Done

EXPR1: Defines the control variable and assigns the initial value;

EXPR2: cyclic control conditions;

Entry condition: Control condition is "true"

Exit Condition: Control condition is "false"

EXPR3: Correcting control variables


Example: The sum of all positive integers within 100 is obtained;

#!/bin/bash#declare-i sum=0 for ((i=1;i<=100;i++)); Do let sum+= $idoneecho "sum: $sum."


BASH programming: Looping statements

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.