Comparison of if judgments of the shell

Source: Internet
Author: User

The IF function in the shell is beyond doubt that every 80% of the shells will be applied to if judgment, then as a novice I should be the whole, I heard a morning class today, and then the morning's things summed up a script, and then to the people to talk about

#!/bin/bash
##############################################################
# File Name:bbjj.sh
# version:v1.0
# Author:ls
# organization:http://shuai12138.blog.51cto.com/
# Created time:2017-03-06 14:04:05
# Description:
##############################################################
Read-p "Pleace input:"-T 2-n 1 x
echo ""
Read-p "Pleace input:"-t 2-n 1 y
echo "" >/dev/null
If [-N $x-N $y]
Then
if [["$x $y" =~ ^[0-9]+$]
Then
If [$x-gt $y]
Then
echo "$x > $y"
elif [$x-eq $y]
Then
echo "$x = $y"
Else
echo "$x < $y"
Fi
Else
echo "You must input number"
Fi
Else
echo "Error:you must input"
Fi

First, the first line of instructions: read-p "Pleace input:"-T 2-n 1 x

Read-p to script Interactive input "Pleace input" is the prompt message when executing the shell. The-T 2 means that after 2 seconds you do not type, then you exit. -N 1 means that I only restrict you to enter one character.

Because I want to compare the size of two numbers so I use 2 read, but why do I have to add an echo in the middle "?" Because if you don't add that read and continue to let you enter $y after you've entered the $x line, it's all about beauty.

echo "" >/dev/null
Why is the output empty I said, but why should I be directed to the "black hole", because if I do not enter anything then my script will have a message, I do not want to see him, so I directed him to the "black hole".

If [-N $x-N $y]
Then

Fi

This is an if judgment,-N is not empty,-a conditional statement (both sides are satisfied only), this sentence is judged only you two times the input is not empty, then will be executed, otherwise.

if [["$x $y" =~ ^[0-9]+$]
Then

Fi

This is a digital judgment, judging whether you are typing is a number, because if you enter the letter, then the system may be twos code to compare the meaningless. So I have to decide if it's a number. (here, in particular, I've put $x$x all together for comparison.) Otherwise it will be judged, waste of memory)

If [$x-gt $y]
Then
echo "$x > $y"
elif [$x-eq $y]
Then
echo "$x = $y"
Else
echo "$x < $y"
Fi

This is the judgment of the numbers. I'm going to judge the two numbers I've entered. -eq equals-gt greater than-lt less than-ge greater than or equal to-le less than equals.

This article is from my study blog, so be sure to keep this source http://shuai12138.blog.51cto.com/10118203/1901603

Comparison of if judgments of the shell

Related Article

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.