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

Shell programming and variables-- write your first shell script

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

First understand the role of shell:

1. Command interpreter: the "translator" is between the system kernel and the user and is responsible for interpreting the command line.

2, the user's login, the default shell program used after login, generally / bin/bash. The internal instructions and running environment of different shell will be different.

[root@localhost] # cat / etc/shells/bin/sh/bin/bash/bin/nologin.

Write the first shell script:

1. Write script code to improve the composition of the script

Use the vi Editor

All scripts have a .sh extension

Every day, linux commands are written in order of execution.

Form a good habit: the first thing to start with is a script statement: #! / bin/bash

[root@localhost ~] # vi first.shroud lh vml*. Bash # this is first scriptcd / bootpwd1s-lh vml*

2. Endow executable authority

Root@localhost ~] # chmod + x first.sh

3. Execute the script file

Method 1: script file path (absolute path and relative path) [this way the script file must have executable permissions]

[root@localhost ~] #. / first.sh

Method 2: sh script file path (this method does not change the location of the environment)

[root@localhost -] # sh / first.sh

Method 3: source script file path

[root@localhost -] # source / first.sh

Method 4: ". + spaces" script file path

[root@localhost] #. First.sh

Introduction of key symbols-- Redirection and Pipeline

Interactive hardware equipment

Standard input: receive data entered by the user from this device:

Standard output outputs data to the user through the device

Standard error: an execution error message is reported through this device

Redirect: saves the output to the specified file

| | Pipe symbol: outputs the result of the command on the left as the processing object of the command on the right |

Cmd1 | cmd2 [. | cmdn]

The function and type of shell variable

1. The function of variables:

Providing specific parameters for flexible management of Linux systems has two meanings

Variable name: use a fixed name, preset by the system or user defined

Variable value: can change according to user settings and changes in the system environment

2. Type of variable

Custom variables: defined, modified, and used by the user

Environment variables: maintained by the system and used to set up the work environment

Position variables: passing parameters to the script from the command line

Predefined variables: a class of variables built into Bash that cannot be modified directly

Custom variable

Define a new variable:

Variable names begin with letters or underscores and are case-sensitive. Full uppercase is recommended.

Variable name = variable value

View the value of a variable

Echo $variable name

Use quotation marks when assigning values

Double quotation marks: allow references to other variable values through the $symbol: reference to other variable values is prohibited. $is regarded as an ordinary character reverse apostrophe: command replacement, extracting the output result after the execution of the command

Assign a value to a variable from keyboard input

Read [- p "prompt message"] variable name

Set the scope of the variable

Format 1:export variable name. Format 2:enport variable name + variable value.

Operation of integer variables

Expr variable 1 operator variable 2 [operator variable 3]. Common operator addition operation: + subtraction operation:-multiplication operation:\ * division operation: / module (remainder) operation:%

Special shell variable

Environment variable

Created by the system to set up the user's working environment

Configuration file: / etc/profile (system environment variable), ~ / .bash_profile (user environment variable)

Common environmental variables

PWD 、 PATH

USER 、 SHELL 、 HOME

Location variable

Expressed as a number between $n and n = 1 to 9

Predefined variable

$#: the number of location variables on the command line

$*: contents of all location variables

$?: the status returned after the last command was executed. When the return status is set to 0, the execution is normal, and a non-zero value indicates an exception or error.

$0: currently executed process / program

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

Servers

Wechat

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

12
Report