Getting Started with Linux Shell scripting (ii) Looping statements

Source: Internet
Author: User
Tags bitwise bitwise operators case statement logical operators


Before the concept of variables and logical operators, you can write a judgment statement, but here note that the two sides of the brackets must be left blank, or error.

Operator
    • Assignment, with a let statement, such as Let "a=1"
    • Arithmetic operation, support +-*/% (modulo operation) * * (Power operation)
    • Bitwise operators, support << (shift left) >> (Shift right) & (bitwise VS) | (bitwise OR) ~ (bitwise reversed) ^ (bitwise XOR)
If/then/else statements

Give me a chestnut, please.

#!/bin/bashread var1if ["$var 1"-gt]thenecho "$var 1 is greater than" else echo "$var 1 was less than" fi

Note: I heard that if else inside another nested if statement, you can consider using the Elif statement, this can reduce the number of fi, so that the structure is clear


Case statement

#!/bin/bashread dayweek= (Sunday Monday Tuesday Wednesday Thursday Friday Saturday) case $day in1) echo ${week[1]};; 2) echo ${week[2]};; 3) echo ${week[3]};; 4) echo ${week[4]};; 5) echo ${week[5]};; 6) echo ${week[6]};; 7) echo ${week[0]};; *) echo "No Such day ...";; Esac


For loop

There are four different formats, and there seems to be something else.

#!/bin/bashfor var1 in Mon Tue Wed Thu Fri Sat sundo Echo ' Today is $var 1 ' donefor var2 in {1..10..2}doecho ' this is $var 2 "Donefor Var3 in $ (SEQ 1 2) Doecho" I had a/an $var 3 "Donefor VAR4 in $ (LS) Doecho" Here's the file $var 4 "done

While loop

#!/bin/bashi=1while ((I <= 5)) Doecho "i = $i" let "+ + i" done

SELECT statement

This SELECT statement to the time the wood has seen, run when you can interact with the user, run the following program to understand. If you don't break it, you're going to die.

#!/bin/bashecho "which color do you like?" Select Var1 in red Green Bluedoecho "I like $var 1" breakdone

Getting Started with Linux Shell scripting (ii) Looping statements

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.