從小白到小白😂

不知道寫啥,用我寫的腳本練習題湊湊數

1.顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小
echo “My hostname is(我的主機名是): `hostname`”
echo “My ipv4address is(我的IPv4地址是): `ifconfig|head -n2|tail -n1|tr -s ” ” “:”|cut -d: -f3`”
echo “My operating system verision is(我的操作系統版本是): `cat /etc/centos-release`”
echo “My kernel version is(我的內核版本是): `uname -r`”
echo “My the CPU model is(我的CPU型號是): `lscpu|grep “Model name”|tr -s ” ” “:” |cut -d: -f3`”
echo “My memory size is(我的內存大小是): `cat /proc/meminfo|grep -n “MemTotal”|tr -s ” ” |cut -d: -f3`”
echo “My hard drive size is(我的硬盤大小是): `lsblk|grep “\bsda\b”|tr -s ” ” “:”|cut -d: -f5`”

2每日將/etc/目錄備份到/root/etcYYYY-mm-dd中
echo -e “\e[1;31mTo start backup(開始備份)!\e[0m”;`read -t 3`
cp -a /etc/ /root/etc/`date +%F`
echo -e “\e[1;31mBack up(備份完畢)!\e[0m”

3顯示當前硬盤分區中空間利用率最大的值
echo “當前硬盤分區空間利用率最大的值:`df|grep “sda”|tr -s ” ” “:”|cut -d: -f5|sort -nr|head -n1`”

4顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序
echo “當前鏈接本主機的IPv4和連接數:`netstat -nt|tr -s ” ” “:”|cut -d: -f6|grep “[0-9]”|uniq -c|sort -nr`”

5計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和
a=`cat /etc/passwd|head -n10|tail -n1|tr -s ” ” “:”|cut -d: -f3`
b=`cat /etc/passwd|head -n20|tail -n1|tr -s ” ” “:”|cut -d: -f3`
let sun=a+b
echo “第十個用戶與第二十個用戶的ID之和:$sun”

6傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
[ $# -ne 2 ] && echo -e “\e[1;31mPlease enter two files!(請輸入兩個文件!)\e[0m”;exit
a=`grep “^$” $1|wc -l`
b=`grep “^$” $2|wc -l`
let sun=a+b
echo “兩個文件所有空白行之和:$sun”

7統計三個目錄中共有多少個一級子目錄和文件
[ $# -ne 3 ] && echo -e “\e[1;31mPlease enter three directories!(請輸入三個目錄!)\e[0m”;exit
a=`ls -l $1|grep -n “^d”|wc -l`
b=`ls -l $1|grep -n “^-“|wc -l`
let c=a+b
echo “${1}目錄下一共有${c}個子目錄以及文件”
d=`ls -l $2|grep -n “^d”|wc -l`
f=`ls -l $2|grep -n “^-“|wc -l`
let g=d+f
echo “${2}目錄一一共有${g}個子目錄以及文件”
s=`ls -l $3|grep -n “^d”|wc -l`
w=`ls -l $3|grep -n “^-“|wc -l`
let x=w+s
let sun=c+g+x
echo “${3}目錄下一共有${x}個子目錄以及文件”
echo “${1},${2},${3}目錄下一共有${sun}個子目錄以及文件”

8接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數
[[ $1 -lt 1 ]] && echo -e “\e[1;31mPlease give me a parameter!(請給個參數!)\e[0m” || grep “^$” $1|wc -l

9接受一個主機的IPv4地址做為參數,測試IP格式是否正確,是否可連通
echo {$1}|egrep ‘\<((([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5]))\>’ && (ping -c1 $1 &>/dev/null && echo “The IP address is accessible(該IP地址可訪問)” || echo “The IP address is not accessible(該IP地址不可訪問)”) ||echo “IP格式不正確”;exit

10檢查磁盤分區空間和inode使用率,如果超過80%,就發廣播警告空間將滿
a=df -i|grep “sda”|tr -s ” ” “%”|cut -d% -f5|sort -nr|head -n1
b=df|grep “sda”|tr -s ” ” “%”|cut -d% -f5|sort -nr|head -n1
[ $a -ge 80 ] && echo “The disk node space will be full(磁盤節點空間將滿)” || echo “There is plenty of disk space(磁盤節點空間充足)”
[ $b -ge 80 ] && echo “Disk space will be full(磁盤空間將滿)” || echo “Ample disk space(磁盤空間充足)”

11判斷當前用戶對指定參數文件,是否不可讀并且不可寫
[ -r $1 -a -w $1 ] && echo “The current user can read and write this file(當前用戶對此文件可讀可寫)” || echo “The current user must not read the file(當前用戶對此文件不可讀不可寫)”

12判斷參數文件是否為sh后綴的普通文件,如果是,添加所有人可執行權限,否則提示用戶非腳本文件
[ -f $1 ] && [[ $1 == *\.sh ]] && chmod u+x,g+x,o+x $1;echo “Execute permission has been added(執行權限已添加)” || echo “It’s not a script file(不是
腳本文件)”

13禁止和充許普通用戶登錄系統
[ -e /etc/nologin ] && echo “Regular users are not allowed to log in(已禁止普通用戶登錄)” || (touch /etc/nologin ;echo “Regular users are not allowed to log in(已禁止普通用戶登錄)”)
[ -e /etc/nologin ] && (rm -f /etc/nologin ;echo “To allow regular users to log in(以允許普通用戶登錄)”) || echo “To allow regular users to log in(>以允許普通用戶登錄)”

 

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/96196

(0)
從那以后從那以后
上一篇 2018-04-15 20:37
下一篇 2018-04-15

相關推薦

欧美性久久久久