Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the knowledge points of shell script

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you what are the knowledge points of the shell script, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1.shell scripts are case sensitive

2.Unix special characters are: (; $? & * () [] `'"+ escape ()

3.Shell 's comments begin with #

4. Definition of function

Function fuction_name () {

Command to execute

}

When calling, use function_name.

5. Control structure

1) If...then statement

If [test_command]

Then

Commands

If

2) If...then...else statement

If [test_command]

Then

Commands

Else

Commands

If

3) If...then...elif...then... (else) statement

If [test_command]

Then

Commands

Elif [test_command]

Then

Commands

Else

Commands

Fi

4) for... In statement

For loop_varible in argument_list

Do

Commands

Done

5) while statement

While test_command_is_true

Do

Commands

Done

6) until statement

Until test_command_is_true

Do

Commands

Done

7) case statement

Case $variable in

Match_1)

Commands_for_1

Match_2)

Commands_for_2

.

.

.

*) # option for other values

Commands_for_no_match

Esac

6.break, continue, exit and return statements

Break jumps out of the whole loop and executes the next code outside the loop.

Continue ends this cycle and continues the next cycle

Exit exits the entire script, usually followed by an integer (such as exit 0), which is sent to the system as a return code

Return is used to return data in a function or to return a result to the calling function.

7.here document

Used to redirect input to an interactive shell script or program without user intervention.

Program_name & 1 > / dev/null

Output to the system-specified console: > / dev/console

21. Parse the command line parameter getopts

Getopts optionstring VARIABLE

Optionstring are the required parameters, separated by colons, and can be omitted if no arguments are needed. If there is a colon before optionstring, then any mismatch will load one in VARIBLE? Number

The function of using getopts is to parse out the parameter, and then use this parameter to do different operations. Such as:

While getopts: s:m:h:d:p: TM

Do

Case $TM in

s)

Do something

m)

Do something

.

.

.

?)

Exit 1

Esac

twenty-two。 Processing files line by line

While read LINE

Do

Echo "$LINE"

Done / tmp/aaa puts all the file and folder name information in the / usr directory in the expiration / tmp/aaa file.

Such as: ls / test 2 > / tmp/aaa if there is no / test folder, an error will occur, and 2 > will store the error message in the aaa file.

">" can create new files, such as: > hello.java

">" can empty a file such as hello.java file has content, I then > hello.java, hello.java content empty "> >" double greater than the sign together becomes an additional function, in the previous file content after the append content. For example: cat / tmp/sh > > / tmp/aaa appends all the files and directory information in the / tmp/sh directory to the aaa file. The previous contents of the aaa file will not be erased. When cat > hello.java, you can type a lot of content on the screen, and you will exit when you press ctrl+D. When re-cat hello.java

What you just entered is all in the hello.java file. Input redirects such as: cat > a.txt $SYSPROFILE

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report