Linux 階段練習(2)
# cat /etc/passwd |grep '.*:x:\<[2-9][0-9][1-9]\>.*'
68、添加用戶bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶名和shell同名的行
# cat /etc/passwd |grep -E '^(.*):x.*/\1$' 後向引用的\1$ 已經可以確定^(.*)的字符個數了
69、利用df和grep,取出磁盤各分區利用率,並從大到小排序
# df |egrep -o '1?[0-9]?[0-9]%'|sort -nr
70、顯示三個用戶root、mage、wang的UID和默認shell
# cat /etc/passwd |grep -E '^(root|wang|mage)' |cut -d: -f3,7
71、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)後面跟一個小括號的行
# cat /etc/rc.d/init.d/functions |grep -E '^([[:alpha:]]+|_+)[a-zA-Z0-9_]*\('
72、使用egrep取出/etc/rc.d/init.d/functions中其基名
# echo /etc/rc.d/init.d/functions |grep -Eo '[[:alpha:]]+$' (註意-v 是選擇非匹配的行,不能在匹配的相同行中取非)
73、使用egrep取出上面路徑的目錄名
# echo /etc/rc.d/init.d/functions |grep -Eo '.*/'
74、統計last命令中以root登錄的每個主機IP地址登錄次數
# last |grep -Eo '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'|sort |uniq -c
75、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255
# [0-9]
# [1-9][0-9]
# 1[0-9][0-9]
# 2[0-4][0-9]
# 25[0-5]
76、顯示ifconfig命令結果中所有IPv4地址
# ifconfig | grep -Eo '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
77、將此字符串:welcome to magedu linux 中的每個字符去重並排序,重復次數多的排到前面
# echo "welcome to magedu linux" |grep -o '.'| grep -o '[[:alpha:]]' |sort |uniq -c|sort -nr
78、復制/etc/profile至/tmp/目錄,用查找替換命令刪除/tmp/profile文件中的行首的空白字符
# :%s/^ +// (:%s/^[[:space:]]+//)
79、復制/etc/rc.d/init.d/functions文件至/tmp目錄,用查找替換命令為/tmp/functions的每行開頭為空白字符的行的行首添加一個#號
# :%s/^[[:space:]]/#&
80、在vim中設置tab縮進為4個字符
# set ts=4
81、復制/etc/rc.d/init.d/functions文件至/tmp目錄,替換/tmp/functions文件中的/etc/sysconfig/init為/var/log
# :%s@/etc/sysconfig/init@/var/log@
82、刪除/tmp/functions文件中所有以#開頭,且#後面至少有一個空白字符的行的行首的#號
# :%s/^#\( \+\)/\1/
83、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小
#!/bin/bash
echo -e "\e[1;31mThe hostname:\e[0m `hostname`"
echo -e "\e[1;31mThe System IP address:\e[0m `ifconfig ens33|head -2 |tail -1|tr -s ' '|cut -d' ' -f3`"
echo -e "\e[1;31mCurrent System Version:\e[0m ` cat /etc/centos-release`"
echo -e "\e[1;31mKernel Version:\e[0m `uname -r`"
echo -e "\e[1;31mThe Mode of CPU:\e[0m `lscpu |grep 'Model name'|tr -s ' '|cut -d: -f2`"
echo -e "\e[1;31mThe total space of Memory:\e[0m `cat /proc/meminfo |grep 'MemTotal'|tr -s ' '|cut -d: -f2`"
echo -e "\e[1;31mThe total space of disk:\e[0m ` lsblk |grep -w "sda"|tr -s ' ' ':'|cut -d: -f5`"
84、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd中
#!/bin/bash
echo -e "\e[1;31m Backup is starting... \e[0m"
[ -d /root/etc`date +%F` ] && { echo "Backup dir has been exist"; read -p "Do you want to overwrite? Yes or No: " key; }
if [[ $key =~ [Yy]+([Ee][Ss])? ]];then
rm -rf /root/etc`date +%F`
cp -av /etc /root/etc`date +%F`
elif [[ $key =~ [Nn][Oo] ]]; then
ran_num=$RANDOM
read -p "Backup name can be added a random number $ran_num, Yes or No: " key1
if [[ $key1 =~ [Yy][Ee][Ss] ]]; then
cp -av /etc /root/etc`date +%F`$ran_num
echo "Backup with a name has been finished."
else
exit
fi
fi
cp -av /etc /root/etc`date +%F`
echo -e "\e[1;31m Backup has been finished.\e[0m"
85、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值
#!/bin/bash
echo -e "\e[1;31mThe the biggest partition usage : \e[0m`df |grep '/dev/sd\+' |tr -s ' ' ':'|cut -d: -f5 |sort -nr |head -1` "
86、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,並按連接數從大到小排序
#!/bin/bash
netstat -nt |grep 'ESTABLISHED'|tr -s ' ' ':' |cut -d: -f6|sort |uniq -c | sort -nr
87、編寫腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和
#!/bin/bash
uid10=`cat /etc/passwd| head -10|tail -1|cut -d: -f3`
uid20=`cat /etc/passwd| head -20|tail -1|cut -d: -f3`
echo "Sum=$[uid10+uid20]"
88、編寫腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
#!/bin/bash
file1=$1
file2=$2
file1_blanks=`grep '^[[:space:]]*$' ${file1} |wc -l`
file2_blanks=`grep '^[[:space:]]*$' ${file2} |wc -l`
echo -e "\e[1;31mTotal blanks:\e[0m $[file1_blanks+file2_blanks]"
89、編寫腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件
#!/bin/bash
etc_dirs=`ls -Al /etc/ |grep '^d'|wc -l`
usr_dirs=`ls -Al /usr/ |grep '^d'|wc -l`
var_dirs=`ls -Al /var/ |grep '^d'|wc -l`
etc_files=`ls -Al /etc/ |grep '^-'|wc -l`
usr_files=`ls -Al /usr/ |grep '^-'|wc -l`
var_files=`ls -Al /var/ |grep '^-'|wc -l`
echo "etc_dirs=$etc_dirs"
echo "usr_dirs=$usr_dirs"
echo "var_dirs=$var_dirs"
echo "etc_files=$etc_files"
echo "usr_files=$usr_files"
echo "var_files=$var_files"
90、編寫腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小於1,則提示用戶“至少應該給一個參數”,並立即退出;如果參數個數不小於1,則顯示第一個參數所指向的文件中的空白行數
#!/bin/bash
[ "$#" -lt 1 ] && echo "Need a parameter" && exit
[ "$#" -ge 1 ] && echo "Total blanks:`grep '^[[:space:]]*$' $1|wc -l`"
91、編寫腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”
#!/bin/bash
ping -c 1 $1 &>/dev/null && echo "Host up" || echo "Host down"
92、編寫腳本/root/bin/checkdisk.sh,檢查磁盤分區空間和inode使用率,如果超過80%,就發廣播警告空間將滿
#!/bin/bash
inode_usage=`df -i|tr -s ' ' ':' |cut -d: -f5 |sort -nr |head -1 |grep -Eo '[[:digit:]]{1,3}'`
partition_usag=`df |grep '/dev/sda*'|tr -s ' ' ':'|cut -d: -f5|sort -nr |head -1|grep -Eo '[[:digit:]]{1,3}'`
[ $inode_usage -gt 80 -o $partition_usag -gt 80 ] && `wall "warning..."`
93、編寫腳本/bin/per.sh,判斷當前用戶對指定參數文件,是否不可讀並且不可寫
#!/bin/bash
[ ! -r $1 -a ! -w $1 ] && echo "The file can't read and write" || echo "The file can read an write"
94、編寫腳本/root/bin/excute.sh ,判斷參數文件是否為sh後綴的普通文件,如果是,添加所有人可執行權限,否則提示用戶非腳本文件
#!/bin/bash
[[ $1 =~ ..*\.sh$ ]] && { echo ".sh"; chmod +x $1; } || echo "no .sh"
95、編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統
#!/bin/bash
users=`cat /etc/passwd|grep -Ew '[[:digit:]]{4}' |cut -d: -f1`
for i in $users; do
usermod -s /sbin/nologin $i
done
#!/bin/bash
users=`cat /etc/passwd|grep -Ew '[[:digit:]]{4}' |cut -d: -f1`
for i in $users; do
usermod -s /bin/bash $i
done
Linux 階段練習(2)