shell脚本(一)

1、打印九九乘法表

[root@centos7 scripts]# vim 99chengfb.sh 
#!/bin/bash
for i in {1..9};do
	#for j in `seq 1 $i`;do
	#for j in `eval echo {1..$i}`;do
	for j in $(eval echo {1..$i});do 
		result=$[$j*$i]
		echo -e "${j}x${i}=$result \t\c"
	done
	echo
done

运行结果

[root@centos7 scripts]# ./99chengfb.sh 
1x1=1 	
1x2=2 	2x2=4 	
1x3=3 	2x3=6 	3x3=9 	
1x4=4 	2x4=8 	3x4=12 	4x4=16 	
1x5=5 	2x5=10 	3x5=15 	4x5=20 	5x5=25 	
1x6=6 	2x6=12 	3x6=18 	4x6=24 	5x6=30 	6x6=36 	
1x7=7 	2x7=14 	3x7=21 	4x7=28 	5x7=35 	6x7=42 	7x7=49 	
1x8=8 	2x8=16 	3x8=24 	4x8=32 	5x8=40 	6x8=48 	7x8=56 	8x8=64 	
1x9=9 	2x9=18 	3x9=27 	4x9=36 	5x9=45 	6x9=54 	7x9=63 	8x9=72 	9x9=81

2、打印等腰三角形。(星个数:start=line*2-1;空格个数:space=总行数-line)

[root@centos7 scripts]# cat triangle.sh 
#!/bin/bash
read -p "please input a num " NUM
for i in `seq 1 $NUM`;do
	for j in `seq 1 $[$NUM-$i]`;do
		echo -e " \c"
	done
	for k in `seq 1 $[$i*2-1]`;do
		echo -e "*\c"
	done
	echo
done
[root@centos7 scripts]# ./triangle.sh 
please input a num 5
    *
   ***
  *****
 *******
*********
[root@centos7 scripts]# 

3、打印国际象棋棋盘

[root@centos7 scripts]# cat color.sh 
#!/bin/bash
greyel (){
	echo -e "\033[1;42m  \033[1;43m  \033[0m\c"
}
yelgre (){
	echo -e "\033[1;43m  \033[1;42m  \033[0m\c"
}
for i in {1..8};do
	for ((j=1;j<=4;j++))do
		if [ $[$i%2] -eq 0 ];then
			greyel 
		else
			yelgre 
		fi 
	done
	echo
done
[root@centos7 scripts]# ./color.sh 
              

4、批量创建用户和批量删除用户

[root@centos7 scripts]# cat batch_creatuser.sh 
#!/bin/bash
until [ "$#" -le 0 ];do
	useradd $1
	echo "$1 is created"
	shift
done
echo "done"
[root@centos7 scripts]# cat batch_deleteuser.sh 
#!/bin/bash
#while [ "$#" -gt 0 ];do
while [ ! -z "$1" ];do
	userdel -r $1 &> /dev/null && echo "$1 is deleted"
	shift
done
echo "done"

5、判断系统的版本号

[root@centos7 scripts]# cat check_version.sh 
#!/bin/bash
version (){
	ver=`sed -r 's/.*[ ]([0-9]+)\..*/\1/' /etc/redhat-release`
	if [ $ver -eq 6 ];then
		echo this is $ver version!!!
		echo this verison is old!!! 
	else
		echo this is $ver version!!!
	fi
}
version
[root@centos7 scripts]# ./check_version.sh 
this is 7 version!!!

6、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出

[root@centos7 scripts]# cat guest.sh 
#!/bin/bash
rand=$[$RANDOM%11]
while true;do
	read -p "please input a number " NUM
	if [[ $NUM =~ ^[[:digit:]]+$ ]] ;then
		if [ $rand -gt $NUM ];then
			echo little
		elif [ $rand -lt $NUM ];then 
			echo great
		else 
			echo right
			break
		fi
	else
		echo "please input a number:1-10"
		continue
	fi
done

运行结果

[root@centos7 scripts]# ./guest.sh 
please input a number 6
great
please input a number 5
great
please input a number 3
little
please input a number 4
right

7、实现阶乘

[root@centos7 scripts]# cat jiecheng.sh 
#!/bin/bash
read -p "please input a number: " NUM
fact() {
	if [ $1 -eq 0 -o $1 -eq 1 ]; then
	echo 1
	else
	echo $[$1*$(fact $[$1-1])]
	fi
}
fact $NUM
[root@centos7 scripts]# ./jiecheng.sh 
please input a number: 9
362880

8、实现100以内所有正奇数之和,实现1到100所有正数之和

[root@centos7 scripts]# ./test_for.sh 
sum=5050
sum1=2500
[root@centos7 scripts]# cat test_for.sh 
#!/bin/bash
declare -i sum=0
declare -i sum1=0
for i in {1..100};do
	let sum=sum+i
done
echo sum=$sum
for i in {1..100..2};do
	let sum1+=i
done
echo sum1=$sum1

[root@centos7 scripts]# ./test_for.sh 
sum=5050
sum1=2500

9、case测试脚本

[root@centos7 scripts]# cat test_case.sh 
#!/bin/bash
read -p "DO you argee? yes or no:" ANS
if [ -z "$ANS" ];then
	echo "please input yes or no"
	exit
fi
case $ANS in 
[Yy]|[Yy][Ee][Ss])
	echo Your answer is YES
	;;
[Nn]|[Nn][Oo])
	echo Your answer is NO
	;;
*)
	echo Your answer is false
esac
[root@centos7 scripts]# ./test_case.sh 
DO you argee? yes or no:y
Your answer is YES

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值