Loop Control of shell script Programming

Source: Internet
Author: User

Loop Control Structure of shell script Programming

Loop Control-for Loop

Syntax 1

For Variable in List

Do

Commands

Done

Syntax structure 2

For Variable in List; do

Commands

Done

This List can be a List, variable, command, etc.

A for Loop provides an element list in advance, and then uses variables to traverse the element list. Each time an element is accessed, a loop body is executed until the element access is completed.

1. The List in the for loop is a List

Eg1: displays the number of lines in the/etc/inittab,/etc/rc. d/rc. sysinit, and/etc/fstab files;

#!/bin/bashfor File in /etc/inittab /etc/rc.d/rc.sysinit /etc/fstab;do Row=`wc -l $File | cut -d' ' -f1`echo "$File has: $Row rows"done

Running result

2. The List in the for loop is a variable.

Eg2: displays the username and ID of a user whose current id is greater than 500;

 

#! /Bin/bashuseradd user1useradd user2useradd user3 # create several new users. The test result Id is 'cat/etc/passwd | awk-F: '{print $3} ''for Var in $ Id; doif [$ Var-ge 500]; then User = 'grep "$ Var \>"/etc/passwd | cut-d:-f1 'echo "$ User uid is $ Var" fidone

Running result

3. The List in the for loop is a command

Eg3: displays the username and shell of the bash user.

The displayed result is Bash user: root,/bin/bash.

Analysis: Determine users by shell ending with bash, and then output these users one by one

 

#!/bin/bashfor Var in `grep "bash\>" /etc/passwd | cut -d: -f7`;doUser=`grep "$Var" /etc/passwd |cut -d: -f1`doneShell=`grep "bash\>" /etc/passwd |cut -d: -f7 |uniq`for name in $User;doecho "Bash user:$name,$Shell"done

Running result

4. The List in the for loop is a series of numbers.

Eg4: Calculate the sum of an Even number (Even number), an Odd number (Odd number), and.

Analysis: When a number and 2 are used for remainder calculation, if the value is 1, the number is an odd number, and the opposite is an even number.

 

#!/bin/bashEvenSum=0OddSum=0for I in `seq 1 100`;do  if [ $[$I%2] -eq 1 ]; then    OddSum=$[$OddSum+$I]  else    EvenSum=$[$EvenSum+$I]  fidoneecho "EvenSum: $EvenSum."echo "OddSUm: $OddSum."

Running result

5. for Loop in C language format

Eg5: Add a user from user520 to user530 with the same password as the user name.

 

#!/bin/bashfor ((i=520;i<=530;i++));douseradd user$iecho "Add user$i."echo user$i | passwd -stdin user$i &>/dev/nulldone

Running result: (you can switch a user to check whether the password is the same as the user name)

The format of other loops is as follows. you can master one loop for all these loops.

While loop Command Format

While test command

Do

Other command

Done

Command Format of until Loop

Until test command

Do

Other command

Done

For a script interview question, you can reply your answer below (for discussion)

Pass a parameter to display all users whose default shell is bash and whose default shell is/sbin/nologin on the current system, and count the total number of users under various shell types.

Run bash eg. sh bash. The result is as follows:

BASH, 3 users, they are:

Root, redhat, gentoo,

Run bash eg. sh nologin. The result is as follows:

NOLOGIN, 2 users, they are:

Bin, ftp,

======= The knowledge learned by the blogger comes from the hands-on Teacher Ma Ge ======

Marco education's "2014 summer camp" has begun !!! Mago education is currently the most cost-effective Linux training, ranking first in the country, and known as the "Whampoa Military Academy" in the Linux field by netizens. All the courses are explained using Centos6.5x86 _ 64, after summing up and refining several network classes, the course system has been gradually improved. The students' learning progress is monitored and the quality test system tests the students' mastery, active online Q & A sessions, accompanied by famous teachers, you can't miss it.
Details DASH: http://www.magedu.com/
Course: http://www.magedu.com/mentuqc
Network Class integration service: http://mageedu.blog.51cto.com/4265610/1379598

Contact: customer service QQ 2813150558 customer service QQ 1661815153

========================================================== ====

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.