Shell selection statement, loop statement

Source: Internet
Author: User
Tags case statement

Judgment statement:
If judging condition
Then
Statement
[Elif]
Statement
...
[Else
Statement
Fi

#!/bin/bash
If [$#-eq 0]
Then
echo "Error"
echo "can use this command like command file_name"
Exit 4
Fi

Type= ' File $ | Cut-d '-f2 '

Echo is a $TYPE


Case statement:
Case Test value in
Option 1)
...
;;
Option 2)
...
;;

...

*)
...
Esac

#!/bin/bash
# This was a simple test in case
#
Case $# in
0)
echo "No option"
;;
1)
echo "You used $# option"
;;
2)
echo "You use $# options"
;;
*)
echo "Options is too ..."
Esac

Looping statements:
    for loop:
      for I in parameter probability list
       do
        loop body
      done
     
    #!/bin/bash
  #
  declare-i sum=0 ;
  declare-i i;

For I in {1..100}
Do
Let sum+= $I;
Done

echo "SUM = $SUM"

List-to-build:
{M... n}: List of integers m to n
Seq Start number Step length end number: The default step length is 1, the starting number is 1, the starting number, step length can be omitted, the end number cannot be omitted

While loop:
While loop condition
Do
Loop body
Dione

#!/bin/bash
#
Declare-i sum=0;
Declare-i I=1;

While [$I-le 100]
Do
Let sum+= $I
Let i++
Done

echo "SUM = $SUM"


Until cycle:
Until cycle conditions
Do
Loop body
Done

#!/bin/bash
#
echo "Enter Username:"
Read user
Until who | grep "$user" &>/dev/null
Do
Sleep 30
Done

echo "$user is on"

For a while loop, the loop will continue as long as the loop condition is met
For until loops, loops are executed as long as the loop condition does not end successfully
In fact, the until loop is less useful than a while loop, but it can be helpful if you're waiting for an event to happen.

Break: Used to exit a loop
Continue: For early start of the next repeating cycle
Both the break and Continue commands accept optional numeric parameters, which can be used to indicate how many loops are included to be interrupted or resumed.

Shift: When it is used to handle command-line arguments, one (or more bits) is shifted to the left at a time, and when the shift is executed, the original $ $ will disappear, replaced by the
$ $ to replace, and so on, and $ #的值也会逐次减少, shift also accepts an optional parameter, that is, a few moves at a time, the default is 1

Shell selection statement, loop statement

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.