While loop in shell programming

Source: Internet
Author: User

While Syntax:

While [condition]

Do

Command

........

Done

Special while usage:

① While true | while ;;

Do

Done ---------------------- endless loop

② While read line_value

Do

Done </path_to/file ------------- each time a row is retrieved from the file and assigned to line_value.

Exercise:

Use the while statement to capture bash users in/etc/passwd.

#! /Bin/bash
#
While read line
Do
Echo $ line | cut-d:-f1, 7 | grep "bash" &>/dev/null
If [$? -Eq0]
Then
Echo 'echo $ line | cut-d:-f1, 7 | grep "bash" | cut-d:-f1'
Fi
Done </etc/passwd

This article is from the keepalive blog, please be sure to keep this source http://jefferyyu.blog.51cto.com/2843635/1305537

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.