[root@centos7 script]#vim user.sh#!/bin/bashuseraddtestecho -e "\033[1;31mUser test is creted\033[0"echo 123456 |passwd --stdin test&>/dev/null
echo -e "\033[1;31mPasswd:123456\033[0m"passwd -e test[root@centos7 script]#bash user.sh
User test is creted
Passwd:123456
Expiring password for user test.
passwd: Success
判断用户是否存在,不存在时创建用户
[root@centos7 script]#vim user2.sh #!/bin/bash[ $# -eq 0 ] && { echo "Usage:`basename $0` USERNAME";exit 10; }id$1&>/dev/null &&{echo"User $1 is exist";exit 20;}useradd$1&>/dev/null &&{echo"$1 is created";echo 123456|passwd --stdin $1&>/dev/null;}||{echo"Error!";exit 30;}[root@centos7 script]#bash user2.sh
Usage:user2.sh USERNAME
[root@centos7 script]#echo $?
10
[root@centos7 script]#bash user2.sh mike
mike is created
[root@centos7 script]#echo $?
0
[root@centos7 script]#bash user2.sh root
User root is exist
[root@centos7 script]#echo $?
20
批量创建用户,设置随机初始密码,第一次登录时提示修改密码
[root@centos7 scripts]#vim for_useradd.sh #!/bin/bashfor i in{1..4};douseradd user$i
password=`tr -dc '[:alnum:]'</dev/urandom |head -c 8`echo user$i:$password|tee -a pass.txt
echo$password|passwd --stdin -e user$i&>/dev/null
echo"User:user$i is created!"done[root@centos7 scripts]#vim for_useradd2.sh #!/bin/bashfor i in{5..7};douseradd user$i
password=`tr -dc '[:alnum:]'< /dev/urandom|head -c 8`echo user$i:$password|tee -a pass.txt |chpasswd
passwd -e user$iecho"user:user$i is created!"done[root@centos7 scripts]#bash for_useradd.sh
user1:1LHAAMAs
User:user1 is created!
user2:Rl6uniGy
User:user2 is created!
user3:7myr8Qcj
User:user3 is created!
user4:SuQignJj
User:user4 is created!
user5:CnvtjlMl
[root@centos7 scripts]#bash for_useradd2.sh
Expiring password for user user5.
passwd: Success
user:user15 is created!
Expiring password for user user6.
passwd: Success
user:user16 is created!
Expiring password for user user7.
passwd: Success
user:user17 is created![root@centos7 scripts]#cat pass.txt
user1:1LHAAMAs
user2:Rl6uniGy
user3:7myr8Qcj
user4:SuQignJj
user5:CnvtjlMl
user6:mVclucjg
user7:h36FSWKN
批量创建不规则用户
[root@centos7 scripts]#vim userlist.txt
xiaohong
xiaoming
lisa
[root@centos7 scripts]#vim for_useradd3.shfor user in`cat userlist.txt`;douseradd$userdone[root@centos7 scripts]#bash for_useradd3.sh [root@centos7 scripts]#getent passwd
xiaohong:x:1038:1038::/home/xiaohong:/bin/bash
xiaoming:x:1039:1039::/home/xiaoming:/bin/bash
lisa:x:1040:1040::/home/lisa:/bin/bash
[root@centos7 scripts]#vim shift_user.sh#!/bin/bash#while [ $# -gt 0 ];dowhile["$1"];douseradd$1echo user:$1 is created
shiftdoneecho finish
[root@centos7 scripts]#bash shift_user.sh ddd ff ss
user:ddd is created
user:ff is created
user:ss is created
finish
[root@centos7 scripts]#getent passwd
ddd:x:1041:1041::/home/ddd:/bin/bash
ff:x:1042:1042::/home/ff:/bin/bash
ss:x:1043:1043::/home/ss:/bin/bash
2、查询本机系统信息
[root@centos7 script]#vim sysinfo.sh #!/bin/bash
COLOR="\033[31m"
ENDCOLOR="\033[0m"echo -e "Os_Version is $COLOR`cat /etc/redhat-release`$ENDCOLOR"echo -e "Kenel_Version is $COLOR`uname -r`$ENDCOLOR"echo -e "CPU_Type is $COLOR`lscpu|grep'Model name'|cut -d: -f2|tr -s ' '`$ENDCOLOR"echo -e "Disk_Space is $COLOR`free -h|grep Mem|tr -s ' '|cut -d' ' -f2`$ENDCOLOR"echo -e "Ipaddr is $COLOR`ifconfig ens33|egrep -o '[0-9.]{7,15}'|head -1`$ENDCOLOR"
unset COLOR ENDCOLOR
[root@centos7 script]#bash sysinfo.sh
Os_Version is CentOS Linux release 7.7.1908 (Core)
Kenel_Version is 3.10.0-1062.el7.x86_64
CPU_Type is Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
Disk_Space is 1.8G
Ipaddr is 192.168.37.112
[root@centos7 script]#vim arg.sh #!/bin/bashecho"1st arg is $1"echo"2st org is $2"echo"3st org is $3"echo"10st org is ${10}"echo"11st org is ${11}"echo"all org is $*"echo"all org is $@"echo"arg number is $#"echo"the script is `basename $0`"[root@centos7 script]#bash arg.sh {a..z}
1st arg is a
2st org is b
3st org is c
10st org is j
11st org is k
all org is a b c d e f g h i j k l m n o p q r s t u v w x y z
all org is a b c d e f g h i j k l m n o p q r s t u v w x y z
arg number is 26
the script is arg.sh
5、位置变量中 $*与$@的区别
[root@centos7 script]#vim son.sh#!/bin/bashecho"1st arg is $1"[root@centos7 script]#vim father.sh#!/bin/bashecho"all arg is $*"
./son.sh "$*"[root@centos7 script]#vim father2.sh#!/bin/bashecho"all arg is $*"
./son.sh "$@"[root@centos7 script]#chmod +x son.sh[root@centos7 script]#bash father.sh {1..10}
all arg is 1 2 3 4 5 6 7 8 9 10
1st arg is 1 2 3 4 5 6 7 8 9 10
[root@centos7 script]#bash father2.sh {1..10}
all arg is 1 2 3 4 5 6 7 8 9 10
1st arg is 1
[root@centos7 script]#vim rm.sh#!/bin/bash
DestinationDir='/tmp'
CurrentTime=`date +%F_%s`mkdir -p $DestinationDir/$CurrentTime/
mv$*$DestinationDir/$CurrentTime/
echo"move complete!you will find the file that is deleted in $DestinationDir/$CurrentTime/"[root@centos7 script]#alias rm='/data/script/rm.sh' [root@centos7 script]#touch f1.txt[root@centos7 script]#rm -f f1.txt
move complete!you will find the file that is deleted in /tmp/2020-04-03_1585902922/
[root@centos7 script]#ll /tmp/2020-04-03_1585902922/
total 0
-rw-r--r-- 1 root root 0 Apr 3 16:35 f1.txt
8、磁盘分区利用率大于80%时报警
[root@centos7 script]#vim disk.sh
used=`df|grep'/dev/sd'|tr -s ' ' %|cut -d% -f5|sort -nr|head -1`test$used -ge 80 &&echo"disk used > 80%"||echo"disk is normal"[root@centos7 script]#bash disk.sh
disk is normal
[root@centos7 scripts]#vim while_read_disk.sh#!/bin/bash
WARNING=80
df|sed -rn '/^\/dev\/sd/s#^([^[:space:]]+).*([[:digit:]]+)%.*$#\1 \2#p'|whileread part use;doif[$use -gt $WARNING];thenecho$part will be full:$useelseecho disk is normal
fidone[root@centos7 scripts]#bash while_read_disk.sh
disk is normal
9、read小案例
[root@centos7 script]#vim read.sh#!/bin/bashecho -e "Please input your name:\c" NAME
read NAME
echo"your name is $NAME"[root@centos7 script]#vim read2.sh#!/bin/bashread -p "Please input your name:" NAME
echo"your name is $NAME"[root@centos7 script]#bash read.sh
Please input your name:wanglu
your name is wanglu
[root@centos7 script]#bash read2.sh
Please input your name:wanglu
your name is wanglu
[root@centos7 script]#vim yesorno.sh#!/bin/bashread -p "Will you marry me?(yes or no:)" value
value=`echo $value|tr'A-Z' a-z''`[["$value"=~ ^(y|yes)$ ]]&&echo"thank you!"&&exit 0
[["$value"=~ ^(n|no)$ ]]&&echo"go out!"||echo"please execute again,and input yes or no!"[root@centos7 script]#bash yesorno.sh
Will you marry me?(yes or no:)y
thank you![root@centos7 script]#bash yesorno.sh
Will you marry me?(yes or no:)No
go out![root@centos7 script]#bash yesorno.sh
Will you marry me?(yes or no:)nihao
please execute again,and input yes or no!
用case实现
[root@centos7 script]#vim yesorno2.sh#!/bin/bashread -p "Do you agree?(yes or no:)" ANSWER
case$ANSWERin[Yy]|[Yy][Ee][Ss])echo"thank you!";;[Nn]|[Nn][Oo])echo"go out!";;
*)echo"input false!"
esac
[root@centos7 script]#bash yesorno2.sh
Do you agree?(yes or no:)Yes
thank you![root@centos7 script]#bash yesorno2.sh
Do you agree?(yes or no:)n
go out![root@centos7 script]#bash yesorno2.sh
Do you agree?(yes or no:)yes i do
input false!
12、用 if 判断年龄
[root@centos7 script]#vim age.sh #!/bin/bashread -p "input your age:" AGE
if[[!"$AGE"=~ ^[0-9]+$ ]];thenecho"please input a digit age"elif[$AGE -lt 18 ];thenecho"good good study!"elif[$AGE -le 60 ];thenecho"good good work!"elif[$AGE -le 120 ];thenecho"enjoy life!"elseecho"welcome to the earth!"fi[root@centos7 script]#bash age.sh
input your age:ipipjxs
please input a digit age
[root@centos7 script]#bash age.sh
input your age:4
good good study![root@centos7 script]#bash age.sh
input your age:24
good good work![root@centos7 script]#bash age.sh
input your age:120
enjoy life![root@centos7 script]#bash age.sh
input your age:230
welcome to the earth!
1、创建用户2、查询本机系统信息3、每天将/etc/目录备份到/data/etc下4、位置变量及特殊变量5、位置变量中 `$*`与`$@`的区别6、实现远程传输7、用一个脚本代替rm8、磁盘分区利用率大于80%时报警9、read小案例10、算术题:鸡兔回笼11、请选择yes或no12、用 if 判断年龄