shell腳本

shell腳本的練習題

timg

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

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: systeminfo.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
echo “The CPU of the system is:” `lscpu |grep “name” |tr -s ” ” |cut -d ” ” -f 5`
echo “The memory size of the system is:” `free -h |grep “Swap” |tr -s ” ” |cut -d” ” -f 2`
echo “The size of the system’s hard disk is :” `lsblk |grep -w “sda” |tr -s ” ” |cut -d ” ” -f 4`
echo “The system version is :” `cat /etc/centos-release |grep -o ” [0-9]”`
echo “The kernel version is :” `uname -r`
echo “The name of the system host :” `hostname`
echo “The IP of the system is :” `ifconfig ens33 |sed -n “2p” |sed “s/.*inet//” |sed “s/net.*//”`

 

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

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: backup.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
echo -e “\e[1;31mStart a backup…\e[0m”
sleep 5
cp -av /etc/ /data/etc`date “+%F”`
echo -e “\e[1;33mBackup completion\e[0m”

 

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

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: disk.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
echo “The maximum value of the hard disk is :” `df |grep “sda” |tr -s ” ” |cut -d” ” -f 5 |sort -nr |head -n1`

 

4.編寫腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第
20用戶的ID之和

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: sumid.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
UID1=`cat /etc/passwd |head -n10 | tail -n1 |cut -d: -f 3`
UID2=`cat /etc/passwd |head -n20 | tail -n1 |cut -d: -f 3`
let UID3=UID1+UID2
echo $UID3

 

5. 編寫腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計
算這兩個文件中所有空白行之和

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: sumspace.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
blabk1=`cat /$1 |grep -c “^$”`
blabk2=`cat /$2 |grep -c “^$”`
let blabk=blabk1+blabk2
echo $blabk

 

6、編寫腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級
子目錄和文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: sumfile.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
file1=`ls -l /etc |grep -c -e “^d” -e “^-“`
file2=`ls -l /var |grep -c -e “^d” -e “^-“`
file3=`ls -l /usr |grep -c -e “^d” -e “^-“`
let file4=file1+file2+file3
echo $file4

 

7.編寫腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測
試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問” ;如果不可
ping通,則提示用戶“該IP地址不可訪問”

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: hostping.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[[ $1 =~ “\<(([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])\>” ]] && echo “Please enter a correct IP” && exit
ping -c1 $1>/dev/null && echo “The IP can be accessed” || echo “The IP is not accessible”

 

8.編寫腳本/root/bin/checkdisk.sh,檢查磁盤分區空間和inode使用率,如
果超過80%,就發廣播警告空間將滿

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: checkdisk.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
disk=`df |grep “sda”|tr -s ” ” “%” |cut -d”%” -f 5 |sort -nr |head -n1`
[ “$disk” -ge 40 ] && echo “The use rate of disk partitions is now :” $disk && wall Space will be full

 

9. 編寫腳本/root/bin/excute.sh ,判斷參數文件是否為sh后綴的普通文件,如
果是,添加所有人可執行權限,否則提示用戶非腳本文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: per.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[ $# -ne 1 ] && echo “You have to enter one” && exit
[ ! -r $1 -a -w $1 ] && echo “The document is unreadable and cannot be written.” || echo “Read and write permissions to the file”

 

10. 編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統

創建nologin文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: nologin.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[ -a /etc/nologin ] && echo “The nologin file exists! “|| (touch /etc/nologin; echo -e “\e[1;31mThe nologin file is created!\e[0m “;)

刪除nologin文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: login.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[ -a /etc/nologin ] && echo -e `rm -f /etc/nologin` -e “\e[1;31mThe file has been deleted!\e[0m” || echo “The file does not exist”

11. 讓所有用戶的PATH環境變量的值多出一個路徑,例如:/usr/local/apache/bin

echo $PATH |sed -r “s@(/usr/local/sbin:)@/usr/local/apache/bin:\1@”

 

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

(1)
周亞飛周亞飛
上一篇 2018-04-18 13:51
下一篇 2018-04-18 20:13

相關推薦

欧美性久久久久