[Shell]-Basic Knowledge (2), shell-Basic Knowledge

Source: Internet
Author: User

[Shell]-Basic Knowledge (2), shell-Basic Knowledge
1. Shell script Running Mode

Create a Shell script file, generally ending with. sh (for example, example. sh)

The Shell script runs as follows:

  • Add 'X' execution permission (relative or absolute path execution)
  • Use the bash or source (.) command to call the script file
2. Basic Bash Functions

  (1) history command history

-C: Clear history commands

-W: Save the History commands in the cache to the configuration file (~ /. Bash_history)

Method for calling History commands:

① Use the top and bottom keys for calling

② Use "! N "repeats the n historical commands

③ Use "!" Repeat the previous command

④ Use "! "Repeated execution of the latest command starting with this character"

  (2) command alias

Format: alias = 'COMMAND '[Option]

Example: alias ls = 'LS-l'

Configuration file:

Single User :~ /. Bashrc

All users:/etc/bashrc

(If you are not familiar with the two configuration files, you can think of them as a global variable and a local variable .)

Different types of command execution priorities:

1. execute commands in relative or absolute paths

No. 2 alias command

No. 3. bash built-in commands

No. 4. commands found in directories defined based on Environment Variables

  (3) Common Bash shortcut keys

    

    

3. Input/Output redirection

(1) Bash Standard Input and Output

Device File name file descriptor type

Keyboard/dev/stdin 0 standard input

Display/dev/stdout 1 standard and correct output

Display/dev/stderr 2 standard error output

  (2) Output redirection

    

  (3) input redirection

Command: wc [Options] [file name]

-C: Number of words (characters)

-W: count the number of words (string)

-L: Number of Statistics rows

For example:

Wc <file (number of words, number of words, and number of lines in the Statistical File)

Wc <character (count words, words, and number of lines from the start character to the end character)

4. Multiple commands are executed sequentially.

  

5. pipeline operator

Format: command 1 | command 2 (use the standard output of command 1 as the standard input of command 2)

Xargs implements the standard output of command 1 as the option of command 2

For example:

Echo "-- help" | cat

      

Echo "-- help" | xargs cat

      

6. wildcard characters

  

7. Other special symbols in Bash

  

Parent Shell and sub-Shell (use pstree to view)

    

The use of Shell does not affect the parent Shell, and the commands executed in () are executed in the newly opened Shell, without affecting the current Shell.

8. Variables

  (1) variable Specification

It may consist of letters, numbers, and underscores, but cannot begin with a number. There cannot be spaces in the middle of the variable.

  (2) Precautions  

In Bash, the default types of variables are string type. To perform numerical operations, convert them to numeric type.

The v variable uses the equal sign link value. There cannot be spaces on both sides of the equal sign

If the v variable has spaces, you must use single quotation marks or double quotation marks to include

$, |, And ''enclosed by double quotation marks (") have special meanings.

The v single quotes are enclosed by common characters.

V to add a variable value, overwrite the variable value, for example, "$ variable name" or "$ {variable name}", e.g.: PATH = $ PATH:/sh

It is recommended that the v variable name be in uppercase for easy differentiation.

(3) Variable Classification

VUser-Defined variables

VEnvironment Variable

VLocation Parameter Variable

VPredefined Variables

A) call:

Echo $ variable name

Example: echo $ PATH

 

B) View:

Set [Option] ---- press enter to display all variables in the system

-U: an error is reported when no declared variable is called (recommended)

-Before running the-x command, output the command once.

C) delete:

Unset variable name

 

2. Environment Variables

A) settings:

Export variable name = value ------ (Declaration ~ /. Bash_profile)

# Export declares environment variables.

B) View:

Env

Common Format: env | grep variable

# Set includes env and env to query environment variables, and set to query all variables

C) PS1 variable: Define Command Prompt

PS1 supports the following options:

V \ d: Display date, in the format of "day of the week"

V \ H: displays the complete host name. For example, the default host name is "localhost. localdomain"

V \ h: the abbreviated host name is displayed. For example, the default host name is "localhost"

V \ t: displays the 24-hour time in the format of "HH: MM: SS"

V \ T: displays the 12-hour time in the format of "HH: MM: SS"

V \ A: displays the 24-hour time in the format of "HH: MM"

V \ @: displays the 12-hour time in the format of "HH: MM am/pm"

V \ u: displays the current user name

V \ v: displays Bash version information.

V \ w: displays the complete name of the current directory

V \ W: display the last directory of the current directory

V \ #: Number of commands executed

V \ $: prompt. If the root prompt is "#", if the normal user prompt is "$"

3. Call format:

D) LANG language variable ---- define the subject environment variable

I. view supported languages in Linux

Locale-a | more

Ii. Currently available languages

Locale

Iii. query default language families

Cat/etc/sysconfig/i18n

Iv. Change the desktop edition to English

Export LANG = en_US.UTF-8

Xdg-user-dirs-gtk-update

 

4. Location Parameter Variables

V $ * display all parameters (received as a whole)

V $ @ show all parameters (receive separately)

V $ # display the number of parameters

Predefined Variables

V $? Determine whether the previous command is executed normally

The error value is not 0. The correct value is 0.

V $ PID of the current process

V $! The process of the last command executed in the background

 

Note:

The v variable name can be customized. If no variable name is specified, the input is saved to the default variable REPLY.

V if only one variable name is provided, the variable is assigned to the entire input line.

V if more than one variable name is provided, the input line is divided into several words, each variable is assigned one by one, and the last variable on the command line gets all the remaining values.

 

 

5. Accept keyboard input

Format: read [Option] [variable name]

Option:-p "prompt message": Information entered during read wait

-T seconds: the number of seconds for read wait (the next one is automatically redirected when the seconds are reached)

-N characters: the maximum number of characters read can receive

-S: hides input information.

NOTE: If special options do not wrap, use \ n to control the conversion character.

 

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.