1.循环控制命令 shift

shift [n] 用于将参量列表 list 左移指定次数,缺省为左移一次。参量列表 list 一旦被移动,最左端的那个参数就从列表中删除。while 循环遍历位置参量列表时,常用到 shift

[root@c7-147 ~]#help shift 
shift: shift [n]
Shift positional parameters.
Rename the positional parameters $N+1,$N+2 ... to $1,$2 ... If N is
not given, it is assumed to be 1.
Exit Status:
Returns success unless N is negative or greater than $#.

shift 创建用户账号

[root@c7-147 ~]#help test
test: test [expr]
-z STRING True if string is empty.
-n STRING
STRING True if string is not empty.

#创建用户

#yyds干货盘点#shell进阶之shift与select相关技术_用户账号

2. sed取IP

#yyds干货盘点#shell进阶之shift与select相关技术_创建用户_02

#yyds干货盘点#shell进阶之shift与select相关技术_创建用户_03

#yyds干货盘点#shell进阶之shift与select相关技术_用户账号_04

#yyds干货盘点#shell进阶之shift与select相关技术_创建用户_05

排序统计次数

#yyds干货盘点#shell进阶之shift与select相关技术_创建用户_06

3. while read 特殊用法

while 循环的特殊用法,遍历文件或文本的每一行

while read line; do
循环体
done < /PATH/FROM/SOMEFILE

说明:依次读取/PATH/FROM/SOMEFILE文件中的每一行,且将行赋值给变量

#yyds干货盘点#shell进阶之shift与select相关技术_创建用户_07

[root@c7-147 ~]#echo magedu | read X ; echo $X 打印空内容,因为开启了子进程

[root@c7-147 ~]#echo magedu | while read X ; do echo $X;done
magedu
[root@c7-147 ~]#echo magedu | { read X ; echo $X; }
magedu
[root@c7-147 ~]#echo magedu | ( read X ; echo $X )
magedu
[root@c7-147 ~]#echo mage wang zhang | ( read X Y Z; echo $X $Y $Z )
mage wang zhang
[root@c7-147 ~]#echo mage wang zhang | while read X Y Z; do echo $X $Y $Z;done
mage wang zhang

磁盘检查

[root@c7-147 ~]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 18307072 3014452 15292620 17% /
devtmpfs 923900 0 923900 0% /dev
tmpfs 934344 0 934344 0% /dev/shm
tmpfs 934344 8844 925500 1% /run
tmpfs 934344 0 934344 0% /sys/fs/cgroup
/dev/sda1 508588 127464 381124 26% /boot
tmpfs 186872 0 186872 0% /run/user/0
[root@c7-147 ~]#df |sed -nr "/^\/dev\/sd/s#^([^ ]+) .* ([0-9]+)%.*#\1 \2#p"
/dev/sda1 26

[root@c7-147 ~]#cat while_read_diskcheck.sh
#!/bin/bash
WARNING=80
MAIL=1722525928@qq.com
df |sed -nr "/^\/dev\/sd/s#^([^ ]+) .* ([0-9]+)%.*#\1 \2#p"|while read DEVICE USE;do
if [ $USE -gt $WARNING ] ;then
echo "$DEVICE will be full,use:$USE" | mail -s "DISK WARNING" $MAIL
fi
done

4.访问日志分析

#yyds干货盘点#shell进阶之shift与select相关技术_用户账号_08

得到统计数和IP ,当统计数大于100就设置防火墙

#yyds干货盘点#shell进阶之shift与select相关技术_用户账号_09


5. lastb

#yyds干货盘点#shell进阶之shift与select相关技术_创建用户_10


6 select 循环与菜单

格式:

select NAME [in WORDS ... ;] do COMMANDS; done

select variable in list ;do

循环体命令

done

[root@c7-147 ~]#help select
select: select NAME [in WORDS ... ;] do COMMANDS; done
Select words from a list and execute commands.

The WORDS are expanded, generating a list of words. The
set of expanded words is printed on the standard error, each
preceded by a number. If `in WORDS' is not present, `in "$@"'
is assumed. The PS3 prompt is then displayed and a line read
from the standard input. If the line consists of the number
corresponding to one of the displayed words, then NAME is set
to that word. If the line is empty, WORDS and the prompt are
redisplayed. If EOF is read, the command completes. Any other
value read causes NAME to be set to null. The line read is saved
in the variable REPLY. COMMANDS are executed after each selection
until a break command is executed.

Exit Status:
Returns the status of the last command executed.

说明:

  select 循环主要用于创建菜单,按数字顺序排列的菜单项显示在标准错误上,并显示 PS3 提示符,等待用户输入

用户输入菜单列表中的某个数字,执行相应的命令

 用户输入被保存在内置变量 REPLY 中

 select 是个无限循环,因此要用 break 命令退出循环,或用 exit 命令终止脚本。也可以按 ctrl+c 退出循环

select 经常和 case 联合使用

与 for 循环类似,可以省略 in list,此时使用位置参量

#自动生成菜单
[root@c7-147 ~]#select MENU in 北京烤鸭 F跳墙 小龙虾 羊蝎子 火锅 点菜结束; do echo $MEMU;done
1) 北京烤鸭
2) 跳墙
3) 小龙虾
4) 羊蝎子
5) 火锅
6) 点菜结束
#?

[root@c7-147 ~]#cat select.sh

#!/bin/bash
sum=0
PS3="请点菜(1-6): "
select MENU in 北京烤鸭 F跳墙 小龙虾 羊蝎子 火锅 点菜结束;do case $REPLY in
1)
echo $MENU 价格是 100
let sum+=100
;;
2)
echo $MENU 价格是 88
let sum+=88
;;
3)
echo $MENU价格是 66
let sum+=66
;;
4)
echo $MENU 价格是 166
let sum+=166
;;
5)
echo $MENU 价格是 200
let sum+=200
;;
6)
echo "点菜结束,退出"
break
;;
*)
echo "点菜错误,重新选择"
;;
esac
done
echo "总价格是: $sum"

#yyds干货盘点#shell进阶之shift与select相关技术_创建用户_11