Initial knowledge of Shell programming

Source: Internet
Author: User
Tags chmod linux shell commands

I. Basic introduction to Shell programming 1.1shell programming

The shell is an interface that the user interacts with the kernel, and the most popular shell is now called the Bash shell

The shell is also a programming language < interpreted programming language >, Shell Scripting < is programming with Linux shell commands >

A system can have multiple shells, you can view the shell installed in the system through the cat/etc/shells command, different shell may support the command syntax is not the same

1.2 Basic formats

The code is written in a plain text file, usually with an. sh suffix

VI hello.sh

#!/bin/bash    "helloWorld"   # #  Note can also be written here # # This is a line of comments

Execute script

SH hello.sh

or add x permission to the script to execute directly

chmod 755 hello.sh

./hello.sh

1.3 Basic syntax 1.3.1 System variables

Variables in the Linux shell are divided into "system variables" and "user-defined variables"

System variables can be viewed through the SET command

System variables: $HOME, $PWD, $SHELL, $USER, and more

1.3.2 Custom Variables 1 , grammar

variable = value (e.g. STR=ABC)

Cannot have spaces on either side of the equals sign

Variable names are generally used in uppercase

Using variables: $arg

Double quotes and single quotes are different, ① double quotation marks only the space, ② single quotes will refer to variables such as $param

2 , Example

VI hello.sh into edit mode,

str= "Hello World"

A=9

Echo $A

Echo $STR

What if I want to print Hello Worlds is greater?

Echo $STRs is greate can you do that?

No, the correct wording is:

echo ${str}s is greate

unset a undo variable a

ReadOnly b=2 declares a static variable b=2 and cannot unset

Execution Result:

Export A #可把变量提升为当前shell进程中的全局环境变量 for use by other child shell programs

Be aware of the export, see the following example:

VI a.sh

#!/bin/Basha="A in a.sh"echo $a/root/b.sh

VI b.sh

#!/bin/BASHB="b in b.sh"echo $becho $a

Assign operation permissions separately: chmod 755 a.sh chmod 755 b.sh

Then execute./a.sh, and you'll find that the a variable defined in the a script is not printed in the B script.

If you want to print out the variable A of the a script in B, you need to define the variable a as export in a script.

At this point, the a variable becomes the global variable of the bash process where the a.sh script is located, and all the child processes of the process can access the variable a

Specific operation, modify a.sh, vi a.sh

#!/bin/bashexport a="A in a.sh"echo $a/root/b.sh

Execution./a.sh

This time A's subprocess B will be able to access the variables of process a!

Another way:

If you call b.sh in the a.sh script as follows

.  . /b.sh    # # Note: Focus on the front one "." No.

Or

source./b.sh

Then, b.sh is running in the bash process space where a.sh resides.

Summarize:

1 , a.sh call b.sh directly in the , will let b.sh in a where the bash executing in the subprocess space of the process

2 , and the child process space can only access the parent process with export the variables defined

3 , a shell The process cannot promote its own defined variables into the parent process space

4 , ". "number, the script is executed in the shell where the caller execution in process space

3 , anti-quote value assignment

A= ' Ls-la ' # # Counter quote, run the command inside and return the result to the variable a

a=$ (Ls-la) # # equivalent to anti-quote

4 , special variables

$? Indicates the status code of the last command exit

$$ indicates the current process number

$ A indicates the current script name

$n represents the n position input parameter (n for number, n>=1)

$# represents the number of parameters, often used in loops

$* and [email protected] All represent parameter lists. '

Note: $* with [email protected] difference

$* and [email protected] All represent all parameters passed to a function or script

ü Not be enclosed by double quotes ""--

$* and [email protected] are all in the form of a list of parameters in "$ $ ... $n"

ü When they are enclosed by double quotes ""--

"$*" takes all the parameters as a whole, forming an entire string in the form of "$ $ ... $n" ;

"[Email protected]" will separate the various parameters to "$" "$" ... "$n" form a list of parameters

1.4 Operator 1.4.1 Arithmetic operation 1, using expr

Format expr m + N or $ ((m+n)) Note that there is a space between the expr operators

For example, calculate the value of (2+3) x4

1. Step calculation
S= ' Expr 2 + 3 '
Expr $S \* 4 # # # * need to be escaped

2. Complete the calculation in one step

Expr ' expr 2 + 3 ' \* 4

echo ' expr \ ' expr 2 + 3\ ' \* 4 '

2. Using (())

((1+2))

(((2+3)))

Count=1

((count++))

Echo $count

But to get the results of the operation, you need to use the $ reference

a=$ ((1+2))

3, with $[]

A=$[1+2]

Echo $a

1.5 Process Control 1.5.1 If Syntax 1, syntax format

If condition
Then
Statements
[Elif condition
Then statements. ..]
[Else
Statements]
Fi

2. Example
#!/bin/bash
Read-p "Please input your name:" name   # # Read command to read input data from console # # printf '%s\n ' $NAMEif [$NAME = Root]        Then                echo "Hello ${name},  welcome!"         elif [$NAME = Likemebee] then echo "Hello ${name}, welcome!" Else echo "SB, get out here!" fi      
3. Judging conditions

1/Conditional Judgment basic syntax

[Condition] ( Note that there are spaces before and after condition)

#非空返回true, you can use $? authentication (0 is true,>1 to false)

[Likemebee]

#空返回false

[  ]

Note [] internal = surrounding spaces, there are differences:

"Not to be continued ... Continuous Update "

Initial knowledge of Shell programming

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.