if case語句練習

 1、 寫一個腳本/root/bin/createuser.sh,實現如下功能:使用一個用戶名做為參數,如果指定參數的用戶存在,就顯示其存在,否則添加之;顯示添加的用戶的id號等信息

[root@localhost bin]# cat createuser.sh
#!/bin/bash
# Date: 2016.8.12
# Author: cyh
# Description: Determine whether a user exists
 
user=`cut -d: -f1 /etc/passwd |grep -o "$1"`
[ $# -ne 1 ] && exit 1
if [[ -n $user ]]; then
         echo "User already exists!"
         echo "The user information: `grep "^$1\>" /etc/passwd |cut -d: -f 1,3`"
else
         useradd $1 > /dev/null
         echo "The user has been created!"
         echo "The user information: `grep $1 /etc/passwd |cut -d: -f 1,3`"
fi

 

腳本測試

[root@localhost bin]# getent passwd tom
[root@localhost bin]# createuser.sh tom
The user has been created!
The user information: tom:2028
[root@localhost bin]# getent passwd tom
tom:x:2028:2033::/home/tom:/bin/bash
[root@localhost bin]#
[root@localhost bin]# getent passwd zhangsan
zhangsan:x:2022:2024::/home/zhangsan:/bin/bash
[root@localhost bin]# createuser.sh zhangsan
User already exists!
The user information: zhangsan:2022

 

2、寫一個腳本/root/bin/yesorno.sh,提示用戶輸入yesno,并判斷用戶輸入的是yes還是no,或是其它信息

[root@localhost bin]# cat yesorno.sh
#!/bin/bash
# Description: Determine the user input characters(yes or no).
 
read -t5 -p "Please input yes or no: "  ans
[ -z $ans ] && echo "You have no input, script has quit" && exit 10
case $ans in
[yY][eE][sS]|y|Y)
         echo "Input is yes!"
         ;;
[nN][oO]|n|N)
         echo "Input is no!"
         ;;
*)
         echo "Input error, please enter again."
         ;;
esac

腳本測試

[root@localhost bin]# yesorno.sh
Please input yes or no: y
Input is yes!
[root@localhost bin]# yesorno.sh
Please input yes or no: yEs
Input is yes!
[root@localhost bin]# yesorno.sh
Please input yes or no: nO
Input is no!
[root@localhost bin]# yesorno.sh
Please input yes or no: n
Input is no!
[root@localhost bin]# yesorno.sh
Please input yes or no: tt
Input error, please enter again.

 

3、寫一個腳本/root/bin/filetype.sh,判斷用戶輸入文件路徑,顯示其文件類型(普通,目錄,鏈接,其它文件類型)

[root@localhost bin]# cat filetype.sh
#!/bin/bash
# Author: cyh
# Date: 2016.8.15
# Description: Input a file, and determine the type of the file
 
if [ $# -ge 1 ]; then
         if [ -z $1  ]; then
                   echo "Your input is empty"
         elif [ -d $1 ]; then
                   echo "this is a directory."
         elif [ -h $1 ]; then
                   echo "this is a link file."
         elif [ -f $1 ]; then
                   echo "this is a common file."
         elif [ -c $1 ]; then
                   echo "this is a character device file."
         elif [ -b $1 ]; then
                   echo "this is a block device file."
         elif [ -s $1 ]; then
                   echo "this is a socket file."
         elif [ -p $1 ]; then
                   echo "this is a pipe file."
         else
                   echo "The input character is empty or file does not exist,please specify again."
         fi
else
         read -t 10 -p "Please input file name: " file
         [[  ! -e $file || -z $file ]] && echo "The input is empty or file does not exist,please specify again."  && exit 
         type=`ls -ld $file 2> /dev/null | cut -c1`
         case $type in
         d)
                   echo "this is a directory."
                   ;;
         -)
                   echo "this is a common file."
                   ;;
         l)
                   echo "this is a link file."
                   ;;
         s)
                   echo "this is a socket file."
                   ;;
         c)
                   echo "this is a character device file."
                   ;;
         b)
                   echo "this is a block device file."
                   ;;
         p)
                   echo "this is a pipe file."
                   ;;
         *)
                   echo "this not is file."
         esac
fi

 

腳本測試

[root@localhost bin]# filetype.sh dfdfd
The input character is empty or file does not exist,please specify again.
[root@localhost bin]# filetype.sh /var
this is a directory.
[root@localhost bin]# filetype.sh /var/log/
this is a directory.
[root@localhost bin]# filetype.sh /var/log/messages
this is a common file.
[root@localhost bin]# filetype.sh /tmp/cat
this is a link file.
[root@localhost bin]# filetype.sh /tmp/hogsuspend
this is a pipe file.
[root@localhost bin]# filetype.sh /var/log/messages21
The input character is empty or file does not exist,please specify again.
 
[root@localhost bin]# filetype.sh
Please input file name: dfsdfsfq2
The input character is empty or file does not exist,please specify again.
[root@localhost bin]# filetype.sh
Please input file name: /etc/fstab
this is a common file.
[root@localhost bin]# filetype.sh
Please input file name: /tmp/cat
this is a link file.
[root@localhost bin]# filetype.sh
Please input file name: /tmp/hogsuspend
this is a pipe file.

 

4、寫一個腳本/root/bin/checkint.sh,判斷用戶輸入的參數是否為正整數

[root@localhost bin]# cat checkint.sh
#!/bin/bash
#
 
read -p "Please input an integer:" integer
#[ $integer -lt 0 ] && echo "Please enter at least one value" && exit 10
if [[ $integer =~ ^0*[0-9][0-9]*$ ]]; then
         echo "this is an integer"
else
         echo "this not an integer"
fi

腳本測試

[root@localhost bin]# checkint.sh
Please input an integer:12
this is an integer
[root@localhost bin]# checkint.sh
Please input an integer:3.14
this not an integer
[root@localhost bin]# checkint.sh
Please input an integer:df
this not an integer
[root@localhost bin]# checkint.sh
Please input an integer:09   
this is an integer
[root@localhost bin]# checkint.sh
Please input an integer:9f
this not an integer

 

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

(0)
cyh5217cyh5217
上一篇 2016-08-15 16:55
下一篇 2016-08-15 21:45

相關推薦

  • 15磁盤管理

    在系統中,常見的硬盤接口有分兩類:并行的和串行的 并行: IDE: 133MB/s SCSI: 640MB/s 串行: SATA: 6Gbps SAS: 6Gbps USB: 480MB/s 存儲設備的設備文件命名方式大致為:/dev/DEV_FILE IDE: /dev/hd#  #–>0,1,2,3 SCSI,SATA,SAS…

    Linux干貨 2016-12-02
  • yum 倉庫

    Yum 倉庫     yum源就是一個軟件集合地,你只需要搜索并安裝你想要的軟件,它會幫你解決大部分軟件的依賴問題。本地源比如說光盤里面一般會附帶一些軟件,這個時候就可以把光盤當成本地源來安裝軟件。網絡源比如說aliyun的鏡像網站,這就屬于網絡源,可以通過互聯網把軟件下載下來并安裝。   yum 倉庫 &n…

    2017-06-24
  • vim的簡單應用

    vim的簡單應用         之前我們學習過Linux的思想,其中有一條就是一切皆文本,所以在這里大部分配置文件都是文本模式存在的,那么使用簡單的文字編輯器就可以修改配置了,之前我們學習過nano文本編輯器,但是Vim會比nano使用的更為方便,也更強大。 基本現在所有的Li…

    2017-06-17
  • 馬哥教育網絡21期+第三周作業博客

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@localhost ~]# who | cut -d' ' -f1 | sort -u 2、取出最后登錄到當前系統的用戶的相關信息。 [ro…

    Linux干貨 2016-08-01
  • 09yum的使用以及簡單配置

    YUM: yellowdog update modifier ,rpm的前端程序,用來解決軟件包相關依賴性,可以在多個庫之間定位軟件包。 yum repository:yum repo,存儲了眾多RPM包,以及包相關的元數據文件,放置于特定目錄repodata下。 yum 訪問的文件服務器主要有三種,ftp,http,file。 yum客戶端配置文件: 【/…

    Linux干貨 2016-11-04
  • N22-第6周作業-冥界之王

    請詳細總結vim編輯器的使用并完成以下練習題1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;    [root@CentOS6 /]# cp /etc/rc.d/rc.sysinit /tmp…

    Linux干貨 2016-09-19
欧美性久久久久