1、腳本練習(xí)
- 編寫腳本/root/bin/sumid.sh褂策,計(jì)算/etc/passwd文件中的第10個(gè)用戶和第20用戶的ID之和
[root@centos7 7.31]#cat sumid.sh
#!/bin/bash
##################################
#Filename:sumid.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
id10=`cat /etc/passwd |head -n10|tail -n1|cut -d: -f3`
id20=`cat /etc/passwd|head -n20|tail -n1|cut -d: -f3`
let c=id10+id20
echo "sumid: $c"
- 編寫腳本/root/bin/sumspace.sh犹撒,傳遞兩個(gè)文件路徑作為參數(shù)給腳本折联,計(jì)算這兩個(gè)文件中所有空白行之和
[root@centos7 7.31]#cat sumspace.sh
#!/bin/bash
##################################
#Filename:sumspace.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
space1=`cat $1|egrep "^[[:space:]]+$" |wc -l`
space2=`cat $2|egrep "^[[:space:]]+$"|wc -l`
let sumspace=space1+space2
echo "sumspace=$sumspace
- 編寫腳本/root/bin/sumfile.sh,統(tǒng)計(jì)/etc, /var, /usr目錄中共有多少個(gè)一級(jí)子目錄和文件
[root@centos7 test]#cat sumfile.sh
#!/bin/bash
##################################
#Filename:sumfile.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
sumfile1=`ls -d /etc/*|wc -l`
sumfile2=`ls -d /var/* |wc -l`
sumfile3=`ls -d /usr/*|wc -l`
let sumfile=sumfile1+sumfile2+sumfile3
echo "sumfile=$sumfile"
- 編寫腳本/root/bin/argsnum.sh,接受一個(gè)文件路徑作為參數(shù)识颊;如果參數(shù)個(gè)數(shù)小于1诚镰,則提示用戶“至少應(yīng)該給一個(gè)參數(shù)”,并立即退出祥款;如果參數(shù)個(gè)數(shù)不小于1清笨,則顯示第一個(gè)參數(shù)所指向的文件中的空白行數(shù)
[root@centos7 test]#cat argsnum.sh
#!/bin/bash
##################################
#Filename:argsnum.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
[ $# -lt 1 ]&&echo one arg is need&&exit 100
[ $# -ge 1 ]&&echo "`cat $1|egrep ^[[:space:]]+$|wc -l`"
- 編寫腳本/root/bin/hostping.sh,接受一個(gè)主機(jī)的IPv4地址做為參數(shù)刃跛,測(cè)試是否可連通函筋。如果能ping通,則提示用戶“該IP地址可訪問(wèn)”奠伪;如果不可ping通,則提示用戶“該IP地址不可訪問(wèn)”
[root@centos7 test]#cat hostping.sh
#!/bin/bash
##################################
#Filename:hostping.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
ping -c1 -w1 $1 && echo "the IP can access"||echo "the IP can't access"
set --
- 編寫腳本/root/bin/checkdisk.sh首懈,檢查磁盤分區(qū)空間和inode使用率绊率,如果超過(guò)80%,就發(fā)廣播警告空間將滿
[root@centos7 test]#cat checkdisk.sh
#!/bin/bash
##################################
#Filename:checkdisk.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
diskused=`df|egrep "/dev/sd"|tr -s " " "%"|cut -d% -f5|sort -nr|head -n1`
inodeused=`df -i|egrep "/dev/sd"|tr -s " " " "|cut -d% -f5|sort -nr|head -n1`
[ $diskused -ge 80 -o $inodeused -ge 80 ]&&wall space will full
- 編寫腳本/bin/per.sh,判斷當(dāng)前用戶對(duì)指定的參數(shù)文件究履,是否不可讀并且不可寫
[root@centos7 test]#cat per.sh
#!/bin/bash
##################################
#Filename:per.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
[ ! -r $1 -a ! -w $1 ] && echo "yes"
set --
- 編寫腳本/root/bin/excute.sh 滤否,判斷參數(shù)文件是否為sh后綴的普通文件,如果是最仑,添加所有人可執(zhí)行權(quán)限藐俺,否則提示用戶非腳本文件
[root@centos7 test]#cat excute.sh
#!/bin/bash
##################################
#Filename:excute.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
[[ "$1" =~ .*sh$ ]]&&[ -f "$1" ]&&chmod a+x $1||echo "this is not script"
set --
- 編寫腳本/root/bin/nologin.sh和login.sh,實(shí)現(xiàn)禁止和充許普通用戶登錄系統(tǒng)
[root@centos7 test]#cat nologin.sh
#!/bin/bash
##################################
#Filename:nologin.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
id=`id -u $1`
[ "$id" -ge 1000 ]&& { passwd -l $1 &> /dev/null;echo $1 is louck; }
unset id;set --
[root@centos7 test]#cat login.sh
#!/bin/bash
##################################
#Filename:login.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
id=`id -u $1`
[ $id -ge 1000 ]&& { passwd -u $1 &> /dev/null;echo "$1 can login"; }
unset id ;set --
- 讓所有用戶的PATH環(huán)境變量的值多出一個(gè)路徑,例如:/usr/local/apache/bin
[root@centos7 7.31]# cat path.sh
#!/bin/bash
##################################
#Filename:path.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
echo "PATH=$PATH:/app/test/7.31">>/etc/profile.d/path.sh
- 用戶root登錄時(shí)泥彤,將命令指示符變成紅色欲芹,并自動(dòng)啟用如下別名:rm=‘rm –i’
cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0’
editnet=‘vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 或ifcfg-ens33 ’ (如果系統(tǒng)是CentOS7)
[root@centos7 test]#cat alias.sh
#!/bin/bash
##################################
#Filename:alias.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
cat>>/root/.bashrc<<end
PS1="\[\e[31m\][\u@\h \W]\\$\[\e[0m\]"
alias cdnet=‘cd /etc/sysconfig/network-scripts/’
editnet=‘vim /etc/sysconfig/network-scripts/fcfg-ens33’
end
- 任意用戶登錄系統(tǒng)時(shí),顯示紅色字體的警示提醒信息“Hi,dangerous吟吝!”
[root@centos7 test]#vim motd.sh
1 #!/bin/bash
2 ##################################
3 #Filename:motd.sh
4 #Description:
5 #Date:2017-08-01
6 #Author:zhangdazhi
7 #Version:2.0
8 #####################################
9 echo "^[[0;31m Hi,dangerous!^[[0m">/etc/motd
- 編寫生成腳本基本格式的腳本菱父,包括作者,聯(lián)系方式剑逃,版本浙宜,時(shí)間,描述等
[root@centos7 test]#cat creatscript.sh
#!/bin/bash
##################################
#Filename:creatscript.sh
#Description:
#Date:2017-7-28
#Author:zhangdazhi
#Version:2.0
#####################################
[ $# -gt 1 ]&&echo "arg must be one"&&exit 100
[ $# -eq 0 ]&&read -p "please input scriptname: " filename
[ $# -eq 1 ]&&filename=$1
[ -a "$filename" ]&&echo "$filename is exit"&&exit 101
touch $filename
chmod a+x $filename
cat>>$filename<<end
#!/bin/bash
##################################
#Filename:`basename $filename`
#Description:
#Date:`date +%F`
#Author:zhangdazhi
#Version:2.0
#####################################
end
vim + $filename
set --
- 編寫用戶的環(huán)境初始化腳本reset.sh蛹磺,包括別名粟瞬,登錄提示符,vim的設(shè)置萤捆,環(huán)境變量等
[root@centos7 test]#cat reset.sh
#!/bin/bash
##################################
#Filename:reset.sh
#Description:
#Date:2017-08-01
#Author:zhangdazhi
#Version:2.0
#####################################
cat>>/root/.bashrc<<end
alias cdnet=‘cd /etc/sysconfig/network-scripts/’
alias editnet=‘vim /etc/sysconfig/network-scripts/fcfg-ens33’
alias cdpack='cd /run/media/root/CentOS\ 7\ x86_64/Packages/'
alias p='poweroff'
alias egrep='egrep --color=auto'
end
cat>>/root/.bash_profile<<end
PATH=$PATH:/app
PS1="\[\e[31m\][\u@\h \W]\\$\[\e[0m\]"
end
echo "set nu">>.vimrc
name=$1
id $name &> /dev/null &&{ echo $name is exist;exit 100; }||{ useradd $name;echo $name is created;echo magedu|passwd --stdin $name; }
echo script is finished
unset name;set --
2裙品、文件查找和壓縮
- 查找/var目錄下屬主為root俗批,且屬組為mail的所有文件
[root@centos7 ~]#find /var -user root -group mail
/var/spool/mail
/var/spool/mail/root
- 查找/var目錄下不屬于root、lp清酥、gdm的所有文件
find /var ! -user root ! -user lp ! -user gdm -ls
- 查找/var目錄下最近一周內(nèi)其內(nèi)容修改過(guò)扶镀,同時(shí)屬主不為root,也不是postfix的文件
find /var -mtime -7 ! -user root ! -user postfix -ls
- 查找當(dāng)前系統(tǒng)上沒(méi)有屬主或?qū)俳M焰轻,且最近一個(gè)周內(nèi)曾被訪問(wèn)過(guò)的文件
find / \( -nouser -o -nogroup -a -atime -7 \) -ls
- 查找/etc目錄下大于1M且類型為普通文件的所有文件
find /etc -size +1M -type f -ls
- 查找/etc目錄下所有用戶都沒(méi)有寫權(quán)限的文件
find /etc ! -perm /222 -ls
- 查找/etc目錄下至少有一類用戶沒(méi)有執(zhí)行權(quán)限的文件
find /etc ! -perm -111 -ls
- 查找/etc/init.d目錄下臭觉,所有用戶都有執(zhí)行權(quán)限,且其它用戶有寫權(quán)限的文件
find /etc/init.d -perm -113