Examples of If Case statement usage in Linux

Source: Internet
Author: User
Tags case statement


If and case two conditional statements in the shell

1. If Syntax 1:
If
Conditions
Then
Commands
Else
Commands
Fi


Syntax 2 for if:
Syntax: If condition
Then
Commands
Elif conditions
Then
Commands
Elif conditions
Then
Commands
....
Else
Commands
Fi

Description: Read is a row for reading user input

Some descriptions of IF statements

First: If condition 1
Then
Commands
Elif Condition 2
Then
Commands
Elif Condition 3
Then
Commands
....
Else
Commands
Fi

The conditions in the 1,2,3 etc, are equal status, that is, the equivalent of the condition of 1 is not satisfied, then see conditions 2, Conditions 1, 2 are not satisfied, see conditions 3 to push down (-a for and, but can not be replaced with &&, otherwise will be an error,-O for or meaning).


Case Condition Statement:
Syntax: Case conditions in
xxx
commands;;
xxx
commands;;
xxx
commands;;
Esac

Description: This ESAC is the end of the case, want to if...fi the same,
Pay attention to commands;; the ";;" cannot be lost.

Example
1, write a script/root/bin/createuser.sh, to achieve the following functions: Using a username as a parameter, if the user of the specified parameter exists, it is added; Displays the ID number of the added user

[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 "$"
[$#-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 $ >/dev/null
echo "The user has been created!"
echo "The User information: ' grep $1/etc/passwd |cut-d:-F 1,3 '"
Fi

Script Test

[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, write a script/root/bin/yesorno.sh, prompts the user to enter Yes or no, and to determine whether the user entered Yes or no, or other information

[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

Script Test

[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, write a script/root/bin/filetype.sh, judge the user input file path, display its file type (normal, directory, link, other file type)

[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 $]; Then
echo "Your input is empty"
elif [D $]; Then
echo "This is a directory."
elif [-H $]; Then
echo "This is a link file."
Elif [F $]; Then
echo "This is a common file."
Elif [C $]; Then
echo "This is a character device file."
Elif [B $]; Then
echo "This are block device file."
Elif [S $]; Then
echo "This is a socket file."
elif [-P $]; 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 are block device file."
;;
P
echo "This is a pipe file."
;;
*)
echo "This isn't is file."
Esac
Fi

Script Test

[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 directory.
[Root@localhost bin]# filetype.sh/var/log/
This is 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, write a script/root/bin/checkint.sh to determine whether the user input parameter is a positive integer

[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 isn't an integer"
Fi

Script Test

[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 isn't an integer
[Root@localhost bin]# checkint.sh
Please input an INTEGER:DF
This isn't 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 isn't an integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.