Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions in Shell if

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use regular expressions in Shell if. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!

Because the work needs to verify the data submitted by the user, this is a simple example of date regular verification, there is a need to understand the students can refer to.

In the case of the shell, if the statement needs to be a regular judgment, check the syntax record.

DATEPATTERN="^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$"if [[ "$STARTDATE" =~ $DATEPATTERN ]] && [[ $ENDDATE =~ $DATEPATTERN ]]; then :elseecho "date format is invalid! "exit;fi

Regular Expressions

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

Examples:

#!/ usr/bin/kshfile="10start11.s"if [[ "$file" =~ "start" ]]thenecho "success"elseecho "failed"fi

Only bash supports [[

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

flag=`echo $file |awk '/start/'`if [ "$flag" = "" ];thenecho "success"elseecho "failed"fi

Bourne Shell if statements are the same as most programming languages-checking whether a condition is true, and if the condition is true, the shell executes the block of code specified by the if statement, and if the condition is false, the shell skips the block of if code and continues to execute the code that follows.

Syntax of the if statement:

if[judgment condition]thencommand 1 command 2...... last_commandfi The above is "How to use regular expressions in Shell if" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report