Fourth day of linux Shell Study Notes

Source: Internet
Author: User

The scripts for this lesson are also shared in share.

Day 4: cyclic structure

Content outline:

Application Instance Analysis

For Loop

While Loop

Principle of for Loop

Achieve results

According to yesterday's menu modification. After selecting 1, 2, and 3, you will be prompted whether to continue the selection. Select 4 to exit.

For variable in list variable list pool

Do

Command1

Command2

Done

Instance 1

Forlist. sh

#! /Bin/bash

# For with list

For loop in 1 2 3 4 5 variable 1 2 3 4 5 defined loop is the Variable list

Do

Echo $ {loop}

Done

If there is no, that is, 0, the loop is stopped.

Instance 2

Var.txt to prove that variables are separated by carriage return or space, unless they are separated by double quotation marks

1 22

3

4

5

6

Forfile. sh

#! /Bin/bash

For loop in 'cat var.txt'

Do

Echo $ {loop}

Done


Instance 3

Write a script to automatically generate a class c ip address segment

The IP address segment network is specified by the user

./Ipaddr. sh 192.168.1

192.168.1.1

192.168.1.2

......

192.168.1.254

#! /Bin/bash

For ip in 'seq 1 100' seq generates step 1, that is, the value of 1-254

Do

Echo $ {1}. $ {ip}

Done



For loop nesting

For variable name 1 in LIST 1

Do

For variable name 2 in List 2

Do

Command 1

...

Done

Done

Instance 4

Write a script to automatically generate a class c ip address segment

The IP address segment network is specified by the user, and the host end is specified by the user

./Ipaddr2.sh 192.168.1 100

192.168.1.1

192.168.1.2

......

192.168.168.1.100

#! /Bin/bash

For ip4 in 'seq 1 ${2 }'

Do

Echo $ {1}. $ {ip4}

Done


#! /Bin/bash

For ip3 in 'seq 1 5'

Do

For ip4 in 'seq 1 ${2 }'

Do

Echo $ {1}. $ {ip3}. $ {ip4}

Done

Done


Principle of while LOOP


Boundary test = conditional test

True/false testing.

If it is always true, it will not stop.

While format

While Condition

Do

Command 1

Command 2

Done

While application scenarios

While endless loop

While ["1" = "1"]

Do

...

Done

While condition Loop

While [$ {num}-lt 10] is less than 10, always repeating

Do

...

Done

Solve the problem.

Use while to generate an endless loop

Exit the entire script

Menu-while.sh

#! /Bin/bash

While ["1" = "1"]

Do

Clear

Echo "------- menu ------"

Echo "1) Show Time"

Echo "2) CPU load"

Echo "3) Memory free"

Echo "0) Exit"

Echo "---------------"

Echo-n "Enter you chose [0-3]:"

Read Chose

Case $ {Chose} in

0) exit ;;

1) date + % T ;;

2) uptime | awk-F' [,:] ''{print $7 }';;

3) free-m | awk '$1 = "Mem:" {print $4 }';;

*) Echo "This is not between 0-3 .";;

Esac

Echo-n "Do you contine [y/n]:"

Read contine

If ["$ {contine}" = "n"-o "$ {contine}" = "N"]

Then

Exit

Fi

Done


Exercises after class

Write a script to generate a level-2 menu.

Level 1 has three functions: cpu info, memory info, and exit

Cpu info has four function items in Level 2: display the cpu type, display the cpu load, return to the main menu, exit the script

Memory Level 2 has four functions: display the remaining memory and swap, return to the main menu, and exit the script.

Summary

Features of for Loop

Unlike C and java, it does not control the number of times based on judgment statements.

It is difficult to implement an endless loop. It is easy to execute an infinite loop while.

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.