SHELL腳本練習手冊
編寫腳本 sumid.sh,計算/etc/passwd?件中的第10個?戶和第20?戶的ID之和
#!/bin/bash
S_O=head /etc/passwd | tail -1 | cut -d: -f3
S_T=head -20 /etc/passwd | tail -1 | cut -d: -f3
echo -e "/etc/passwd 文件中的第10個和第20個用戶的ID之和為:\e[1;33m$[${S_O}+${S_T}]\e[0m"
編寫腳本 sumspace.sh,傳遞兩個?件路徑作為參數給腳本,計算這兩個?件中所有空??之和
#!/bin/bash
read -p "please input One path : " ONE_PATH
read -p "please input Two path : " TWO_PATH
S_ONE=grep "^$" ${ONE_PATH} |wc -l
S_TWO=grep "^$" ${TWO_PATH} |wc -l
echo "file spaces is : ${S_ONE} ${S_TWO}"
sum=$[${S_ONE}+${S_TWO}]
echo -e "${ONE_PATH}、${TWO_PATH} space sum is \e[31m${sum}\e[0m"
編寫腳本 sumfile.sh,統計/etc, /var, /usr?錄中共有多少個?級??錄和?件
#!/bin/bash
read -p "please input first_dir_path : " FIRST
read -p "please input second_dir_path : " SECOND
read -p "please input three_dir_path : " THREE
FIR_F=find ${FIRST} -maxdepth 1 | wc -l
SEC_F=find ${SECOND} -maxdepth 1 | wc -l
THR_F=find ${THREE} -maxdepth 1 | wc -l
FIR_S=find ${FIRST} -maxdepth 1 -type d | wc -l
SEC_S=find ${SECOND} -maxdepth 1 -type d | wc -l
THR_S=find ${THREE} -maxdepth 1 -type d | wc -l
fir_dir=$[${FIR_S}+${SEC_S}+${THR_S}-3]
echo -e "\e[1;33m${FIRST}、${SECOND}、${THREE}\e[0m three first dirs sum : \e[1;31m${fir_dir}\e[0m"
SUM=$[${FIR_F}+${SEC_F}+${THR_F}]
echo -e "\e[1;33m${FIRST}、${SECOND}、${THREE}\e[0m three files sum : \e[1;31m$[${SUM}-${fir_dir}]\e[0m"
編寫腳本 argsnum.sh,接受?個?件路徑作為參數;如果參數個數?於1,則提??戶“?少應該給?個參數”,並?即退出;如果參數個數不?於1,則顯?第?個參數所指向的?件中的空??數
#!/bin/bash
[[ $# -lt 1 ]] && (echo "at least one argument"&&exit)|| echo -e "\e[1;31m$1\e[0m 文件空白行數為:\e[1;33mgrep ‘^$‘ $1|wc -l
\e[0m"
編寫腳本 hostping.sh,接受?個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提??戶“該IP地址可訪問” ;如果不可ping通,則提??戶“該IP地址不可訪問”
#!/bin/bash
read -p "please input IP:" IP
LOG_COLOR=‘\e[1;31m‘
AND_COLOR=‘\e[0m‘
A=[[ "$IP" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]];echo $?;
B=ping -c1 -w1 $IP &>/dev/null;echo $?;
if [ $A -eq 0 ];then
if [ $B -eq 0 ];then
echo -e "${LOG_COLOR}該IP地址可以訪問${AND_COLOR}"
else
echo -e "${LOG_COLOR}該IP地址不可訪問${AND_COLOR}"
fi
else
echo -e "${LOG_COLOR}IP 格式輸入不正確${AND_COLOR}"
exit
fi
編寫腳本 checkdisk.sh,檢查磁盤分區空間和inode使?率,如果超過80%,就發?播警告空間將滿
#!/bin/bash
disk=df|egrep /dev/sd|tr -s ‘ ‘ ‘%‘|cut -d% -f5|sort -nr|head -n1
inode=df -i|egrep /dev/sd|tr -s ‘ ‘ ‘%‘|cut -d% -f5|sort -nr|head -n1
[ $disk -ge 80 -o $inode -ge 80 ] && wall space will full.
編寫腳本 per.sh,判斷當前?戶對指定參數?件,是否不可讀並且不可寫
#!/bin/bash
[ ! -r $1 -a ! -w $1 ] && echo "$1 is not read and write" || exit
編寫腳本 excute.sh ,判斷參數?件是否為sh後綴的普通?件,如果是,添加所有?可執?權限,否則提??戶?腳本?件
#!/bin/bash
[[ $1 =~ .sh$ ]] && [ -f $1 ] && (chmod a+x $1;echo "$1 is .sh")||echo "$1 is not
.sh"
編寫腳本 nologin.sh 和 login.sh,實現禁?和允許普通?戶登錄系統
#!/bin/bash
[ -f /etc/nologin ] && echo “nologin”|| (touch /etc/nologin;echo “nologin”)!/bin/bash
[ -f /etc/nologin ] && ( rm -f /etc/nologin;echo “login”)||echo “login”
編寫腳本 createuser.sh,實現如下功能:使??個?戶名做為參數,如果指定參數的?戶存在,就顯?其存在,否則添加之;顯?添加的?戶的id號等信息
#!/bin/bash
read -p "please input username:" user
id $user &>/dev/null
[[ ! $? -eq 0 ]] && (useradd $user &> /dev/null && echo "add $user user") ||echo "the user is exits"
編寫腳本 yesorno.sh,提??戶輸?yes或no,並判斷?戶輸?的是yes還是no,或是其它信息
#!/bin/bash
read -p "please input yes or no :" ANS
[[ "$ANS" =~ ^(Yy?)$ ]] && echo yes
[[ "$ANS" =~ ^(Nn?)$ ]] && echo no
編寫腳本 filetype.sh,判斷?戶輸??件路徑,顯?其?件類型(普通,?錄,鏈接,其它?件類型)
#!/bin/bash
read -p "please input file path: " PT
file $PT >/testdir/shell/test.txt
leixing=egrep -o "link|text|block|directory" /testdir/shell/test.txt
case $leixing in
text)
echo "the path is file"
;;
link)
echo "the path is Link"
;;
block)
echo "the path is Block"
;;
directory)
echo "the path is Directory"
;;
*)
echo "the path is Others"
esac
編寫腳本 checkint.sh,判斷?戶輸?的參數是否為正整數
#!/bin/bash
read?-p?"please?input?a?number:"?num
[[ $num =~ ^[0-9]+$ ]] && echo “$num is int”||{echo ”lease?input?a?number”;exit;}讓所有?戶的PATH環境變量的值多出?個路徑,例如:/usr/local/apache/bin
vim /etc/profile
export PATH=/usr/local/apache/bin:$PATH
source /etc/profile 生效
?戶root登錄時,將命令指?符變成紅?,並?動啟?如下別名:
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 ’ (如果系統是CentOS7)[root@CentOS ~]# vim ~/.bashrc
alias rm=‘rm –i‘
alias cdnet=‘cd /etc/sysconfig/network-scripts/‘
alias editnet1=‘vim /etc/sysconfig/network-scripts/ifcfg-eth0‘
alias editnet2=‘vim /etc/sysconfig/network-scripts/ifcfg-ens33‘
export PS1=‘[\e[1;31m][\u@\h \W]\$[\e[0m]‘ 紅色
source ~.bashrc 生效
任意?戶登錄系統時,顯?紅?字體的警?提醒信息“Hi,dangerous!”
[root@CentOS ~]# vim /etc/issue
^[[031m " Hi,dangerous!" ^[[0m"
編寫?成腳本基本格式的腳本,包括作者,聯系?式,版本,時間,描述等
[root@CentOS ~]# vim .vimrc
set nu
set ignorecase
set cursorline
set autoindent
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == ‘sh‘
call setline(1,"#!/bin/bash")
call setline(2,"#")
call
setline(3,"#****"
)
call setline(4,"#Author: w")
call setline(5,"#QQ: 1")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: http://www.magedu.com")
call setline(9,"#Description: The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights
reserved")
call
setline(11,"#****
")
call setline(12,"")
endif
endfunc
autocmd BufNewFile * normal G
編寫腳本 systeminfo.sh,顯?當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本, CPU型號,內存??,硬盤??
#!/bin/bash
LOG_COLOR=‘\e[1;33m‘
AND_COLOR=‘\e[0m‘
echo -e "當前主機信息如下:"
echo -e "主機名:${LOG_COLOR}${HOSTNAME}${AND_COLOR}"
echo -e "IPV4 地址為:${LOG_COLOR}ifconfig | grep broadcast | tr -s " " | cut -d" " -f3
"${AND_COLOR}
echo -e "操作系統版本為:${LOG_COLOR}uname -r
"${AND_COLOR}
echo -e "內核版本為:${LOG_COLOR}cat /etc/redhat-release | cut -d"." -f1-2
"${AND_COLOR}
echo -e "CPU型號為:${LOG_COLOR}lscpu | grep Model | tail -1 | tr -s ‘ ‘ | cut -d: -f2
"${AND_COLOR}
echo -e "內存大小為:${LOG_COLOR}free -mh | head -2 | tail -1 | tr -s ‘ ‘ | cut -d" " -f2
"${AND_COLOR}
echo -e "硬盤容量為:${LOG_COLOR}fdisk -l | head -2 | tail -1 | awk -F ‘:|,‘ ‘{print $2}‘
"${AND_COLOR}
編寫腳本 backup.sh,可實現每?將/etc/?錄備份到/root/etcYYYY-mm-dd中
#!/bin/bash
cp /etc /testdir/etcdate +%F
echo "/etc 備份完成"
編寫腳本 disk.sh,顯?當前硬盤分區中空間利?率最?的值
#!/bin/bash
echo "當前硬盤分區中空間利用率最大的值為:df |grep /dev/sd |grep -o "[0-9]\{1,3\}%" | sort -rn | head -1
"
編寫腳本 links.sh,顯?正連接本主機的每個遠程主機的IPv4地址和連接數,並按連接數從?到?排序
#!/bin/bash
echo "正在連接本主機的每個遠程主機的 IPV4 的地址和連接數為:"
echo "netstat -tan | tr -s " " : | cut -d: -f6 | grep ^[0-9] | sort | uniq -c
"
編寫腳本 reset.sh,對新系統進行環境配置
#!/bin/bash
#字體顏色
LOG_COLOR=‘\e[1;33m‘
AND_COLOR=‘\e[0m‘
#網卡名
N_N=ifconfig|head -1 | tr -s " " : |cut -d: -f1
#網卡配置文件所在目錄
D_NET="/etc/sysconfig/network-scripts/ifcfg-${N_N}"
#yum源配置文件所在目錄
D_YUM="/etc/yum.repos.d/"
#關閉seliunx
setenforce 0 &>/dev/null
sed -i ‘s#^SELINUX=.#SELINUX=disabled#g‘ /etc/selinux/config
echo -e "${LOG_COLOR}SELINUX已經修改完畢,重啟即生效。${AND_COLOR}"
read -p "即將修改主機名,請給主機起一個可愛的名字吧:" HOST
#更改主機名
hostname ${HOST}
cat >/etc/hostname <<EOF
${HOST}
EOF
echo -e "${LOG_COLOR}名ok,接下來是yum配置${AND_COLOR}"
#配置yum源
mkdir /media/cdrom -p &>/dev/null
mount /dev/sr0 /media/cdrom &>/dev/null
mkdir ${D_YUM}backup &>/dev/null
mv ${D_YUM}C ${D_YUM}backup
cat >>${D_YUM}CentOS.repo <<EOF
[CentOS]
name=CentOS
baseurl=file:///media/cdrom
gpgcheck=0
EOF
echo ‘/dev/sr0 /media/cdrom iso9660 defaults 0 0‘ >>/etc/fstab
yum clean all &>/dev/null
yum makecache &>/dev/null
echo -e "${LOG_COLOR}yum配置完成。${AND_COLOR}"
#關閉防火墻
systemctl stop firewalld.service &>/dev/null
systemctl disable firewalld.service &>/dev/null
echo -e "${LOG_COLOR}防火墻已經關閉,並禁止開機啟動${AND_COLOR}"
#更改IP
read -p "接下來配置IP\n請鍵入IP地址:" IPAD
read -p "請鍵入子網掩碼:" NET
read -p "請鍵入網關地址:" GAT
sed -i ‘s#^BOOT.*#BOOTPROTO=static#g‘ ${D_NET}
cat >>${D_NET} <<EOF
IPADDR=${IPAD}
NETMASK=${NET}
GATEWAY=${GAT}
EOF
systemctl restart network.service &>/dev/null
echo -e "${LOG_COLOR}網卡配置已生效${AND_COLOR}"
sleep 1
echo -e "${LOG_COLOR}主機環境配置完成,系統自動重啟 3...${AND_COLOR}"
sleep 1
echo -e "${LOG_COLOR}2..${AND_COLOR}"
sleep 1
echo -e "${LOG_COLOR}1...${AND_COLOR}"
sleep 1
reboot
SHELL腳本練習手冊