How to use regular expressions in Shell if
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!