Shell loop

Shell loop control statements

1, if the format

a, script format

! # / bin / bash 
# # # This script is mainly exercise 20,190,909 
### This script is mainly used if loop; 
whether ### judges batch execution parameter is null; 
if [$ 1!] 
the then 
	echo "** ****** batch execution format: SH $ 0 BatchTask_1 " 
	BREAK 
Fi 
### determines whether the parameter is equal to batch BatchTask_1 
canshu BatchTask_1 = 
iF Test. 1 = $ $ canshu 
the then 
	SH. 1 $ the start.sh 
the else 
	echo" ***** **** batch execution parameters are not correct, the following parameters batch execution format "! 
	echo" ********* batch execution format: SH $ 0 BatchTask_1 " 
	BREAK 
fi

b, command line format:

 

if [ $(ps -ef |grep -c "ssh") -ge 1 ];then echo last >>/home/log/1/last_log.sh;fi

2、if else

#! / bin / bash 
parameters determine the batch execution ### is equal to BatchTask_1 
canshu = BatchTask_1 
IF the Test $ 1 = $ canshu 
the then 
	SH start.sh $ 1 
the else 
	echo "parameter ********* batch execution is incorrect Please parameters the following batch execution format "! 
	echo" ********* batch execution format: SH $ 0 BatchTask_1 " 
	BREAK 
fi

3、if elif else

#! / bin / bash 
### This script was written in the 20,190,909 
IF the Test $ 1 = M 
the then 
	echo "This was a man!" 
elif the Test $ 1 = F 
the then 
	echo "It's a woman!" 
the else 
	echo "of unknown sex, be fine ! " 
fi

4, for cycle;

a, script format:

#! / bin / bash 
# # # This script is mainly used for contact loop 
### This script was written 20,190,909 
for i in 1 2 3 4 5 
do 
	echo "This is $ i cycles!" 
DONE

b, command line format:

for i in 1 2 3 4 5; do echo "This is $ i cycles!"; done;

5, while circulation

for while loop continues to execute cycles series of commands, but also for reading data from the input file, usually a command for a test;

! # / bin / bash 
# # # This script is mainly used for practice while loop; 
### this script was written in 20,190,909; 
int = 1 
while (($ int <= 5)) 
do 
	RES = `expr $ int \ * $ int` 
	echo "$ int * int equal to $ RES $" 
	the let "$ int ++" 
DONE
	

a, while the test loop

! # / bin / bash 
# # # This script was written in the 20,190,910 
### This script is mainly used while circulating the Test; 
echo "=========== >> press Ctrl + d exit the script! !! " 
echo -n -e" Please enter the name you want to enter: \ the n-" 
the while the Read RES 
do 
	echo" $ RES iS a Little caiji !!! " 
DONE

b, infinite loop

! # / bin / bash 
### infinite loop 
int = 1 
the while to true 
do 
	echo "$ int" 
	the let "int ++" 
DONE 

### command line format; 
the while to true; do echo "to true"; DONE;

6, until the cycle

until loop executes a series of commands until a stop condition is true;

On the contrary until loop while loop processing mode determination condition returns false, the statements within the loop body continues, or out of the loop;

! # / bin / bash 
# # # This script is mainly used until loop; 
### this script was written 20.19091 million; 
echo "print output is less than the specific number of all integers" 
IF [$ 1!] 
the then 
	echo "==== === >> script execution format: sh $ 0 parameter 1 " 
	BREAK 
fi 
int = 0 
an until [int $ -gt $ 1] 
do 
	echo $ int 
	the let" int ++ " 
DONE

7、case

the shell is a multi-select statement case statement, a case statement may be used to match a pattern with a value, if the matching is successful, the corresponding command is executed.

#! / bin / bash 
### script is mainly used in this exercise Case 
### This script was written 20.19091 million 
IF [! $ 1] 
the then 
	echo "======== ===== execute script command format error ==== " 
	echo" execution ======== format: sh start.sh parameter " 
	BREAK 
the else 
	Case $ 1 in 
	BatchTasck_WZ) 
		SH start.sh BatchTasck_WZ 
		echo" batch being executed is: sh start.sh BatchTasck_WZ " 
		;; 
	BatchTasck_MS) 
		SH start.sh BatchTasck_MS 
		echo" batch being executed is: SH start.sh BatchTasck_MS " 
		;; 
	BatchTasck_HK) 
		SH start.sh BatchTasck_HK 
		echo" batch being executed is: SH start.sh BatchTasck_HZ " 
		;; 
	BatchTasck_ZA ) 
		SH start.sh BatchTasck_ZA  
		echo "batch being executed is: sh start.sh BatchTasck_ZA"
		;; 
	*) 
		echo "batch script parameters ================= >> MS is: BatchTask_MS" 
		echo "================ = batch script parameters >> WZ is: BatchTask_WZ " 
		batch script parameters echo" ================= >> HK is: BatchTask_HK " 
		echo" ====== batch script parameters =========== >> ZA is: BatchTask_ZA " 
		;; 
	esac 
fi

8 out of the loop

In the recycling process, and sometimes need out of the loop at the end of the cycle does not meet the conditions of the time, there are two commands you can realize this function: break and continue;

a、break

Allow all break out cycles (terminates the execution of all cycles later)

! # / bin / bash 
# # # This script is mainly used for exercise break command; 
### this script was written in 20.19091 million 
IF [$ 1!] 
the then 
	echo "This script execution ========== >> format: sh $ 0 parameter 1 " 
	echo" ========== >> 1 character format parameter " 
	BREAK 
the else 
	IF the Test $ 1 = hair 
	the then 
		echo" ========== >> this is not just fun !! " 
		BREAK 
	the else 
		echo" to the $ 1 brother !! " 
	fi 
fi

		

b、continue

continue with the break command similar to the command, only a little difference, he will not jump out of circulation all, just out of the current cycle;

! # / bin / bash 
# # # This script is mainly used for practice continue command; 
### this script was written 20.19091 million 
the while to true 
do 
	echo -n -e "Please enter the name you want to enter: \ the n-" 
	the Read name 
	IF [! $ name] 
	the then 
		the Continue 
	the else 
		IF the Test $ name = hair 
		the then 
			echo "========== >> this is not just fun !!" 
			the Continue 
		the else 
			echo "to the $ 1 brother !!" 
			. 5 SLEEP 
		Fi 
	Fi 
DONE

	

  

 

 

 

 

Guess you like

Origin www.cnblogs.com/tengjiang/p/11493869.html