Shell脚本for、while、continue、break和函数调用的用法

Shell脚本for、while、continue和break的用法
for循环的使用

#!/bin/bash

echo "*****************************"
#***********************************
#数字性循环
for ((i=1;i<=10;i++))
do
	echo `expr $i \* 2`
done
echo "*****************************"
#***********************************
#这里的seq是打印一串数字的队列
for i in `seq 1 2 10`
do
	echo $i
done
echo "*****************************"
#***********************************
for i in {
     1..10}
do
	echo $i
done
echo "*****************************"
#***********************************
#字符性循环
for i in `ls`
do 
	echo $i
done
echo "*****************************"
#***********************************
for i in $*
do
	echo $i
done
echo "*****************************"
#***********************************
for i in f1 f2 f3
do
	echo $i
done
echo "*****************************"
#***********************************
English="How are you"
for i in $English
do
	echo $i
done
echo "*****************************"
#***********************************
for i in $HOME/*
do
	echo $i
done
echo "*****************************"
#***********************************
for i in `ls $HOME/LV2/shell/practice1/*`
do
	echo $i
done
echo "*****************************"
#***********************************

while循环的使用

#!/bin/bash

#***********************************
i=1
j=0
while [ $i -le 100 ]
do
	let j=j+i
	let i++ 
done
echo $j
#***********************************
#while循环查询分数
i=0
while [ $i -lt 1 ]
#while true #wihle后面跟true的话就是死循环
do
	echo -n "Please input score:"
	read N
	if [ $N -lt 0 -o $N -gt 100 ]
	then
		echo "Wrong input"
		exit
	else
		G=`expr $N / 10`
		case $G in
			9 | 10)
				echo "$N : A"
				;;
			6 | 7 | 8)
				echo "$N : B"
				;;
			*)
				echo "$N : C"
				;;
		esac
	fi
	i=`expr $i + 1`
done
#***********************************
#while循环批量创建文件
i=1
j=$#

if [ $j -eq 0 ]
then
	echo "usage:filename"
	exit
fi

while [ $i -le $j ]
do
	>file${i}
	i=`expr $i + 1`
done

echo -n "Do you want to delete the file?"
echo "  y or Y to delete;"
read D
if [ $D = y -o $D = Y ]
then
	i=1
	while [ $i -le $j ]
	do
		rm file${i}
		i=`expr $i + 1`
	done
fi

continue和break的使用

#!/bin/bash

#***********************************
#continue
echo -n "Please input a value:"
read VAL
if [ $VAL -lt 1 ]
then
	echo "Wrong input"
	exit
fi

i=1
while [ $i -le $VAL ]
do
	COUNT=$i
	i=`expr $i + 1`
	if [ `expr $COUNT % 2` -eq 1 ]
	then
		continue 
	fi
	echo "$COUNT"
done
echo "************************"


#***********************************
#break
I=1
while [ $I -le 5 ]
do
	J=1
	while [ $J -le 5 ]
	do
		if [ $J -gt 3 ]
		then
			#这里的break后面跟的2可以直接跳出两层while循环
			#这是与C语言不一样的地方
			break 2
		fi
		echo "$I  $J"
		J=`expr $J + 1`
	done
	I=`expr $I + 1`
done

两种函数的调用方法

#!/bin/bash

#***************函数1****************
fun1()
{
     
	#Shell函数中的变量默认都是全局变量,加上local后就变成局部变量了
	local X=`cat /etc/passwd | grep "^$1:" | wc -l`
	echo $X
}

echo -n "Please input user:"
read N

#函数fun1中的echo $X可以作为返回值
G=`fun1 $N`

if [ $G -eq 1 ]
then
	echo "$N is exist"
else
	echo "$N is not exist"
fi



#***************函数2****************
fun2()
{
     
	local X=`cat /etc/passwd | grep "^$1:" | wc -l`
	if [ $X -eq 1 ]
	then
		return 1
	else
		return 0
	fi
}

echo -n "Please input user:"
read N

fun2 $N

# "$?"用于接受上一个参数的结果
G=$?

if [ $G -eq 1 ]
then
	echo "$N is exist"
else
	echo "$N is not exist"
fi

你可能感兴趣的:(Shell脚本)