shell腳本

最近學了shell腳本,自己感覺挺有難度的,今天就簡單整理一些shell腳本的練習和作業

練習:

   1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小。
#!/bin/bash
# ——————————————————–
# Filename: 1.sh                                        
# Revision: 1.0                                          
# Description: Centos7配置信息                     
# ——————————————————–
ipv4=`ifconfig ens33 |grep “\<inet\>”|tr -s ‘ ‘|cut -d ‘ ‘ -f3` 
banben=`cat /etc/redhat-release;uname -r`
CPU=`lscpu|grep “Model name”|cut -d : -f 2|tr -s ‘ ‘`
Ferr=$[$(cat /proc/meminfo |head -1|grep -o “[[:digit:]]\+”)/1024/1024]
Fdisk=`fdisk -l|head -n 2|tail -n 1|cut -d ‘ ‘ -f3,4`
echo “系統基本信息”
echo “name:”$(hostname)
echo “IPV4地址:””$ipv4”
echo “版本信息和內核:”$banben
echo “CPU型號:””$CPU”
echo “內存大小:””$Ferr GB”
echo “硬盤大?。?#8221;”$Fdisk”

2、編寫腳本/root/bin/backup.sh,可實現將/etc/目錄備份到/root/etcYYYY-mm-dd中? 
#!/bin/bash
cp -a /etc /root/etc$(date +%F)

3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值?
#!/bin/bash
#——————————————–
#Filename: 
#Revision: 1.0
#Date:
#Description:硬盤利用率最大的值:
#——————————————–
echo “硬盤利用率最大的值:$(df |grep -o “\<[[:digit:]]\{1,3\}%” |sort -n |tail -n 1)”;

4、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序
#!/bin/bash
netstat -tun | grep “[0-9]” | tr -s ‘ ‘ : |cut -d: -f6|sort|uniq -c|sort -rn

1.寫一個腳本名為jiaozuoyexx.sh 當執行該腳本時如jiaozuoyeXX.sh testXX.sh,就會自動將該testXX.sh傳給教師機,路徑是
scp testXX.sh mage26@172.17.252.213:~/scripts  密碼為mage26

#!/bin/bash
# ——————————————
# Filename:  jiaozuoye16.sh
# Revision:  1.0 
# Date:    
# Author:  
# Description: 交作業
# ——————————————

scp $1 mage26@172.17.252.213:~/scripts

2.寫一個能夠創建新腳本的Shell script,如名為createshXX.sh 當執行時createsh /root/bin/test1.sh
則會自動創建并打開/root/bin/test1.sh,且其中包含以下內容。
#!/bin/bash
# ——————————————
# Filename: 
# Revision: 
# Date: 
# Author: 
# Email:
# Website:
# Description: 
# ——————————————

—————————–答案1————————
#!/bin/bash
# ———————–
# filename:
# revision:
# date:
# author:
# email:
# website:
# description:
# ———————–
echo “#!/bin/bash
# ———————–
# filename:
# revision:
# date:
# author:
# email:
# website:
# description:
# ———————–
” >>$1
chmod +x $1
vim $1

———————答案2—————————————

#!/bin/bash
touch $1
cat >$1<<EOF
#!/bin/bash
# ——————————————
# Filename: 
# Revision: 
# Date: 
# Author: 
# Email:
# Website:
# Description: 
# ——————————————
#”***********************腳本內容如下*********************”
EOF

chmod +x $1
vi   $1

—————————答案3———————–
#!/bin/bash
#———————————————————-
#Filename:moban
#Revision:1.0
#Description:
#Date:2017年8月2日18:14:59
#———————————————————-
#Filename:moban
#Revision:1.0
#Description:
#Date:2017年8月2日18:14:59
#Email:dreamworks.cnn@gmail.com
#uthor:THINKLXY.50
#Website:
#———————————————————-
read -p “Enter the ScriptName: ” name
touch $name

A=(0 1 2 3 4 5 6 7 8 9 10)

A[1]=”#!/bin/bash\n”
A[2]=”#———————————————————-\n”
A[3]=”#Filename:\n”
A[4]=”#Revision(版本):1.0\n”
A[5]=”#Description:\n”
A[6]=”#Date:\n”
A[7]=”#Email:dreamworks.cnn@gmail.com\n”
A[8]=”#Website(博客):\n”
A[9]=”#Author(作者):THINKLXY\n”
A[10]=”#———————————————————-\n”
A[0]=

echo -e “${A[*]}” |sed “s/^[[:blank:]]//” >> $name
chmod 700 $name

vim $name

1、編寫腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和? 
#!/bin/bash
# —————————————————-
# Filename: sumid.sh                                               
                                 
# Revision: 1.0
# Date: 2017/08/02
# Author: along
# Description: 計算/etc/passwd 第10和20用戶ID之和
# —————————————————-
root1=$(cat /etc/passwd | head -$1 |tail -1 | cut -d: -f3)
root2=$(cat /etc/passwd | head -$2 |tail -1 | cut -d: -f3)
echo $[$root1+$root2]
unset root1 root2

補充:  cat -n和nl都是顯示行號的

對比自己,還有很多不足,需要改進

shell腳本                   


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

#!/bin/bash
# ——————————————
# Filename: sumspace04.sh 
# Revision: null
# Date: 2017-08-04 06:32:47
# Author: zhaodong
# Email: 1213217229@qq.com
# Website: null
# Description: new file
# ——————————————
read -p “please input first filename:” first_filename
read -p “please input second filename:” second_filename
file1=`cat $first_filename|grep ^[[:space:]]*$|wc -l`
file2=`cat $second_filename|grep ^[[:space:]]*$|wc -l`
echo “sum=$[$file1+$file2]”

———————————–答案————————————–

#!/bin/bash
# ——————————————
# Filename:sumspace.sh
# Revision:
# Date:2017-8-3
# Author:lily Lee
# Email:2319761707@qq.com
# Website:
# Description:This is script
# ——————————————
_space=`cat $@ | egrep “^[[:space:]]*$” |wc -l`
echo $_space

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

#!/bin/bash
# ——————————————
# Filename:sumspace.sh
# Revision:
# Date:2017-8-3
# Author:lili Lee
# Email:2319761707@qq.com
# Website:
# Description:This is a script
# ——————————————
_etc=`ls -A $1 | wc -l`
_var=`ls -A $2 | wc -l`
_usr=`ls -A $3 | wc -l`
let sum=$_etc+$_var+$_usr
echo $sum

對比自己

shell腳本
   
1、編寫腳本/root/bin/argsnumsh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數

[   “$@”  ]&&(cat $1 |grep “^[[:space:]]*$” |wc -l) ||(echo 至少應該給一個文件參數!;exit)

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

ping -c1 -w1 $1 &> /dev/null && echo “該IP地址可以訪問!”||echo”該IP地址不可訪問!”

3、編寫腳本/root/bin/checkdisk.sh,檢查磁盤分區空間和inode使用率,如果超過80%,就發廣播警告空間將滿
diskused_max=df | grep sd|sort -nr -k5|head -1|tr -s ' ' %|cut -d% -f5
inodeused_max=df -i| grep sd|sort -nr -k5|head -1|tr -s ' ' %|cut -d% -f5
[ “$diskused_max” -gt “80” ] && wall “空間即將滿”||echo “空間使用率不超過80%”
[ “$inodeused_max” -gt “80” ] && wall “inode即將滿”||echo “inode使用率不超過80%”
unset diskused_max inodeused_max 

———————————————-答案二——————————————————–
dev=df|grep "/dev/sd"|egrep -o "[0-9]{1,3}%"|sort -n|tail -n 1|cut -d% -f1
ino=df -i|egrep -o "[0-9]{1,3}%" |sort -n|tail -n 1|cut -d% -f1
[[ “$dev” -gt 80 ]] || [[ “$ino” -gt 80 ]] && echo $(wall 磁盤已滿)
unset dev
unset ino
    

作業:
1..編寫腳本/bin/per.sh,判斷當前用戶對指定的參數文件,是否不可讀并且不可寫
#!/bin/bash
# ——————————————
# Filename:
# Revision:
# Date:
# Author:
# Email:
# Website:
# Description:
# ——————————————
# read -p “please input filename:”
$1

[ ! -r $1 -a ! -w $1 ] && echo true || echo false

用到如下知識點

shell腳本

2.編寫腳本/root/bin/excute.sh ,判斷參數文件是否為sh后綴的普通文件,如果是,添加所有人可執行權限,否則提示用戶非腳本文件
#!/bin/bash
# ——————————————
# Filename:
# Revision:
# Date:
# Author:
# Email:
# Website:
# Description:
# ——————————————
# read -p “please input a filename:” $1
#[ -f $1 ] && [[ $1 =~ “.sh\>” ]] && chmod u+x $1 || echo “這是一個非腳本文件”
ls $1 |grep -q “.sh$” && [ -f $1 ] && chmod u+x $1 && ls -l $1 || echo “這是一個非腳本文件”
3.編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統
#!/bin/bash
# ——————————————
# Filename:
# Revision:
# Date:
# Author:
# Email:
# Website:
# Description:
# ——————————————
#id $1 |cut -d ” ” -f1
#id $1 |tr -s ‘ ‘ |cut -d “=” -f2 |cut -d ” ” -f1 && [ $1 -eq 1000 ] && echo “禁止非普通用戶登錄”
id $1 |tr -s ‘ ‘ |cut -d “=” -f2 |cut -d ” ” -f1 |grep -q -o “[0-9]\{4,\}”&& usermod -L $1 && echo “禁止登錄”
#!/bin/bash
# ——————————————
# Filename:
# Revision:
# Date:
# Author:
# Email:
# Website:
# Description:
# —————————————–

id $1 |tr -s ‘ ‘ |cut -d “=” -f2 |cut -d ” ” -f1 |grep -q -o “[0-9]\{4,\}” && usermod -U $1 && echo “please input name and passwd”

shell腳本

補充:這里簡單介紹一個特殊的命令

shell腳本

vim 中搜索的時候支持基本正則不支持擴展正則

shell腳本

原創文章,作者:sqjl,如若轉載,請注明出處:http://www.www58058.com/83933

(0)
sqjlsqjl
上一篇 2017-08-05
下一篇 2017-08-05

相關推薦

  • Hadoop hdfs 分布式文件系統

    Hadoop簡介:一個分布式系統基礎架構,由Apache基金會開發。用戶可以在不了解分布式底層細節的情況下,開發分布式程序。充分利用集群的威力高速運算和存儲。Hadoop實現了一個分布式文件系統(Hadoop Distributed File System),簡稱HDFS。HDFS有著高容錯性的特點,并且設計用來部署在低廉的(low-cost)硬件…

    Linux干貨 2017-04-19
  • LAMP及部署wordpress/phpMyadmin

    LAMP詳解 wordpress安裝 一、引言 lamp含義:黃金組合。簡要介紹一下下面這四個東西吧。linux,不用說了有很多發行版本,主流的三大版本是Debian系列,RedHat系列,slackware系列。apache,全稱叫Apache HTTP Server,是世界使用排名第一的web服務器軟件,httpd是超文本傳輸協議http服務器的主程序?!?/p>

    Linux干貨 2016-12-13
  • Linux 第四天: (07月28日) Linux文件管理

    Linux 第四天: (07月28日) Linux文件管理         rootfs 根目錄文件系統 root filesystemLSB  Linux Standard BaseFHS 文件系統分層結構 Filesystem Hierarchy Standard     藍色表示 目…

    Linux干貨 2016-08-08
  • ansible-yaml初級語法(hosts、remote_user、tasks)

    環境準備:     1、主控節點IP:172.16.16.9     2、兩個被控節點:1)172.16.16.48   2)172.16.16.50     3、hosts配置如下:       &…

    Linux干貨 2016-11-28
  • 關于rpm及yum的一些感想

    rpm -ivh PACKAGE_FILE  注意: (1) 不要對內核做升級操作;Linux支持多內核版本并存,因此 ,對直接安裝新版本內核 v rpm {-q嚴格–query} [select-options] [query-options] v [se…

    Linux干貨 2016-08-24
  • 一切皆文件

    Linux Linux內一切皆文件;表現之一:硬件設備也通過文件表示 物理終端:/dev/console 虛擬終端: /dev/tty#[1,6] 串行終端:/dev/ttyS# 偽終端: /dev/pts/# 注意:在啟動設備之后,在其上關聯一個用戶接口程序,即可實現與用戶交互,交互式程序有兩類:GUI(圖形化界面)和CLI(命令行界面)。 查看終端設備:…

    Linux干貨 2017-04-01
欧美性久久久久