Regular expressions in Shell if using the detailed _linux shell

Source: Internet
Author: User

Because the work needs to validate the data submitted by the user, this is a simple date validation example, you need to know the students can refer to.

The IF statement in the shell needs to make a regular judgment and check the grammatical records.

Datepattern= "^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$"
if [["$STARTDATE" =~ $DATEPATTERN]] && [[$ENDDATE =~ $ Datepattern]]; Then:
else
echo "date format is invalid!"
Exit;
Fi

Regular expressions that are commonly used

if [[$file ' =~ ' start]]
or if [[' $file ' =~ ' start ']]

Example:

#!/usr/bin/ksh
file= "10start11.s"
if [["$file" =~ "Start"]]
then
echo "Success"
else
echo "Failed"
fi

only bash supports [[

Ksh should not support regular in test, use awk grep sed and other tools to implement it

Flag= ' echo $file |awk '/start/'
if ["$flag" = ""];then
Echo "Success"
else
echo "failed"
fi

The Bourne Shell's if statement is the same as most programming languages-the detection condition is true, and if the condition is true, the shell executes the code block specified by the IF statement, and if the condition is false, the shell skips the if code block and continues executing the code.

Syntax for an IF statement:

if[judgment conditions]
then
command1
command2
... Last_command
fi
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.