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
下一篇 2016-11-22

相關推薦

  • 雖千萬人吾往矣

    哈佛有句名言 “當你覺得為時已晚的時候,恰恰是最早的時候”。這句話給了我雖千萬人吾往矣的勇氣。 之前那份工作懶散 安逸 。當我下定決心從那種環境中走出來的時候還是下了很大的決心。陌生的行業 陌生的環境 陌生的前途。我抱著對這份行業無限的憧憬來到了北京。   作為一個南方人的確有點不適合北方這黃沙漫漫的城市,剛來的一兩天不停的打噴嚏,可能這就是北京歡…

    Linux干貨 2018-03-26
  • 運維的危險命令,用了必死(1)

    Linux命令行佷有用、很高效,也很有趣,但有時候也很危險,尤其是在你不確定你自己在正在做什么時候。這篇文章并不打算引來你對Linux或linux 命令行的憤怒。我們只是想讓你意識到在你運行某些命令時應該三思而后行。(譯注:當然,以下命令通常都是在root權限下才能將愚蠢發揮到無可救藥;在普通用戶身份下,破壞的只是自己的一畝三分地。)

    2017-11-16
  • 第二周博客作業

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示? cat(concatenate)#從頭開始看     文本文件查看工具 SYNOPSIS:     cat [OPTION]… [FILE]… -A 輸出行最后加上$號 -n 輸出行號 例…

    Linux干貨 2016-12-12
  • 馬哥網絡21-第5周作業

    1、顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行; [root@localhost proc]# grep "^[[:space:]]\{1,\}" /boot/grub/grub.conf root (hd0,0) kernel /vmlinuz-2…

    Linux干貨 2016-08-08
  • 正則表達式

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其他用戶都沒有任何訪問權限 [root@localhost ~]#? mkdir /home/tuser1======>創建/home/tuser1目錄 [root@localhost ~]#? ?cp? -a? /etc/skel/? ?/hom…

    2017-10-10
  • N26-第二周博客作業

    1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相 關示例演示。 文件管理類命令:mkdir rmdir cp rm mv ①mkdir命令:創建目錄 格式:mkdir [OPTION]… DIRECTORY… option -m, –mode=MODE -p, –paren…

    Linux干貨 2017-02-02

評論列表(1條)

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

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

欧美性久久久久