Shell Record-shell Script Basics (iii)

Source: Internet
Author: User
Tags case statement

The basic control statement of the if...fi statement, which allows the shell to conditionally make a decision and execute the statement.

Grammar
If[]thenStatement(s)ifistruefi    

Shell expression evaluation. If the result value is true, the given statement is executed. If expression is false, no statement will not be executed. Most of the time, comparison operator decisions are used.

Focus on the space between the parentheses and the expression. This space is mandatory, otherwise the syntax is wrong.

If expression is a shell command, then it is assumed to be so if it executes after it returns 0. Returns true if it is a Boolean expression.

Example:
#!/bin/sha=10b=20if [< Span class= "PLN" > $a == $b ]then echo  "A is equal to B" fiif [  $a != $b ]then echo  "A is not equal to B" fi                

This will produce the following results:

Is isn't equal to B

The if...else...fi statement is a control statement that allows the next table to execute a statement Shell to make a decision between two choices in a more controllable manner.

Grammar
If [ expression ]then statement (s )  to be executed if expression is trueelse  statement (s to be executed if expression is not truefi   

Shell expression evaluation. If the resulting value is true, the given statement (s) is executed. If the expression is false, the statement will not be executed.

Example:

If we take the above example, then it can be used in a better way using the If...else statement as follows:

#!/bin/sha=tenb=if[= =]then' A is equal to B 'else' A is Not equal to B "fi              

This will produce the following results:

Is isn't equal to B 

The if...elif...fi statement is a control statement in advance of a level, in the form of several conditions that allow the Shell to make the right decision.

Grammar
If [Expression1 ]Then Statement(S)To be executedIfExpression1 Is TrueElif [Expression2 ]Then Statement(S)To be executedIfExpression2 Is TrueElif [Expression3 ]then  statement (s to be executed if expression 3  is trueelse statement (s)  to be executed if no expression is truefi                

There's nothing special about this piece of code. This is just a series, if statement is part of each statement else clause. The following statement is the actual case based on execution, and if the condition is not ture, the else block is executed.

Example:
#!/bin/shA=10B=20If [$a==$b]then echo  "A is equal to B" elif [ $a - gt $b ]then echo  "A is greater than B" elif [< Span class= "PLN" > $a -lt $b ]then echo  "A is less than B" else echo  "None of the condition met" fi               

This will produce the following results:

is less than B

You can use multiple IF...ELIF statements to perform multiple branches. However, this is not always the best solution, especially when all branches depend on the value of a single variable.

Shell supports CASE...ESAC statement processing it is in this case that it is more efficient than the IF...ELIF statement.

Grammar

The basic syntax of the CASE...ESAC statement is to give an expression evaluation and several different statements to execute an expression-based value.

The interpreter checks the value of the expression in each case until a match is found. If there is no match, it is used by default.

CaseWordInchPattern1) statement (s to be Executed if Pattern1 matches ;;  Pattern2)  statements)  to be executed if;  Pattern3)  statements)  to be executed if; esac             

Here the string characters are compared in each pattern until a match is found. Executes the statement-matching pattern. If no match is found, no action is taken in case the declaration exits.

There is no maximum number of patterns, but the minimum is one.

When the statement part executes, the order; Indicates that the program flow jumps to the end of the entire case statement. Similar to the C programming language break.

Example:
#!/bin/shfruit= "Kiwi" case< Span class= "PLN" >  "$FRUIT"  in "Apple" )  echo  "apple pie is quite tasty."  ;   "banana" )  echo  "I like Banana nut bread. "  ;   "Kiwi" )  echo  "New Zealand is famous for Kiwi. "  ; esac             

This will produce the following results:

New Zealand is for Kiwi.    

The case statement is a good command line parameter to calculate as follows:

#!/bin/shOption="${1}" Case${Option} Inch -F)FILE= "${2}"  echo  "File name is $FILE"  ;;  -d)  DIR = "${2}"  echo  "Dir name is $DIR"  ;  *)  echo  "' BaseName ${0} ': Usage: [-f file] | [-D directory] " exit 1 Span class= "com" ># Command to come out of the program with status 1 ;;  esac             

The following is an example of running this program:

$./Test.Shtest.Sh:Usage: [ -f filename] | [ -D Directory]$ ./test.-f index.../test.-f index.file name is Index.< Span class= "PLN" >htm$ ./test. SH -d unixdir name is< Span class= "PLN" > unix$              

Shell Record-shell Script Basics (iii)

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.