(1)編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小
#!/bin/bash
echo “MY hostname is `hostname`”
echo “My IPv4 address is `ifconfig | egrep -w “inet” | head -n1 | tr -s ” ” “:” | cut -d: -f4` ”
echo “My Kernel is `cat /etc/redhat-release` ”
echo “My CPU is `lscpu | egrep “Model name” | tr -s ‘ ‘ | cut -d: -f2`”
echo “My Mem is `free | egrep “Mem” | tr -s ” ” “:” | cut -d: -f2`”
echo “My Disk is `lsblk | egrep “^sda” | tr -s ” ” | cut -d” ” -f4`”
(2)編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到 /root/etcYYYY-mm-dd中
答:
#!/bin/bash
#
echo “copy start…”
sleep 5
cp -a /etc/ /data/etc`date +%F`
echo “copy finished”
(3)編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值
答:
#!/bin/bash
#
df | egrep “^/dev/sd” | tr -s ” ” “%” | cut -d% -f5 | sort -nr | head -n1
(4)編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序
答:
#!/bin/bash
#
sort access_log | cut -d” ” -f1 | uniq -c | sort -nr
或者:
sort access_log | egrep -o “^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\>” | uniq -c | sort -nr
(5)編寫腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第 20用戶的ID之和
答:
#!/bin/bash
#
uid10=”`cat /etc/passwd | head | tail -n1 | cut -d: -f3`”
uid20=”`cat /etc/passwd | head -n20 | tail -n1 | cut -d: -f3`”
usum=”$[ uid10 + uid20 ]”
echo $usum
(6)編寫腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
答:
#!/bin/bash
#
space1=”`cat $1 | egrep “^[[:space:]]*$” | wc -l`”
space2=”`cat $2 | egrep “^[[:space:]]*$” | wc -l`”
spacesum=”$[ space1 + space2 ]”
echo $spacesum
(7)編寫腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級 子目錄和文件
答:
#!/bin/bash
#
file1=”`ls $1 | wc -l`”
file2=”`ls $2 | wc -l`”
file3=”`ls $3 | wc -l`”
sumfile=”$[ file1 + file2 + file3 ]”
echo $sumfile
(8)編寫腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數 不小于1,則顯示第一個參數所指向的文件中的空白行數
答:
#!/bin/bash
#
[ “$#” -lt 1 ] \
&& { echo “please input a path”; exit; } \
|| { space=”`cat $1 | grep -c “^[[:space:]]*$”`”; echo “The file space lines are $space”; }
(9)編寫腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測 試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可 ping通,則提示用戶“該IP地址不可訪問”
答:
#!/bin/bash
#
ping -c1 $1 &>/dev/null && echo “host up” || echo “time out”
(10)編寫腳本/root/bin/checkdisk.sh,檢查磁盤分區空間和inode使用率,如果超過80%,就發廣播警告空間將滿
答:
#!/bin/bash
#
disk=”`df | egrep “^/dev/sda” | tr -s ” ” % | cut -d% -f5 | sort -nr | head -n1`”
inode=”`df -i | egrep “^/dev/sda” | tr -s ” ” % | cut -d% -f5 | sort -nr | head -n1`”
[ “$disk” -ge 80 ] && echo “warning!!!The disk will be full”
[ “$inode” -ge 4 ] && echo “warning!!!The inode will be full”
(11)編寫腳本/bin/per.sh,判斷當前用戶對指定參數文件,是否不可讀并且不可寫
答:
#!/bin/bash
#
[ “$#” -lt 1 ] && { echo “please input a argument”;exit; }
[[ -r $1 ]] && [[ -w $1 ]] && echo “yes” || echo “no”
(12)編寫腳本/root/bin/excute.sh ,判斷參數文件是否為sh后綴的普通文件,如果是,添加所有人可執行權限,否則提示用戶非腳本文件
答:
#!/bin/bash
#
[[ -f $1 ]] && [[ “$1” == *.sh ]] && chmod a+x “$1” || echo “no .sh file”
(13)編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統
答:
①For nologin:
#!/bin/bash
#
[[ -e /etc/nologin ]] && echo “no login” || { touch /etc/nologin ; echo “no login”; }
②For login:
#!/bin/bash
#
[[ -e /etc/nologin ]] && { rm -f /etc/nologin ; echo “access login”; } || echo “access login”
(14)讓所有用戶的PATH環境變量的值多出一個路徑,例如: /usr/local/apache/bin
答:①vim /etc/profile ②找到export PATH USER在下面一行輸入export PATH=$PATH:/usr/local/apache/bin ③. /etc/profile 使其生效。
(15)用戶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)
答:①vim ~/.bashrc ②alias cdnet=’cd /etc/sysconfig/network-scripts/’
alias editnet=’vim /etc/sysconfig/network-scripts/ifcfg-eth0′
alias editnet=’vim /etc/sysconfig/network-scripts/ifcfg-eno16777736′
alias rm=’rm -i’
(16)任意用戶登錄系統時,顯示紅色字體的警示提醒信息“Hi,dangerous!”
答:在[root@Centos6 profile.d]#下建立腳本vim dangerous.sh
#!/bin/bash
#
echo -e “\033[31m ###################################################\033[0m”
echo -e ” \033[31m hi,dangerous!!!\033[0m”
echo -e “\033[31m ###################################################\033[0m”
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/95850