Shell Syntax format

Source: Internet
Author: User

Shell Syntax format


If statement


If condition

Then

Command sequence

Fi


If condition

Then

Command sequence

Else

Command sequence

Fi


If condition

Then

Command sequence

Elif conditions

Then

Command sequence

Elif conditions

Then

Command sequence

Else

Command sequence

Fi


eg

#!/bin/bash


Read-p "Enter a password:" Password

If ["$password" = "pass"]; Then

echo "OK"

Else

echo "Error"

Fi



Case $ variable name in

Mode 1)

Command sequence

;;

Mode 2)

Command sequence

;;

Mode N)

Command sequence

;;

*)

Esac


Case $ variable name in

Mode 1| Mode 2)

Command sequence

;;

Mode 3| Mode 4)

Command sequence

;;

Mode 5| mode 6)

Command sequence

;;

*)

Esac


eg

#!/bin/bash


Case $ in

[a-z]| [A-z])

echo "character"

;;

[[:d Igit:]])

echo "Number"

;;

*)

echo "Eorror"

Esac



For variable in value 1 value 2 ... Value n

Do

Command sequence

Done


For (initialize variable value; end loop condition; operation))

Do

Command sequence

Done


eg

#!/bin/bash


For i in {1..10}

Do

Echo $i

Done


while [condition]

Do

Command sequence

Done


While Read-r line

Do

Command sequence

Done < file


eg

#!/bin/bash


File=/etc/sysconfig/network-scripts/ifcfg-eth0


While Read-r line

Do

Echo $line

Done < $file


Shell Syntax format

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.