linux-shell 練習題

 

linux-shell 練習題

一、編寫腳本 systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操做系統版本,內核版本,CPU型號,內存大小,硬盤大小linux

[root@CentOS7 scripts]# cat systeminfo.sh 
#!/bin/bash
#
COLOR="\033[1;31m"
COLOR_END="\033[0m"
ECHO="echo -e"
$ECHO "hostname is ${COLOR}`hostname`$COLOR_END" 
$ECHO "ipaddr is ${COLOR}`ifconfig ens33|grep -Eo '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'|head -1`$COLOR_END"
$ECHO "os info is ${COLOR}`cat /etc/redhat-release`$COLOR_END"
$ECHO "kenel info is ${COLOR}`uname -r`$COLOR_END"
$ECHO "cpuinfo is ${COLOR}`lscpu|grep 'Model name:'|tr -s ' '|cut -d: -f2|sed -r 's/^[[:space:]]//'`$COLOR_END"
$ECHO "Meminfo is ${COLOR}`free -h|grep 'Mem'|tr -s ' ' ':'|cut -d':' -f2`$COLOR_END"
$ECHO "disk space is ${COLOR}`lsblk|grep 'disk'|tr -s ' '|cut -d' ' -f4`$COLOR_END"

 二、編寫腳本 backup.sh,可實現每日將/etc/目錄備份到/backup/etcYYYY-mm-dd中shell

[root@CentOS7 scripts]# cat backup.sh 
#!/bin/bash
#
DIR=/backup/etc`date +"%F-%T"`
if [ ! -d "$DIR" ];then
	mkdir -p $DIR
fi
echo -e "\033[1;31mstart backup /etc directory\033[0m"
cp -rv --preserve=mode /etc $DIR
echo -e "\033[1;31m/etc directory backup at $DIR\033[0m\a"

三、編寫腳本 disk.sh,顯示當前硬盤分區中空間利用率最大的值bash

[root@CentOS7 scripts]# cat disk.sh 
#!/bin/bash
#
diskSpace=`df -h|grep "/dev/sd"|tr -s ' '|cut -d' ' -f5|tr -d '%'|sort -nr|head -1`
echo -e "\033[1;31mdisk space max is ${diskSpace}%\033[0m"

四、編寫腳本 links.sh,顯示正鏈接本主機的每一個遠程主機的IPv4地址和鏈接數,並按鏈接數從大到小排序ui

[root@CentOS7 scripts]# cat links.sh 
#!/bin/bash
#
foreign_ip_connect=`netstat -nt|tr -s ' ' ':'|cut -d: -f6|grep -Eo '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'|sort|uniq -c|sort -nr`
echo -e  "\033[1;31m$foreign_ip_connect\033[0m"
相關文章
相關標籤/搜索