bash腳本編程練習:判斷、循環

 

1、寫一個腳本,判斷當前系統上所有用戶的shell是否為可登錄shell(即用戶的shell不是/sbin/nologin);分別這兩類用戶的個數;通過字符串比較來實現;

#!/bin/bash
#
declare -i sumlogin=0
declare -i sumnologin=0
for i in $(cut /etc/passwd -d: -f7);do
if [[ "$i" == "/sbin/nologin" ]];then
let sumnologin++
else
let sumlogin++
fi
done
echo "totle nologin user $sumnologin\ntotle loging user $sumlogin"

執行結果:
~]# bash loginuser.sh
totle nologin user 23
totle loging user 9

2、寫一個腳本 (1) 獲取當前主機的主機名,保存于hostname變量中; (2) 判斷此變量的值是否為localhost,如果是,則將當前主機名修改為www.magedu.com; (3) 否則,則顯示當前主機名;

#!/bin/sh
#
hostname=$(hostname)
if [[ "$hostname" == "localhost.localdomain" ]];then
hostname www.void.com
echo "hostname change to $(hostname) ok"
else
echo "hostname is $hostname"
fi

執行結果:
a:hostname=localhost.localdomain
~]# bash hostname.sh
hostname change to www.void.com ok

b:hostname=test
~]# bash hostname.sh
hostname is test

3、寫一個腳本,完成如下功能 (1) 傳遞一個磁盤設備文件路徑給腳本,判斷此設備是否存在; (2) 如果存在,則顯示此設備上的所有分區信息;

#!/bin/bash
#
if [ $# -lt 1 ];then
echo "At least one arg!"
exit 1
else
if fdisk -l $1 &>/dev/null;then
echo -e "$(fdisk -l $1)"
else
echo "The device is not exist!"
fi
fi

執行結果:
a:參數不存在
 ~]# bash device.sh
At least one arg!

b:設備不存在
~]# bash device.sh /dev/sdb
The device is not exist!

c:設備存在
~]# bash device.sh /dev/sda

磁盤 /dev/sda:21.5 GB, 21474836480 字節,41943040 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x0004bb94

   設備 Boot  Start End  Blocks   Id  System
/dev/sda1   *2048 1026047  512000   83  Linux
/dev/sda2 10260484194303920458496   8e  Linux LVM

4、寫一個腳本,完成如下功能 腳本能夠接受一個參數; (1) 如果參數1為quit,則顯示退出腳本, 并執行正常退出; (2) 如果參數1為yes,則顯示繼續執行腳本; (3) 否則,參數1為其它任意值,均執行非正常退出;

#!/bin/bash
#
read -p "Do you want to executing the script continue?[quit or yes]" arg
case $arg in
quit)
echo "script is quit"
exit 0
;;
yes)
echo "continue!"
;;
*)
echo "what!!!"
exit 2
;;
esac

執行結果:
~]# bash casetest.sh
Do you want to executing the script continue?[quit or yes]quit
script is quit
~]# echo $?
0
~]# bash casetest.sh
Do you want to executing the script continue?[quit or yes]yes
continue!
~]# echo $?
0
~]# bash casetest.sh
Do you want to executing the script continue?[quit or yes]aaaaaa
what!!!
~]# echo $?
2

5、寫一個腳本,完成如下功能 傳遞一個參數給腳本,此參數為gzip、bzip2或者xz三者之一; (1) 如果參數1的值為gzip,則使用tar和gzip歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.gz; (2) 如果參數1的值為bzip2,則使用tar和bzip2歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.bz2; (3) 如果參數1的值為xz,則使用tar和xz歸檔壓縮/etc目錄至/backups目錄中,并命名為/backups/etc-20160613.tar.xz; (4) 其它任意值,則顯示錯誤壓縮工具,并執行非正常退出;

#!/bin/bash
#
time=$(date +%Y%m%d)
read -p "Please choose one compress Tool[gzip/bzip2/xz]" arg
case $arg in
gzip)
tar -zcf /backups/etc-$time.tar.gz /etc &>/dev/null
echo -e  "compress etc-$time.tar.gz ok"
;;
bzip2)
tar -jcf /backups/etc-$time.tar.bz2 /etc &>/dev/null
echo -e  "compress etc-$time.tar.bz2 ok"
;;
xz)
tar -Jcf /backups/etc-$time.tar.xz /etc &>/dev/null
echo -e  "compress etc-$time.tar.xz ok"
;;
*)
echo "error compress Tool"
;;
esac

執行結果:
~]# bash tar.sh
Please choose one compress Tool[gzip/bzip2/xz]gzip
compress etc-20161121.tar.gz ok
~]# ls /backups
etc-20161121.tar.gz

~]# bash tar.sh
Please choose one compress Tool[gzip/bzip2/xz]bzip2
compress etc-20161121.tar.bz2 ok
~]# ls /backups
etc-20161121.tar.bz2  etc-20161121.tar.gz

~]# bash tar.sh
Please choose one compress Tool[gzip/bzip2/xz]xz
compress etc-20161121.tar.xz ok
~]# ls /backups
etc-20161121.tar.bz2  etc-20161121.tar.gz  etc-20161121.tar.xz

~]# bash tar.sh
Please choose one compress Tool[gzip/bzip2/xz]test
error compress Tool

6、寫一個腳本,接受一個路徑參數: (1) 如果為普通文件,則說明其可被正常訪問; (2) 如果是目錄文件,則說明可對其使用cd命令; (3) 如果為符號鏈接文件,則說明是個訪問路徑; (4) 其它為無法判斷;

#!/bin/bash
#
if [ $# -lt 1 ];then
echo "At least one arg!"
exit 1
fi

if ! [ -e $1 ];then
echo "file is not exist"
exit 2
fi

if [ -f $1 ];then
echo "this file can be access normal"
elif [ -d $1 ];then
echo "this file can be cd"
elif [ -h $1 ];then
echo "this is a link file"
else
echo "unkown"
fi

執行結果:
~]# bash file.sh
At least one arg!
~]# bash file.sh aaaa
file is not exist
~]# bash file.sh /etc
this file can be cd
~]# bash file.sh /etc/passwd
this file can be access normal

7、寫一個腳本,取得當前主機的主機名,判斷 (1) 如果主機名為空或為localhost,或為"(none)",則將其命名為mail.magedu.com; (2) 否則,顯示現有的主機名即可;

#!/bin/bash
#
hostname=$(hostname)
if [ -z "$hostname" -o "$hostName" == "localhost.localdomain" -o "$hostname" == "localhost" ];then   
hostname mail.void.com
echo "hostname has change to $(hostname)"
else
echo "$hostname"
fi

執行結果:
~]# hostname
localhost.localdomain
~]# bash changehost.sh
hostname has change to mail.void.com

~]# hostname test
~]# hostname
test
~]# bash changehost.sh
hostname is test

8、寫一腳本,接受一個用戶名為參數; (1) 如果用戶的id號為0,則顯示其為管理員; (2) 如果用戶的id號大于0且小于500, 則顯示其為系統用戶; (3) 否則,則顯示其為普通用戶;

#!/bin/bash
#
if [ $# -lt 1 ];then
echo "at lease one arg"
exit 1
fi

if ! id $1 &>/dev/null;then
echo "no such user"
else
userid=$(id -u $1)
if [ $userid -eq 0 ];then
echo "this is admin"
elif [ $userid -gt 0 ] && [ $userid -lt 200 ];then
echo "this is system user"
else
echo "this is normal user"
fi
fi

執行結果:
~]# bash readuser.sh
at lease one arg

~]# bash readuser.sh root
this is admin

~]# bash readuser.sh void
this is normal user

~]# bash readuser.sh test
no such user

~]# bash readuser.sh mail
this is system user

10、寫一個腳本,傳遞一個用戶名參數給腳本; (1) 如果用戶的id號大于等于500,且其默認shell為以sh結尾的字符串,則顯示“a user can log system.”類的字符串; (2) 否則,則顯示無法登錄系統;

#!/bin/bash
#
if [ $# -lt 1 ];then
echo "at least one arg"
exit 1
fi

if ! id $1 &>/dev/null;then
echo "no such user"
exit 2
fi

userid=$(id -u $1)
if [ $userid -ge 500 ] && grep "^$1.*sh$" /etc/passwd &>/dev/null;then
echo "a user can log system"
else   
echo "the user can't login"
fi


執行結果:
~]# useradd test1 -u 550 -s /sbin/nologin
~]# useradd test2 -u 300 -s /bin/bash
~]# useradd test3 -u 551 -s /bin/bash

~]# bash userread.sh
at least one arg

~]# bash userread.sh ddddddd
no such user

~]# bash userread.sh test1
the user can't login
~]# bash userread.sh test2
the user can't login
~]# bash userread.sh test3
a user can log system

原創文章,作者:N23-蘇州-void,如若轉載,請注明出處:http://www.www58058.com/60331

(0)
N23-蘇州-voidN23-蘇州-void
上一篇 2016-11-22 00:38
下一篇 2016-11-22 12:31

相關推薦

  • 啟動流程與內核管理

    centos6和centos5啟動流程與內核管理,啟動過程中遇到的系統不能啟動,各個階段該怎么解決

    Linux干貨 2018-01-02
  • N25第七周學習總結(CentOS 7 系統啟動流程)

    CentOs系統啟動流程 大綱 1、    什么是CentOs系統啟動流程 2、    為什么要了解CentOS系統啟動流程 3、    了解CentOS系統啟動流程對我們有什么幫助 4、    CentOs系統啟動流程詳解   …

    Linux干貨 2017-02-20
  • 用戶管理和組管理

         Linux是一個多用戶多任務的系統 ,因此學習用戶管理和組管理,對我們工作和學習有很大的幫助。用戶是你登錄這個操作系統的憑證,因此用戶的創建和修改,刪除是用戶管理的基本工作,所以你掌握這些,對你的工作有很好的幫助。 用戶賬號管理 /etc/passwd username:x: UId:gid:comment:…

    Linux干貨 2016-08-10
  • Linux之網絡管理

    Linux之網絡管理     在學習linux的過程中,Linux網絡的管理和配置中是很重要的,幾乎學習的后期都離不開網絡的概念和配置,如集群中的使用,學習好Linux網絡至關重要。     Linux網絡IP地址有兩種配置方式:靜態指定和動態分配 動態分配就是利用DHCP服務器,動態的給linux主機分配IP地址。靜…

    Linux干貨 2016-09-05
  • Linux下 文件類型不同顏色的含義

    linux 文件顏色的含義 藍色代表目錄 綠色代表可執行文件 紅色表示壓縮文件 淺藍色表示鏈接文件 灰色表示其他文件 紅色閃爍表示鏈接的文件有問題了 黃色表示設備文件 藍色文件———-目錄 白色文件———-一般性文件,…

    Linux干貨 2016-10-18
  • linux常用的文件,目錄管理命令總結和文件元數據信息含義

    常用的文件管理類命令及其具體用法 1.rmdir刪除空目錄 -p 遞歸刪除多級空目錄 這個命令不能刪除非空目錄 例如 [root@dxlcentOS ~]# rmdir /tmp/we rmdir: 刪除 “/tmp/we” 失敗: 目錄非空 [root@dxlcentOS ~]# rmdir /tmp/we2 2.mkdir創建目錄 常用選項-p 自動遞歸…

    Linux干貨 2017-10-05

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-11-30 21:21

    非常不錯,完整地看來下有腳本有輸出,展示出了扎實的腳本基本功,期待下次作業~

欧美性久久久久