Shell Process Control statement if and for case

Source: Internet
Author: User
Tags logical operators

The Linux shell has its own process Control statement, which includes a conditional statement (if), a looping statement (For,while), and a SELECT statement (case). I'll use the example below to illustrate how each statement is used.

One, Shell conditional statements (if usage)
If statement structure [IF/THEN/ELIF/ELSE/FI]

If condition test statement
Then
Action
[Elif Conditions
Action
Else
Action
]
Fi

If the condition test statement is not very clear, you can refer to the following: Linux shell logical operators, logical expressions
Shell commands, which can be split by semicolons, or by line breaks. If you want to write multiple commands in one line, you can pass the ";" Segmentation.
Such as:
[[email protected] test]# a=5;if [[A-GT 4]]; then echo ' OK '; fi;
Ok

Example: (test.sh)
#!/bin/sh
scores=40;
if [[$scores-GT 90]]; Then
echo "Very good!";
elif [[$scores-GT 80]]; Then
echo "good!";
elif [[$scores-GT 60]]; Then
echo "pass!";
Else
echo "No pass!";
Fi

The condition tests are: [[]],[],test], note: [[]] is separated from the variable by a space.

Second, the circular statement (For,while,until usage):
for cyclic use method (For/do/done)
Syntax structure:
1.for ... in statement
For variable in SEQ string
Do
Action
Done
Description: The SEQ string is separated by a space character, and each time for...in reads, it will read the value in order, giving the preceding variable.
Example (testfor.sh):
#!/bin/sh
For I in $ (seq 10); Do
echo $i;
Done
2.for (assignment; condition; op-statement))
for (assignment; condition; Operation statement)
Do
Action
Done
Example (testfor2.sh):
#!/bin/sh
For ((i=1;i<=10;i++));d o
echo $i;
Done
while cyclic use (While/do/done)
While statement structure
While conditional statement
Do
Action
Done
Example 1:
#!/bin/sh
i=10;
while [[$i-GT 5]];d o
echo $i;
((i--));
Done
Example 2: (Loop read file contents:)
#!/bin/sh
While Read Line;do
Echo $line;
Done </etc/hosts;
Until loop statements
Syntax structure:
Until conditions
Do
Action
Done
It means: Exit until the condition is met. Otherwise, the action is executed.
Example (testuntil.sh):
#!/bin/sh
a=10;
until [[$a-lt 0]];d o
echo $a;
((a));
Done
Results:
III. Shell Selection Statement (case, select usage)
Case SELECT statement Use (CASE/ESAC)
Grammatical structure
Case $arg in
Pattern | Sample) # arg in pattern or sample
;;
PATTERN1) # arg in PATTERN1
;;
*) #default
;;
Esac
Description: Pattern1 is a regular expression and can be used with the following characters:
* Any string
? Any character
[ABC] A, B, or C three characters one of them
[A-n] Any character from A to n
| Multiple options

Instance:
#!/bin/sh
Case $ in
Start | Begin
echo "Start Something"
;;
Stop | End
echo "Stop Something"
;;
*)
echo "Ignorant"
;;
Esac
Running Result: ======================

testcase.sh start
Start something
SELECT statement Use method (Generate menu selection)
Grammar:
Select variable name in SEQ variable
Do
Action
Done
Instance:
#!/bin/sh
Select ch in "Begin" "End" "Exit"
Do
Case $ch in
"Begin")
echo "Start Something"
;;
"End")
echo "Stop Something"
;;
"Exit")
echo "Exit"
Break
;;
*)
echo "Ignorant"
;;
Esac
Done
Operation Result:
Description: Select is a circular selection, typically used with case statements.

These are the shell's process control statements, conditions, loops, and choices.

Shell Process Control statement if and for case

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.