CCFFFF] [color=# 00 FFFF] 1, while loop
#!/bin/bash
# while - count: display a series of Numbers
The count=1
While [$count - le 5]; Do
Echo $count
Count=$((count + 1))
The done
Echo "Finished"
2, while improving the menu program
#!/bin/bash
# while - menu: a menu driven system information program
DELAY=3 # number of seconds to display the results
While the [[$REPLY!=0]]. Do
The clear
The cat & lt; <- _EOF_
Both Please Select:
1. The Display System Information
2. The Display Disk Space
3. The Display Home Space Utilization
0. Quit
_EOF_
The read - p "Enter selection [0, 3] & gt; "
If the [[$REPLY=~ ^ [0, 3] $]]. Then
If the [[$REPLY==1]]. Then
Echo "Hostname: $Hostname
"Uptime
Sleep $DELAY
Fi
If the [[$REPLY==2]]. Then
Df -h
Sleep $DELAY
Fi
If the [[$REPLY==3]]. Then
If the [[$(id - u) - eq 0]]. Then
Echo "Home Space Utilization (All Users)"
Du - sh/home/*
The else
Echo "Home Space Utilization ($USER)"
Du - sh $HOME
Fi
Sleep $DELAY
Fi
The else
Echo "Invalid entry."
Sleep $DELAY
Fi
The done
Echo "the Program terminated."
3, jump out of the loop
#!/bin/bash
# while - menu2: a menu driven system information program
DELAY=3 # number of seconds to display the results
while true; Do
The clear
The cat & lt; <- _EOF_
Both Please Select:
1. The Display System Information
2. The Display Disk Space
3. The Display Home Space Utilization
0. Quit
_EOF_
The read - p "Enter selection [0, 3] & gt; "
If the [[$REPLY=~ ^ [0, 3] $]]. Then
If the [[$REPLY==1]]. Then
Echo "Hostname: $Hostname
"Uptime
Sleep $DELAY
The continue
Fi
If the [[$REPLY==2]]. Then
Df -h
Sleep $DELAY
The continue
Fi
If the [[$REPLY==3]]. Then
If the [[$(id - u) - eq 0]]. Then
Echo "Home Space Utilization (All Users)"
Du - sh/home/*
The else
Echo "Home Space Utilization ($USER)"
Du - sh $HOME
Fi
Sleep $DELAY
The continue
Fi
If the [[$REPLY==0]]. Then
Break
Fi
The else
Echo "Invalid entry."
Sleep $DELAY
Fi
The done
Echo "the Program terminated."
4, until: while command exit status is not 0 means that the termination of circulation, and the opposite until command, will received 0 exit status is terminated,
#!/bin/bash
# until - count: display a series of Numbers
The count=1
Until [$count - gt 5]; Do
Echo $count
Count=$((count + 1))
The done
Echo "Finished"
5, the for loop
For variable (in words), Do
Commands
The done
Example: the command line input:
For I in A, B, C, D. Do the echo $I; The done
For I in {A.. D}; Do the echo $I; The done
For I in D *; Do the echo $I; The done
6, for: C form
For ((expression1; Expression2; Expression3)); Do
Commands
The done
Example:
#!/bin/bash
C # simple_counter: demo of style for the command
For ((I=0; I<5; I=I + 1)); Do
Echo $I
The done [color=# FF00FF] [/color] [/color]