"Shell"--Basic knowledge (2)

Source: Internet
Author: User
Tags aliases locale

1. How shell scripts run

Create shell script files, usually ending with. SH (e.g. example.sh)

The shell script works as follows:

    • Add ' x ' Execute permissions (relative or absolute path execution)
    • To invoke a script file using the bash or source (.) command
2,Bash's basic function

  (1) Historical order history

-C: Empty history command

-W: Save history commands in cache to config file (~/.bash_history)

Methods to invoke the history command:

①, using the up and down keys to invoke

②, use "! n "Repeating nth history command

③, use "!! "Repeatedly executes the previous command

④, use "! Character "repeats the most recent command that starts with this character

  (2), command aliases Alias

Format: Alias aliases = ' original command ' [options]

Example: Alias ls= ' Ls-l '

Configuration file:

Single User: ~/.BASHRC

All Users:/ETC/BASHRC

(The two profiles here are not quite clear to the classmate, can be understood as a global variable, one is a local variable.) )

Different types of command execution priority:

Execute commands in a way that is the most, relative, or absolute path

No.2, alias command

No.3, Bash built-in commands

No.4, the command that is queried by the directory defined by the environment variable

  (3), bash common shortcut keys

    

    

3. Input/Output redirection

(1), Bash's standard input and output

Device device file name file descriptor type

Keypad/dev/stdin 0 Standard input

Display/dev/stdout 1 standard correct output

Display/dev/stderr 2 standard error output

  (2), output redirection

    

  (3), input redirection

Command: WC "option" "File name"

-C: Count words (characters)

-W: Count Words (String)

-L: Count rows

For example:

WC < file (number of words in the statistics file, Word count, number of lines)

WC << Character (count words, words, lines from start character to end character)

4. Sequential execution of MULTIPLE commands

  

5, pipe character

Format: Command 1 | Command 2 (standard output of command 1 as standard input for command 2)

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

For example:

echo "--help" | Cat

      

echo "--help" | Xargs Cat

      

6. Wildcard characters

  

7. Other special symbols in bash

  

Parent Shell and child shell (view with Pstree)

    

Using the shell does not affect the parent shell, the commands executed in () are executed in the new open shell and do not affect the shell

8. Variables

  (1), variable specification

Can consist of letters, numbers, and underscores, but cannot start with a number, and cannot have spaces in the middle of a variable.

  (2), precautions  

V in bash, the default type of the variable is string, and if you need to perform a numeric operation, you need to convert to a numeric type

The v variable is linked with an equal value, and cannot have spaces on the left or right side

If there are spaces in the V variable, you need to use single or double quotation marks including

V double quotes "" In the $, |, and ' have special meanings.

V single quotation marks are all ordinary characters.

V If you need to increase the value of the variable, make a variable value overlay, "$ variable name" or "${variable name}", Eg:path= $PATH:/sh

V Variable name recommended capitalization, easy to distinguish

(3), variable classification

v user-defined variables

v Environment Variables

v positional parameter variables

v pre-defined variables

    1. User-defined variables

A) Call:

echo $ variable Name

For example: Echo $PATH

b) View:

Set [options]----Direct ENTER to display all variables in the system

-U error when invoking a variable that is not declared (recommended)

Output the command one time before the-X command executes

c) Delete:

unset variable Name

2. Environment variables

A) Set:

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

#export声明的是环境变量

b) View:

Env

Common formats: env | grep variable

#set中包括env, ENV query environment variable, set query all variables

c) PS1 variable: Define command Prompt

PS1 can support the following options:

V \d: Displays the date in the format "Day of the Week"?

V \h: Displays the full host name. such as the default hostname "Localhost.localdomain"

V \h: Displays the abbreviated hostname. such as the default hostname "localhost"

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

V \ t: Displays 12-hour time in the format "HH:MM:SS"

V \a: Displays 24-hour time in the format "hh:mm"

V \@: Displays 12-hour time in the format "hh:mm am/pm"

V \u: Displays the current user name

V \v: Show version information for bash

V \w: Displays the full name of the current directory

V \w: Displays the last directory in the current directory

V \#: The first few commands executed

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

3. Call Format:

d) Lang language variable----defined subject environment variable

I. Viewing the languages supported by Linux

Locale–a | More

II. Currently using the available language languages

Locale

Iii. querying the default language family

cat/etc/sysconfig/i18n

Iv. Modifying the desktop version of Chinese as English

    1. Change the current language to en_US, default is ZH_CN

Export Lang=en_us. UTF-8

    1. Execute command for file name conversion

Xdg-user-dirs-gtk-update

4. Position parameter variable

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

v [email protected] Show all parameters (receive separately)

V $ #显示参数个数

Pre-defined variables

V $? Determine if the previous command is performing properly

Error value is not 0, correct is 0

V $$ PID of the current process

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

Precautions:

The v variable name can be customized, and if you do not specify a variable name, the input is saved in the default variable reply

V If only one variable name is supplied, the entire input line is assigned to the variable

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

5. Accept keyboard Input

Format: read [options] [variable name]

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

-T seconds: the number of seconds read waits (the number of seconds to jump to the next one automatically)

-N characters: read maximum number of characters to receive

-S: Hide input information

Note: If the special option does not wrap, use \ n to control the conversion character.

"Shell"--Basic knowledge (2)

Related Article

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.