Shell simple Use (-) judgment

Source: Internet
Author: User

Take a few simple examples of the judgments involved

1. Comparison of file-related judgments

#!/bin/Bashif[$#-lt1 ]     Then    Echo "No file name"Exit1elif[-D $1 ]     Then    Echo$1is directoryelif[-B $1 ]     Then         Echo$1is block deviceelif[-H $1 ]     Then        Echo$1Is linkfileelif[-C $1 ]     Then        Echo$1is character deviceelif[-F $1 ]     Then        Echo$1is normalfileElse     Echo "Don ' t recognize $"fiif[-R $1 ]     Then        Echo$1Is can readfiif[ -W$1 ]     Then         Echo$1IS canWritefiif[-X $1 ]     Then        Echo$1Is can excutefiif[-U $1 ]     Then        Echo$1Has SUID attributionfiif[-G $1 ]     Then        Echo$1Has SGID attributionfiif[-K $1 ]     Then        Echo$1Has Sticky bit attributionfiif[-S $1 ]     Then        Echo$1is a XXXfiExit0

The results of the implementation are as follows:

2. Number-related judgments

A.

#!/bin/Bashif[" $"-eq" $"]; Then# if[" $"-ne" $"]; Then    Echo$1Equal $2fiif[" $"-lt" $"]; Then# if[" $"-le" $"]; Then    Echo$1  Lessthan $2fiif[" $"-gt" $"]; Then# if[" $"-ge" $"]; Then    Echo$1Grate Then$2fi

B.

#!/bin/Bashread-P"Input The Age:" Age Case$ageinch    [0-9]| [1][0-2])        Echo " Child"        ;; [1][0-9])        Echo " Young"        ;; [2-5][0-9] )        Echo "Adult"        ;; [6-9][0-9])            Echo " Old"        ;; *    )        Echo "Not people"        ;;EsacExit0

The results of the implementation are as follows:

B.

#!/bin/Bash
Read-P"Iuput your Birthday (mmdd.ex> 0709):"Birnow=`Date+%m%d '
if["$bir"=="$now"]; Then    Echo "Happy Birthday!!!"elif["$bir"-gt"$now"]; Then Year=`Date+%Y ' Total_d=$(($((`Date--Date="$year $bir"+%s '-'Date+%s '))/ -/ -/ -))Echo "Your birthday'll be $total _d later"Else Year=$((`Date+%y ' +1)) Total_d=$(($((`Date--Date="$year $bir"+%s '-'Date+%s '))/ -/ -/ -))Echo "Your birthday'll be $total _d later"fi

The results of the implementation are as follows:

3. String comparisons

#!/bin/Bashif[" $"==" $"]; Then# if[[" $"==" $"]]; Then    Echo$1Equal $2fiif[" $"!=" $"]; Then    Echo$1No equal $2fiif[[" $">" $"]]; Then# if[" $"\>" $"]; Then    Echo$1Grate Then$2fiif[[" $"<" $"]]; Then# if[" $"\<" $"]; Then    Echo$1  Less  Then$2fiif[-Z" $"]; Then    Echo$1is emptyfiif[-N" $"]; Then    Echo$1is no emptyfiif[[" $"==1* ]]; Then    Echo$1=="1*" 1111111111fiif[[" $"=="1*"]]; Then    Echo$1== \"1*\ "2222222222fi

The results of the implementation are as follows:

Two times comparison of 1*, pattern matching and full string matching, respectively

Shell simple Use (-) judgment

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.