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
下一篇 2016-08-15

相關推薦

  • LINUX下用戶管理命令簡述

    LINUX下用戶管理命令簡述 添加用戶并設置密碼 useradd [用戶名] 創建用戶 [root@localhost ~]# useradd jack [root@localhost ~]# cat /etc/shadow | grep jack jack:!!:17257:0:99999:7::: passwd [用戶名] 設置密碼 [root@loca…

    Linux干貨 2017-04-05
  • http

    http http:hyper text transfer protocol, 應用層協議, 80/tcp, 文本協議 html:hyper text mark language, 是一種編程語言,超文本標記語言; html格式示例: <html> <head> <title>TITLE</title> &lt…

    Linux干貨 2017-06-04
  • 文件的查找作業

    1、查找/var目錄下屬主為root,且屬組為mail的所有文件 2、查找/var目錄下不屬于root、lp、gdm的、所有文件 3、查找/var目錄下最近一周內其內容修改過,同時屬主不為root,也不是postfix的文件 4、查找當前系統上沒有屬主或屬組,且最近一個周內曾被訪問過的文件 5、查找/etc目錄下大于1M且類型為普通文件的所有文件 6、查找/…

    Linux干貨 2016-08-15
  • 第一周-Linux Basic

        本文主要介紹了計算機的組成及功能、Linux的發行版、Linux哲學思想、一些基本命令、命令幫助的獲取、文件系統層級結構標準。 一、計算機的組成及功能     計算機由硬件和軟件兩大類組成     硬件   &n…

    Linux干貨 2016-11-28
  • SRE管理職責簡介

    讀書筆記 摘要 SRE是Site Reliability Engineer的簡稱,從名字可以看出Google的SRE不只是做Operation方面的工作,更多是保障整個Google服務的穩定性。 SRE管理職責簡介 監控系統 警報 工單 日志 應急事件處理 變更管理 預測需求和規劃容量 資源部署 小結 監控系統 監控系統是 SRE 團隊監控服務質量…

    Linux干貨 2017-04-03
  • 馬哥教育網絡班20期+第4周課程練習

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 [root@localhost ~]# cp -r /etc/skel/ /home/tuser1 [root@localhost ~]# chmod&n…

    Linux干貨 2016-07-04
欧美性久久久久