Linux shell Programming basics

Source: Internet
Author: User
This is a basic article on shell. The main content is the role of bash, the role of history, the pipeline operator, how to customize variables, and how to execute scripts. The next article will introduce some content such as if,, while, case and other statements. You are welcome to discuss with me... I. shell environment overview 1. Role of Shell...

 

This is a basic article on shell. The main content is the role of bash, the role of history, the pipeline operator, and how to customize variables.

The next article describes how to execute scripts and other statements, such as if, for, while, and case. You are welcome to discuss with me...

I. shell environment overview

1. Role and common categories of Shell

The role of Shell-the command interpreter, "translator"; between the operating system kernel and the user, responsible for interpreting the command line (similar to the cmd in win)

2. switch the Shell environment

1) temporary switch: execute other Shell programs, such as ksh and zsh.

2). change the user logon Shell:

P needs to modify the last field recorded by the user in the/etc/passwd file

P or execute: usermod-s Shell program path username

3. common Bash functions

Tab key auto-completion command edit shortcut key Command History Command alias

Standard input/output redirection operation pipeline operation

4. Bash command history

1) Command History

Saves commands that have been executed by users.

Storage location :~ /. Bash_history file

2) view the history Command history

3) Call History commands

! N: execute the nth command in the history.

! Str: execute commands starting with "str" in the history

4) set the number of historical commands/etc/profile

Modify the parameter HISTSIZE (1000 by default)

5. Bash standard input and output

Interactive hardware device

Standard input: Receives user input data from this device

Standard output: the device outputs data to users.

Standard error: The device reports an error message.

6. Bash redirection

Changes the direction of standard input, standard output, and standard errors

7. Bash MPs queue operations

Pipeline operation symbol "|"

Connect the left and right commands, and use the result output from the left command as the input (processing object) of the right command)

Format: cmd1 | cmd2 [... | cmdn]

II. Application of Shell variables

Shell variable types

User-defined variables: defined, modified, and used by the user

Environment variables: they are maintained by the system and used to set the user's Shell working environment. only a few variables can be modified.

Pre-defined variables: Bash pre-defined special variables that cannot be directly modified

Location variable: Pass the execution parameters to the program through the command line

Variable assignment and reference

1) define new variables

Variable names must start with English letters or underscores (_) and are case sensitive.

Format: variable name = variable value

2) view the variable value

Format: echo $ variable name

3) assign values to variables from the input content on the keyboard

Format: read [-p "info"] variable name

4) assign values to variables with different quotation marks

Double quotation mark "": other variable values can be referenced with the $ symbol.

Single quotation marks '': do not reference other variable values. $ is considered as a common character.

Reverse marker '': outputs the command execution result to the variable.

5) set the scope of the variable

Format: export variable name...

Export variable

6) calculation of numerical variables

Returns an integer expression.

Format: expr variable 1 operator variable 2... [operator variable n]

Common operators of expr

Addition operation: +

Subtraction :-

Multiplication :\*

Division :/

Modulo (remainder) operation: %

Environment variable

1) environment variable configuration file

Global Configuration File:/etc/profile

User Configuration File :~ /. Bash_profile

2) view environment variables

The set command can view all Shell variables, including environment variables.

Common Environment variables: path variables, similar to windows path

III. Shell script concept

1. Shell script

A) purpose: complete specific and complex system management tasks

B) format: multiple Linux commands and common text files are stored in a centralized manner.

C) execution method: Explain the execution in sequence according to the preset sequence.

2. run the Shell script program

1) directly execute a script file with the "x" permission

Example:./repboot. sh

2) use the specified interpreter program to execute the script content

Example: bash repboot. sh, sh repboot. sh

3) use the source command (or.) to read and execute the script.

Example: souce repboot. sh or. hello. sh

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.