【shell】脚本案例1

案例:

  1. 需求:猜商品价格。通过变量 RANDOM 来获取随机的价格,然后提示用户猜价格,并记录次数,猜中退出,或次数达到 5 也退出。
  2. 使用while读取文件,文件的内容如下
  3. 开发一个 rsync 起停脚本
  4. 示例:批量创建用户,用户名以 test 开头,按数字序号变化。一共添加 30 个账号,名称如:test01、test02、...、test10、....test30

    用户初始密码为 Abc123456

  5. 将之前使用 for 循环语句创建的 test01 - test30 用户删除

     

[root@openEuler ~]# cat while4.sh 
#!/bin/bash

price=$[ $RANDOM % 100 ]
time=0

while true
do
	read -p 'Please enter product price [0-99]: ' input
	let time++
	if [ $input -eq $price ]; then
		echo 'Good luck, you guessed it.'
		echo 'You have guessed $time times.'
		exit 0
	elif [ $input -gt $price ]; then
		echo "$input is to high"
	else
		echo "$input is to low"
	fi
	if [ $time -eq 5 ]; then
		echo "You have guessed is 5 times. exit"
		exit 1
	fi
done
[root@openEuler ~]# bash while4.sh 
Please enter product price [0-99]: 50
50 is to low
Please enter product price [0-99]: 80
80 is to high
Please enter product price [0-99]: 70
70 is to high
Please enter product price [0-99]: 60
60 is to low
Please enter product price [0-99]: 65
65 is to low
You have guessed is 5 times. exit
[root@openEuler ~]# 

 :使用while读取文件,文件的内容如下

# 1. 创建文件
[root@openEuler ~]# cat ips
192.168.72.131  22
192.168.72.132  23
192.168.72.133  22

# 2. 编写脚本 
[root@openEuler ~]# cat while6.sh 
#!/bin/bash

while read line
do
	IP=`echo $line|cut -d" " -f1`   # 也可以使用awk来实现,如:IP=`echo $line|awk '{print $1}'`
	PORT=$(echo $line|cut -d " " -f 2)
	echo "IP:$IP, PORT:${PORT}"
done < ips

# 3. 运行测试
[root@openEuler ~]# bash while6.sh 
IP:192.168.72.131, PORT:22
IP:192.168.72.132, PORT:23
IP:192.168.72.133, PORT:22

练习:开发一个 rsync 起停脚本

[root@openEuler ~]# cat my_rsync.sh 
#!/bin/bash

if [ "$#" -ne 1 ] ; then
	echo "Usage: $0 {start|stop|restart}"
	exit 1
fi

case "$1" in
	"start")
		/usr/bin/rsync --daemon
		sleep 1
		if [ `ss -lntup|grep rsync|wc -l` -ge 1 ] ; then
			echo "rsync is start"
			exit 0
		fi
		;;
	"stop")
		killall rsync &>/dev/null
		sleep 1
		if [ `ss -lntup|grep rsync|wc -l` -eq 0 ] ; then
			echo "rsync is stop"
			exit 0
		fi
		;;
	"restart")
		if [ `ss -lntup|grep rsync|wc -l` -ge 1 ] ; then
			killall rsync &>/dev/null
			sleep 1
		fi
		/usr/bin/rsync --daemon
		sleep 1
		echo "rsync is restarted"
		exit 0
		;;
	*)
		echo "Usage: $0 {start|stop|restart|"

esac


[root@openEuler ~]# bash my_rsync.sh 
Usage: my_rsync.sh {start|stop|restart}
[root@openEuler ~]# bash my_rsync.sh start
rsync is start
[root@openEuler ~]# bash my_rsync.sh stop
rsync is stop
[root@openEuler ~]# bash my_rsync.sh restart
rsync is restarted

示例:批量创建用户,用户名以 test 开头,按数字序号变化。一共添加 30 个账号,名称如:test01、test02、...、test10、....test30

用户初始密码为 Abc123456

[root@openEuler ~]# cat for6.sh
#!/bin/bash

for ((i=1;i<=30;i++))
do
	if [ $i -lt 10 ]; then
		user=test0$i
	else
		user=test$i
	fi
	if ! id -u $user &> /dev/null
	then
		useradd $user
		echo "Abc123456" | passwd --stdin $user &> /dev/null
	else
		echo "$user is exists"
	fi
done

[root@openEuler ~]# bash for6.sh
[root@openEuler ~]# grep test /etc/passwd
test01:x:1001:1001::/home/test01:/bin/bash
test02:x:1002:1002::/home/test02:/bin/bash
test03:x:1003:1003::/home/test03:/bin/bash
test04:x:1004:1004::/home/test04:/bin/bash

或者

[root@openEuler ~]# cat for7.sh 
#!/bin/bash

for u in `seq -f "test%02g" 1 30`
do
	if ! id -u $u &>/dev/null
           useradd $u
	   echo "Haha123123" | passwd --stdin $u
	fi
done

将之前使用 for 循环语句创建的 test01 - test30 用户删除

[root@openEuler ~]# cat until2.sh 
#!/bin/bash

i=1
until [ $i -gt 30 ]
do
	# define accout
	if [ $i -lt 10 ] ; then
		user=test0$i
	else
		user=test$i
	fi
	# condition accout
	if id -u $user &>/dev/null ; then
		userdel -r $user
	else
		echo "$user is not exits."
	fi
	i=$((i+1))
	#let i++
done
[root@openEuler ~]# bash until2.sh 
[root@openEuler ~]# grep test /etc/passwd

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xialliy

你的鼓励是我最大的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值