Syntax structure for Bash shell scripts

Source: Internet
Author: User

Purpose: Self-collation, self-review, self-reflection!

Content: Dry Goods! Dry!! Dry!!!

Attach: The shortcomings of the hope that you correct, can give like me rookie a little inspiration is the best. Just the inspiration ...

Thank you: Thank the old boy (see your blog post and video, O (∩_∩) o haha ~), of course, there are online technology Daniel, reference book "UNIX Shell"

Bash Shell Scripting Structure

#/bin/sh

Shbang line, script interpreter.

such as: #/bin/csh,#/bin/awk ... such as

#

Comments.

such as: This doc is writed Chbo.

Shell metacharacters or wildcard characters

*, "," ",?, [],|,>,>>, etc.

e.g. RM./*

Display output echo "Hello,chbo."
Local variables

Var=value

Var= ' value '

Var= "Value1 value2"

Assign a value to a variable---the command result---

var= ' cmd '

var=$ (CMD)

Environment variables

---Temporary entry into force---

Export Var=value

Var=value; export Var

---Permanent entry into force---

Write:/ROOT/.BASH_PROFILE/ROOT/.BASHRC

Write normally:/etc/profile

Script directory:/ETC/PROFILE.D

View variables

Echo $var _name

Echo $PATH


Special variables

$ A: printing itself

$n: ${10}

$? : Echo $? Judging command Execution results

$#: Number of parameters to be transmitted

Such as:

[email protected] 02]# cat 4.sh

[$#-ne 2] && {

echo "must"

Exit 1

}

echo haha

Pass the reference

---interactive---

[Email protected]]# read-t 5-p "pls input:" A

Pls input:33

[Email protected]]# echo $a

33

---non-interactive---

Var1=$1

Var2=$2

Arithmetic operations

1, ((n=1+2))

2. n= ' expr 5 + 9 '

3, let n=1+2

4, n= ' echo 3+3|BC '

5, N=$[2+3]

Way one: Test

Mode two: []

Method Three: [[]]

---Numeric comparison--------------------------------

-eq-ge-gt-le-lt-ne

such as: [$1-eq]

---string comparison------------------------------

= =!

-N str1 Check if the length of the str1 is not 0

-Z str1 check str1 length is 0

Such as:

["$str 1" = "$str 2"]

[-N "$str 1"]---> Check if a variable contains data

Note:

1, plus "

2, = Both sides must have spaces /p>

---File comparison--------------------------------

-e

-d-f -r -w -x (Excute)  -s (size

-nt (new than)  

-o Check that the file exists and belongs to the current user all

-G checks whether a file exists with the same default group as the current user

For example:

[-S $file] Check if file exists not empty

[$file 1-nt $file 2] Check file1 than file2 new


conditional statement If [-f $file]

Then

 echo 0

Else

 echo 1

Fi                

<===>

[-F $file]&&echo 0| | Echo 1

-------------------------------------------

If [ ]

Then

 statements

Elif

Then

 statements

Elif

Then

 statements

Else

 statements

If

-------------------------------------------

Case Var_name in

PATTERN1)

 statements

;;

Pattern2)

 statements

;;

Pattern3)

 statements

;;

*)

 default value

;;

Esac


Looping statements

For n in ' seq 10 '

Do

Echo 10.0.0. $n

Done

---------------------------

While []

Do

Commands

Done

----------------------------

Until []

Do

Commands

Done

Function

Function name () {

Instructions

return n

}

Perform:

  1. Define the function before calling

  2. Calling functions, writing directly to the function name

Execution of the script
  1. SH script_name

  2. chmod +x Path/script_name;path/script_name

  3. SOURCE Script_name or. Script_name

    (The current shell can invoke the contents of the script)






This article is from the "Chboyinxiang" blog, make sure to keep this source http://1076546426.blog.51cto.com/9959876/1702751

Syntax structure for Bash shell scripts

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.