linux编写复制脚本程,常用的Shell脚本
1、通過(guò)位置變量創(chuàng)建linux系統(tǒng)賬戶及密碼
$1 是執(zhí)行腳本的第一個(gè)參數(shù),$2 是執(zhí)行腳本的第二個(gè)參數(shù)
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 19:19:45
5 #Name: userad.sh
6 #Version: v1.0
7 #Description: This is a Script.
8
9 `useradd $1`10 echo "$2" |passwd --stdin "$1"
2、每周5使用tar命令備份/var/log 下的所有日志文件,防止文件覆蓋
vim backup.sh
tar zcfP/tmp/varlog_$(date +%F).tar.gz /var/log
crontab-e* * * * 5 bin/bash /server/scripts/backup.sh
3、實(shí)時(shí)監(jiān)控本機(jī)內(nèi)存和硬盤(pán)剩余空間,剩余內(nèi)存小于500M,根分區(qū)剩余空間小于1000M的時(shí)候發(fā)送報(bào)警郵件
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 20:29:30
5 #Name: jiankong.sh
6 #Version: v1.0
7 #Description: This is a Script.
8
9 free=`free -m|awk 'NR==3 {print$4 }'`10 df=`df |awk 'NR==2{print $4}'`11
12 if [[ $free -lt 500 && $df -lt 1000000]];then13 mail -s "Warning~" 1227566276@qq.com14 echo 'send success!'
15 else
16 echo 'zhengchang!'
17exit 018 fi
4、腳本生成一個(gè) 100 以內(nèi)的隨機(jī)數(shù)值,提示用戶猜數(shù)字,更具用戶的輸入,提示用戶猜對(duì)了,猜打了或猜小了,直至用戶猜對(duì)腳本結(jié)束。
5、檢測(cè)本機(jī)當(dāng)前用戶是否為超級(jí)管理員,如果是管理員,使用 yum 安裝 vsftpd,如果不是,則提示您非管理員,使用字串對(duì)比版本
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 20:43:03
5 #Name: root.sh
6 #Version: v1.0
7 #Description: This is a Script.
8
9 root=`id -u`10 if [ $root -eq 0 ];then11 yum install -y vsftpd12 else
13 echo 'no super user~'
14 fi
6、依次提示用戶輸入 3 個(gè)整數(shù),腳本根據(jù)數(shù)字大小依次排序輸出 3 個(gè)數(shù)字
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 20:45:50
5 #Name: 3geshu.sh
6 #Version: v1.0
7 #Description: This is a Script.
8 read -p "first:"a9 read -p "second:"b10 read -p "third:"c11
12 if [[ -z $a || -z $b || -z $c ]];then13 echo "please input num---"
14 exit 1
15fi16
17 if [[ -n "$(echo $a| sed -n"/^[0-9]\+$/p")" && -n "$(echo $b| sed -n"/^[0-9]\+$/p")" && -n "$(echo $c| sed -n"/^[0-9]\+$/p")"]];then18
19 if [ $a -lt $b ];then20 t=$a;a=$b;b=$t;21fi22 if [ $a -lt $c ];then23 t=$a;a=$c;c=$t;24fi25 if [ $b -lt $c ];then26 t=$b;b=$c;c=$t;27fi28 echo "big --- small:$a,$b,$c"
29
30 else
31 echo "dont abcd...."
32 fi
7、編寫(xiě)腳本,實(shí)現(xiàn)人機(jī)《石頭,剪刀,布》游戲
8、編寫(xiě)腳本測(cè)試192.168.4.0/24整個(gè)網(wǎng)段中那些主機(jī)處于開(kāi)機(jī)狀態(tài),那些主機(jī)處于關(guān)機(jī)狀態(tài)(for 版本)
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 21:13:05
5 #Name: ping1.sh
6 #Version: v1.0
7 #Description: This is a Script.
8 for i in {1..254}9do10 ping 192.168.4.$i -c 2 -w 1 2&>1 >/dev/null11 if [ $? -eq 0 ];then12 echo "192.168.4.$i active!!!"
13 else
14 echo "192.168.4.$i down..."
15fi16 done
9、編寫(xiě)腳本測(cè)試 192.168.4.0/24 整個(gè)網(wǎng)段中那些主機(jī)處于開(kāi)機(jī)狀態(tài),那些主機(jī)處于關(guān)機(jī)狀態(tài)(多進(jìn)程版),定義一個(gè)函數(shù)來(lái)實(shí)現(xiàn),ping 某一臺(tái)主機(jī),并檢測(cè)主機(jī)的存貨狀態(tài)
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 21:18:43
5 #Name: ping2.sh
6 #Version: v1.0
7 #Description: This is a Script.
8fun_ping(){9
10 ping 192.168.4.$i -c 1 -w 1 2&>1 >/dev/null11 if [ $? -eq 0 ]12then13 echo "192.168.4.$i active!"
14 else
15 echo "192.168.4.$i down."
16fi17}18 for i in {1..254}19do20fun_ping21 done
10、編寫(xiě)腳本,復(fù)制文件的時(shí)候,顯示進(jìn)度條
11、9*9 乘法表(編寫(xiě) shell 腳本,打印 9*9 乘法表)
12、使用死循環(huán)實(shí)時(shí)顯示 eth0 網(wǎng)卡發(fā)送的數(shù)據(jù)包流量
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 21:50:32
5 #Name: eth4.sh
6 #Version: v1.0
7 #Description: This is a Script.
8 while:9do10 ifconfig eth4 |sed -n '5,6p'
11 done
13、使用user.txt 文件中的人員名單,在計(jì)算機(jī)中自動(dòng)創(chuàng)建對(duì)應(yīng)的賬戶并配置初始密碼
腳本執(zhí)行,需要提前準(zhǔn)備一個(gè) user.txt 文件,該文件中包含有若干用戶信息
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 21:27:13
5 #Name: zidonguser.sh
6 #Version: v1.0
7 #Description: This is a Script.
8 user=`cat /server/scripts/user.txt`9 for i in$user10do11useradd $i12 echo "123456" |passwd --stdin "$i"
13 done
14、編寫(xiě)批量修改擴(kuò)展名腳本,如批量將 txt 文件修改為doc文件
執(zhí)行腳本時(shí),需要給腳本添加位置參數(shù)
腳本名 txt doc (可以將 txt 的擴(kuò)展名修改為doc)
腳本名 doc jpg(可以將doc 的擴(kuò)展名修改為jpg)
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 18:58:14
5 #Name: rename.sh
6 #Version: v1.0
7 #Description: This is a Script.
8 read -p "input weizhi"$wz9 for i in`ls $wz`10do11 mv $i `echo $i|sed s#txt#doc#g`
12
13 done
15、查看有多少遠(yuǎn)程的 IP 在連接本機(jī)(不管是通過(guò) ssh 還是 web 還是 ftp 都統(tǒng)計(jì))
使用 netstat -atn 可以查看本機(jī)所有連接狀態(tài),-a 查看所有,
-t 僅顯示tcp連接信息, -n數(shù)字格式顯示
local address(第四列是本機(jī)的IP和端口信息)
Foreign address(第五烈是遠(yuǎn)程主機(jī)的IP和端口信息)
使用awk命令僅顯示第5列數(shù)據(jù),再顯示第1列IP地址的信息
sort 可以按數(shù)字的大小排序,最后使用 Uniq 將多余重復(fù)的刪除,并統(tǒng)計(jì)重復(fù)次數(shù)
#netstat -atn | awk -F"[: ]+" '{print $5}' 端口號(hào)
netstat -atn |egrep ":80|:22|:21" | awk '{print $5}' |awk -F ":" '{print $1}' |sort -rn |uniq -c
16、對(duì)100 以內(nèi)的所有正整數(shù)相加求和(1+2+3.。。+100)
eq 100 可以快速自動(dòng)生成100個(gè)整數(shù)
1 #!/bin/bash
2 #Author: Peter zh
3 #Blog: https://home.cnblogs.com/zhangwduoduoj/
4 #Time: 2019-08-17 21:10:05
5 #Name: qiuhe.sh
6 #Version: v1.0
7 #Description: This is a Script.
8
9 sum=10 for i in `seq 100`11do12 let sum+=i13done14echo $sum15
總結(jié)
以上是生活随笔為你收集整理的linux编写复制脚本程,常用的Shell脚本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: AndroidStudio 3.4更新了
- 下一篇: windows的\r\n与Linux的\