Linux Shell Learning Notes fourth Day _ other

Source: Internet
Author: User

The script for this lesson is also shared in the share.

Day Fourth: Circular structure

Content outline:

application Example Analysis

For loop

While loop

The principle of the For loop

Achieve effect

Modified according to yesterday's menu. Selecting 1, 2, 3 also prompts you to continue with the selected feature. Option 4 is still an exit operation.

For variable in list variable lists pool

Todo

Command1

Command2

Done

Instance One

forlist.sh

#!/bin/bash

#for with List

For loop in 1 2 3 4 5 variables that define loops 1 2 3 4 5 is a list of variables

Todo

Echo ${loop}

Done

When not followed, that is equal to 0, then stop the loop.

Example Two

Var.txt to prove that a variable is a carriage return or a space-delimited, unless double quotes are specifically delimited

1 22

3

4

5

6

forfile.sh

#!/bin/bash

For loop in ' Cat Var.txt '

Todo

Echo ${loop}

Done


Example Three

Write a script to automatically generate a class C IP address segment

The IP address segment network portion 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 254 ' seq produces a step of 1, or 1-254 value

Todo

Echo ${1}.${ip}

Done



For loop nesting

For variable name 1 in List 1

Todo

For variable name 2 in List 2

Todo

Command 1

...

Done

Done

Example Four

Write a script to automatically generate a class C IP address segment

The IP address segment network portion 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} '

Todo

Echo ${1}.${IP4}

Done


#!/bin/bash

For IP3 in ' SEQ 1 5 '

Todo

For IP4 in ' seq 1 ${2} '

Todo

Echo ${1}.${IP3}.${IP4}

Done

Done


The principle of the while loop


Boundary test = condition test

True and false test.

If it has been true, it will not stop, dead loop.

The while format

While condition

Todo

Command 1

Command 2

Done

While application scenario

While Dead loop

While ["1" = "1"]

Todo

...

Done

While condition loop

While [${num}–lt 10] is less than 101 straight loop

Todo

...

Done

Solve the problem.

Use while to produce a dead loop

Exits to exit the entire script

menu-while.sh

#!/bin/bash

While ["1" = "1"]

Todo

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 $} ';;

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

*) echo "This isn't between 0-3.";;

Esac

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

Read Contine

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

Then

Exit

Fi

Done


Practice after class

Write a script that produces a level two menu.

The first level has three function items: CPU info, memory info, exit

CPU Info There are four feature items in the second level: Show CPU type, show CPU load, return main menu, exit script

Memory There are four functional items in the second level: Display the memory surplus, the remaining amount of swap, return to the main menu, exit the script.

Summary review

Attributes for A For loop

He is not the same as C and Java, not according to the judgment statement to control the number of times

For implementing a dead loop is difficult. While doing a dead loop is simple.

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.