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 “內存大?。?#8221;”$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 19:25
下一篇 2017-08-05 19:57

相關推薦

  • 第二周

    3、(1)、  :  ~]# mkdir -p /tmp/{a_c,a_d,b_c,b_d}                  or     ~]# …

    Linux干貨 2016-08-22
  • 2016.10.12 成為了馬幫的一員!

    未來的6個月內,好好學習天天向上!

    Linux干貨 2016-10-19
  • 關于大型網站技術演進的思考(十四)–網站靜態化處理—前后端分離—上(6)

    原文出處: 夏天的森林   前文講到了CSI技術,這就說明網站靜態化技術的講述已經推進到了瀏覽器端了即真正到了web前端的范疇了,而時下web前端技術的前沿之一就是前后端 分離技術了,那么在這里網站靜態化技術和前后端分離技術產生了交集,所以今天我將討論下前后端分離技術,前后端分離技術討論完后,下一篇文章我將會以網站 靜態化技術的…

    Linux干貨 2015-03-03
  • 正則表達與擴展正則表達

    說到正則表達式那就不得不說grep命令: 1.grep命令的作用:grep命令是一種強大的文本搜索工具,它能使用正則表達式搜索文本,并把匹 配的行打印出來。grep全稱是Global Regular Expression Print,表示全局正則表達式版本,它的使用權限是所有用戶。2.格式 2.命令格式 :grep [options] 3.主要參數: gre…

    2017-08-07
  • 文件查找(Find和Locate)常用用法及摩根定律趣解(重要)

    文件查找(Find和Locate)常用用法及摩根定律趣解(重要) 摩根定律: 非(A 且 B)=(非 A)或(非 B) 非(A 或 B)=(非 A)且(非 B) 這樣看起來不是很好理解下面通過一些列圖片給大家展示; 圖(1)  圖(2)  圖二中的黑色就A且B,A且B的反面三種情況(如圖1)即為(非A)或(非B)。因此…

    Linux干貨 2016-08-21
  • 文件權限

    文件權限 文件屬性       rw-r–r–表示文件權限     Root 表示文件所有者     Root 表示文件所屬組     0 表示文件的大小 …

    Linux干貨 2016-08-11
欧美性久久久久