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

Analysis of examples written by Shell script

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you an example analysis of Shell scripting. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1 Basics:

Shell is a scripting language written in C language and a command line interface

It is the bridge between the user and the Linux. The user inputs the command to the Shell, the Shell passes the corresponding operation to the kernel (Kernel), and the kernel outputs the processing result to the user.

Bash is a kind of shell (language)

The first line specifies the interpreter #! / bin/bash

Generate a sub-bash on the current terminal to execute the script (except source)

Script execution method

Bash test.sh (/ bin/sh generally soft link to / bin/bash;/bin/sh is equivalent to / bin/bash-- posix, so the difference between sh and bash is actually whether bash enables posix mode)

. / (add executable permission chmod + x tesh.sh)

Source test.sh (no sub-bash is generated for execution in the current bash)

1.1 variables 1.1.1 system variabl

1.1.2 ordinary variables and temporary environment variables

Common variable: var=value

Temporary environment variable: export var=value

Scope:

Normal variable: current shell

Temporary environment variables: current shell and sub-shell (bash)

Environment variable file:

System level:

/ etc/profile: system-wide environment variables and startup files

/ etc/bashrc: system-wide functions and aliases

1.1.3 position variable

$1-$n

Quotation marks can also be quoted

Shift can swallow up a variable, and the following variable moves forward.

Run time:

1.1.4 Special variables

1.1.5 variable reference

Variable reference: $+ variable name (special case ${variable})

Special characters can be recognized in double quotation marks, but not in single quotation marks.

1.2 comments

#

2 Shell string processing: ${} 2.1get string length: ${# var}

2.2 string slicing: ${var:start:length}

2.3 replacement string: ${var/old/new}

Before old: /-- matches the first string; / /-- matches all characters

${VAR// [^ 0-9] /}: replace all non-digits in VAR with empty ones

2.4 string interception

# remove the left, the shortest match; # # the longest match

% remove right side, shortest match;% longest match

* match deleted characters

2.5 variable state assignment

${VAR:-string} returns string if the VAR variable is empty

${VAR:+string} returns string if the VAR variable is not empty

${VAR:=string} if the VAR variable is empty, the VAR variable is reassigned to string

${VAR:?string} output string to stderr if the VAR variable is empty

3 Shell expressions and operator 3.1 conditional expressions

3.2 Integer comparator

3.3 string comparator

&: the front is true and the latter is true

| |: the front one is the one behind the fake execution. |

3.4 File testing

3.5 Boolean operator

3.6 logical discriminator

3.7 Integer Operation

$(): ternary operator: true-return to the right; false-return to the left

3.8 the role of parentheses in Shell

4 Shell process control 4.1 if single branch: if condition; then command fi

Double branches: if condition; then command else command fi

Multi-branch: if condition; then command elif condition; thenelse command fi

4.2 forfor variable name in value list; do command done

4.3 while

While condition; do command done

# read file contents line by line

4.4 break and continue

Break: terminating a loop

Continue: jump out of the current cycle

4.5 casecase mode name in mode 1) command;; mode 2) command;; *) execute command esac if the above is not met

The above is the example analysis of the Shell script shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to 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

Internet Technology

Wechat

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

12
Report