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

How to write shell script in linux

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

Share

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

This article is to share with you about how to write shell scripts in linux, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Shell script writing specification

1. A standard Shell script will indicate which program (interpreter) will execute the contents of the script on the first line, which is generally programmed in Linux bash: #! / bin/bash or #! / bin/sh

The difference between bash and sh: sh is a soft connection to bash. In most cases, there is no difference between "#! / bin/bash" and "#! / bin/sh" in scripts, but a more standardized way of writing is to use # at the beginning of the script! / bin/bash

2. In the shell script, the content followed by # is used to annotate the script. The comment part will not be executed as a program, but will only be shown to developers and users. The system interpreter will not see it, let alone execute it. Comments can be on their own or follow the script command on the same line as the command. Comments should not be in Chinese as much as possible, and it is best not to have Chinese in the script.

3. Version, copyright and other information will be added at the beginning of the Shell script, such as:

# Date:16:29 2018-10-20 # Author: Create by xiaoxie# Description: This script function is... # Version: 1.1

4, in the shell script, try not to use Chinese comments, try to use English comments, to prevent the trouble of Chinese garbled code after the local machine or switching the system environment.

5. Shell scripts should be named with the .sh extension for example: 1.sh

6. Pairs of symbols should be written at one time as far as possible, and then backspace to add content to the symbols to prevent omissions. These paired symbols include: {}, [],'', "", etc.

7. There must be at least one space at both ends of the bracket [], so when you type the bracket, leave a space [], then type the middle content in the backspace, and make sure there is at least one space at both ends.

8. For process control statements, the format should be written at one time, and then the content should be added. For example, complete the format of for loop statement at one time

Fordo content done

9. Make the code easier to read by indenting, such as:

If conditional content then content fi

10. Double quotation marks should be added to the string definition of regular variables, and there can be no spaces before and after the equal sign. If strong references are needed, use single quotation marks (''). If it is a command reference, use reverse quotation marks (``).

11. The single quotation marks, double quotation marks and back quotation marks in the script must be symbols in English.

2. Variables in Shell

It is recommended that variable names be capitalized when defining variables, such as A=xie Barrier 99

View the contents of the variable echo $An or echo ${A}

Double quotation marks: allows other variable values to be referenced by the $symbol

Single quotation marks: reference to other variable values is prohibited, and $is regarded as a normal character

Reverse apostrophe: command replacement, extract the output result after the execution of the command, the definition method of the global variable export variable name

Position parameter

Location parameters are often used when writing scripts, such as the script name is followed by spaces plus variables, spaces plus variables, in fact, with parameters.

Position parameters are variables that are determined according to their respective positions on the command line that calls the Shell program, and are entered after the program name. The position parameters are separated by spaces, and Shell takes the first position parameter to replace $1 in the program file, the second to replace $2, and so on.

Note: $0 is a special variable whose content is the file name of the current shell program, so $0 is not a positional parameter.

#! / bin/bashecho $1echo $(($2 million))

When I execute, I add three parameters to the file name

Predefined variable

Predefined variables are similar to environment variables and are defined at the beginning of Shell, except that users can only use these variables according to the definition of shell, and all predefined variables are made up of the symbol "$" and another symbol. There are several common Shell predefined variables.

$#: number of location parameters

$*: contents of all location parameters

$?: the status returned after the command is executed. 0 means there is no error, and non-0 means there is an error.

$: the process number of the current process

$!: the last process number running in the background

$0: name of the currently executed process

Suppose I now have a 1.sh script file with the following contents

#! / bin/bash echo $1echo ${2} + ${3} echo $# # print out the number of location parameters echo $* # print the contents of the location parameter echo $? # print the status returned after the command execution echo $# print the process number of the current process echo $0 # print the process name of the current process

When I execute, I add three parameters to the file name

Third, the arithmetic operation of variables

1. Arithmetic operators commonly used in Shell

2. Common arithmetic operation commands in Shell

3. The usage of double parentheses (()) numerical operation command

The function of double parentheses (()) is to compare numerical operations with numerical values. It is very efficient and flexible, so it is a commonly used operation operator under Linux. The method of operation is as follows:

4. If conditional judgment sentence # double conditional judgment # if conditional judgment then command elif conditional judgment then command else command fi # or if conditional judgment; then command elif conditional judgment; then command else command fi

The match of if statement to string

For Loop statement for condition do command done # # or for condition; do command done

While Loop statement while conditional do Command done

7. Break, Continue, exit loop control statements

Break and continue are used to control the direction of the program in conditional statements and loop statements (for, while, if, etc.), while exit is used to terminate all statements and exit the current script.

This is how to write shell scripts in linux. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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