shell脚本案例-基础

5 篇文章 0 订阅
1、创建用户
创建某用户,设置初始密码,第一次登录时提示修改密码
[root@centos7 script]#vim user.sh

#!/bin/bash
useradd test
echo -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/bash
for i in {1..4};do
        useradd 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/bash
for i in {5..7};do                                                                        
        useradd user$i
        password=`tr -dc '[:alnum:]' < /dev/urandom|head -c 8`
        echo user$i:$password |tee -a pass.txt |chpasswd
        passwd -e user$i
        echo "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.sh

for user in `cat userlist.txt`;do
        useradd $user
done    
[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 ];do
while [ "$1" ];do
        useradd $1
        echo user:$1 is created
        shift
done
echo 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

3、每天将/etc/目录备份到/data/etc下(手动)
[root@centos7 script]#vim backup.sh 

#!/bin/bash
echo -e "\033[31mstart backup!\033[0m"
sleep 2
cp -a /etc /data/etc`date +%F`
echo -e "\033[31mbackup is finished!\033[0m" 

[root@centos7 script]#bash backup.sh 
start backup!
backup is finished!
[root@centos7 script]#ll /data
total 12
drwxr-xr-x. 146 root root 8192 Apr  3 14:54 etc2020-04-03
drwxr-xr-x    2 root root   23 Apr  3 15:33 script
4、位置变量及特殊变量
[root@centos7 script]#vim arg.sh 

#!/bin/bash
echo "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/bash
echo "1st arg is $1"
[root@centos7 script]#vim father.sh

#!/bin/bash
echo "all arg is $*"
./son.sh "$*"
[root@centos7 script]#vim father2.sh

#!/bin/bash
echo "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
6、实现远程传输
[root@centos7 script]#vim scp.sh

#!/bin/bash
echo "开始上传"
scp $* wang@172.16.128.16:/data/script37/
echo "上传完成"  
[root@centos7 script]#bash scp.sh file.txt
开始上传
上传完成
7、用一个脚本代替rm
[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'|while read part use;do
        if [ $use -gt $WARNING ];then
                echo $part will be full:$use
        else
                echo disk is normal                                                            
        fi
done

[root@centos7 scripts]#bash while_read_disk.sh 
disk is normal
9、read小案例
[root@centos7 script]#vim read.sh

#!/bin/bash
echo -e "Please input your name:\c" NAME
read NAME
echo "your name is $NAME"  
[root@centos7 script]#vim read2.sh

#!/bin/bash
read -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
10、算术题:鸡兔回笼
[root@centos7 script]#vim chock-rabbit.sh 

#!/bin/bash
read -p "input head num:" head
read -p "input foot num:" foot
rabbit=$[ foot/2-head ]                                                                      
chook=$[ head-rabbit ]
echo "rabbit:$rabbit chook:$chook"

[root@centos7 script]#bash chock-rabbit.sh 
input head num:9
input foot num:20
rabbit:1 chook:8
11、请选择yes或no
用[[ ]]实现
[root@centos7 script]#vim yesorno.sh

#!/bin/bash
read -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/bash
read -p "Do you agree?(yes or no:)" ANSWER
case $ANSWER in
[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/bash
read -p "input your age:" AGE
if [[ ! "$AGE" =~ ^[0-9]+$ ]];then                                                             
        echo "please input a digit age"
elif [ $AGE -lt 18 ] ;then
        echo "good good study!"
elif [ $AGE -le 60 ];then
        echo "good good work!"
elif [ $AGE -le 120 ];then
        echo "enjoy life!"
else
        echo "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!
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值