Linux隨筆:簡單腳本練習-1

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

#!/bin/bash
[ $# -lt 1 ] && { echo "Usage: ./argsnum.sh xxx  Provide at least 1 argument!!!"; exit; }
space_line=`grep -E "^[[:space:]]*$" $1 |wc -l`
echo "Number of sapce lines/line are/is ;${space_line}"

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

#!/bin/bash
[ $# -lt 1 ] && { echo "Usage: ./hostping.sh IP"; exit; }
ping -c4 -W2 $1 > /dev/null && echo "$1 can be connected!" || echo "$1 can't be connected!"

3、編寫腳本 checkdisk.sh,檢查磁盤分區(qū)空間和inode使用率,如果超過80%,就發(fā)廣播警告空間將滿

#!/bin/bash
#********************************************************************
#Author: wuxiangbupt
#Date: 2021-12-09 15:10
#FileName: checkdisk.sh
#Description:Shell Script
#********************************************************************

warning=80
disk_usage=`df |grep -E "/dev/sd" |tr -s ' ' % |cut -d% -f5 |sort -rn |head -n1`
inode_usage=`df -i |grep -E "/dev/sd" |tr -s ' ' % |cut -d% -f5 |sort -rn |head -n1`
[ ${disk_usage} -gt ${warning} -o ${inode_usage} -gt ${warning} ] &&
echo -e "\E[1;31m DISK USED: ${disk_usage}%, INODE USED: ${inode_usage}% WILL BE FULL SOON!!! \E[0m" ||
echo -e "\E[1;32m DISK USED: ${disk_usage}%, INODE USED: ${inode_usage}% SAFE USE! \E[0m"

驗證:

[root@centos8 shell-excercises]# dd if=/dev/zero of=/boot/img.txt bs=1M count=700
[root@centos8 shell-excercises]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        948M     0  948M   0% /dev
tmpfs           976M     0  976M   0% /dev/shm
tmpfs           976M  9.5M  966M   1% /run
tmpfs           976M     0  976M   0% /sys/fs/cgroup
/dev/sda2        94G  5.8G   88G   7% /
/dev/sda5        47G  365M   47G   1% /data
/dev/sda1       947M  922M   26M  98% /boot
tmpfs           196M  3.5M  192M   2% /run/user/0
[root@centos8 shell-excercises]# ./checkdisk.sh
 DISK USED: 98%, INODE USED: 1% WILL BE FULL SOON!!!
[root@centos8 shell-excercises]# rm -f /boot/img.txt
[root@centos8 shell-excercises]# ./checkdisk.sh
 DISK USED: 24%, INODE USED: 1% SAFE USE!

4、編寫腳本 per.sh,判斷當前用戶對指定參數文件,是否不可讀并且不可寫

#!/bin/bash
[ ! -r $1 -a ! -w $1 ] && echo "File $1 can't be read or write!" || {
echo "File $1 can be r or w!"; }

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

#!/bin/bash
#********************************************************************
#Author: wuxiangbupt
#Date: 2021-12-08 09:42
#FileName: excute.sh
#Description:Shell Script
#********************************************************************
[[ -f $1 &&  "$1" =~ \.sh$  ]] && { chmod a+x $1; echo -e "\E[1,32m Add x for all user!\E[0m"; } || echo "No such file or not sh file"

6、編寫腳本 nologin.sh和 login.sh,實現禁止和允許普通用戶登錄系統

#!/bin/bash
#********************************************************************
#Author: wuxiangbupt
#Date: 2021-12-08 11:18
#FileName: login.sh
#Description:Shell Script
#********************************************************************
id $1 &> /dev/null || { echo "no user $1!"; exit; }
user_id=`id $1 |grep -oE "uid=[0-9]+" |cut -d= -f2`
user_shell=`cat /etc/passwd |grep $1|cut -d: -f7`
[ ${user_id} -ge 1000 -a ${user_shell} != "/bin/bash" ] && {
echo -e "\E[1;32m Set user $1 shell from ${user_shell} to /bin/bash...\E[0m";
usermod -s /bin/bash $1 &> /dev/null;
echo -e "\E[1;32m Done...\E[0m"; } || {
echo -e "\E[1;31m Shell already in login or not valid user...\E[0m"
}
#!/bin/bash
#********************************************************************
#Author: wuxiangbupt
#Date: 2021-12-08 11:18
#FileName:nologin.sh
#Description:Shell Script
#********************************************************************

id $1 &> /dev/null || { echo "no user $1!"; exit; }
user_id=`id $1 |grep -oE "uid=[0-9]+" |cut -d= -f2`
user_shell=`cat /etc/passwd |grep $1|cut -d: -f7`
[ ${user_id} -ge 1000 -a ${user_shell} != "/sbin/nologin" ] && {
echo -e "\E[1;32m Set user $1 shell from ${user_shell} to /sbin/nologin...\E[0m";
usermod -s /sbin/nologin $1 &> /dev/null;
echo -e "\E[1;32m Done...\E[0m"; } || {
echo -e "\E[1;31mUser shell already in nologin or not valid user...\E[0m"
}
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 1、編寫腳本,統計/etc、/usr、/var目錄中有多少個一級子目錄和文件 2、自動生成腳本 3、編寫腳本sum...
    JevonWei閱讀 1,165評論 0 1
  • 訪問博客查看 本文[https://hwcoder.top/Linux-Note-1] 排版更美觀ヾ(?ω?`)o...
    Hwcoder閱讀 1,024評論 0 1
  • bash中變量的種類 局部變量:生效范圍為當前shell進程,對當前shell之外的其它shell進程,包括當前s...
    毛利卷卷發(fā)閱讀 575評論 0 1
  • Linux習慣問題: 在vim編輯時,按了ctrl + s后,再按ctrl + q就可以繼續(xù)執(zhí)行了。ctrl + ...
    光著腳的鞋閱讀 4,704評論 0 16
  • 【腳本1】打印形狀 打印等腰三角形、直角三角形、倒直角三角形、菱形 【腳本2】截取字符串 現有一個字符串如下: h...
    學無止境_9b65閱讀 555評論 0 1

友情鏈接更多精彩內容