Linux learning record 4 (Bash and Shell scirpt), bashscirpt

Source: Internet
Author: User
Tags array definition

Linux learning record 4 (Bash and Shell scirpt), bashscirpt

1. What is Shell?

Shell in the narrow sense refers to the software for command columns, including basic Linux operation window Bash, etc. shell in the broad sense includes the software for graphic interfaces, because the graphic interface can also operate various drivers to call the core for work.

The legitimate shell of the system is written in the/etc/shells file.

Ii. Basic Bash Shell Operation Skills

1. view the command that was run in the previous Logon: vim ~ /. Bash_history2, command, and file completion function (Tab key) tabs are followed by the first word of a string of commands, and the command completion tab is followed by the second word of a string of commands, file completion 3. Command alias setting function: aliasalias lm = 'LS-al' 4. Programmatic script (shell scripts) 5. wildcard 6. line feed input \ + [enter] 7. Remove the string [crtl] + u/[crtl] + k8 from the cursor and move it from the cursor to the entire command string front/end [crtl] + a/[crtl] + e9. Use env and export to observe environment variables; set can observe all the variables in the current bash environment 10, continuous commands; Two commands can be executed consecutively:
  • & The previous command must have no error message
  • | If it is correct, the second entry is not executed. The second error is followed.
11. Encoding Problems: LANG or LC_ALL settings; locale lists the current language. 12. view the original code of the alias: type-a alias command 13. list all the buttons and key content: stty-a 14. Set the key function: stty erase ^ h: set crtl + h to delete character 15 and get core version: echo $ (uname-r) Iii. Shell Script Concept:Shell script is a program written using the shell function, which uses plain text files. Write some shell syntaxes and commands in it, and use regular expressions, pipeline commands, and data stream redirection to process commands. My personal understanding: in fact, Shell script is a batch processing command in window, which writes shell commands and programs with a script syntax. Shell script is a good tool for system management, but it is not enough to process a large number of numeric operations, because shell scripts is slow and uses a large amount of CPU resources, the allocation of Host resources is poor. Variable:Variable type: bash variable type is a string by default, which can be an integer or array type. It has no floating point type, so there is no decimal number. The result of 1/3 is 0. variable Declaration: The default value is a string, which can be directly declared as variable name = variable content (x = 123). To declare it as an array or an integer, you can:
  • The variable and variable content are connected with an equal sign =, a = 123
  • Equal signs cannot be directly followed by space characters
  • It consists of letters and numbers, but cannot start with a number.
  • If the variable content contains special characters, you can use "" or ''to combine the variable content.
  • "" $ Variable, can retain the original features
  • $ Variable in '', which is treated as a common character
  • You can use the Escape Character \ to treat unknown special characters as common characters.
  • ''Or (): The command in'' or () is executed first as an external input.
  • You can perform the [cancel] action by converting-to +: declare + x sum:
  • The variable type is listed separately: declare-p sum
  • Upgrade to environment variable: export PATH
  • Cancel variable: unset
  • Array definition: var [1] = "123" var [2] = "456" var [3] = "789"
  • ''Or (): The command in'' or () is executed first as an external input.
  • Variable $: The displayed number is your PID Number.
  • Variable? : The error code of the previous error command. If there is no error, it is 0.

 

Variable Interaction: read-p "enter your name:"-t 30 name (-p indicates the prompt world,-t indicates the input time of 30 seconds) variable content extension: PATH = "$ PATH": Delete the/home/bin variable content: echo $ {path #/*:}, which means starting from/: delete the shortest characters

 

Replace variable content:

 

Run1. Execute directly in absolute and relative paths. The executor must have the script execution permission. 2. Execute global variables through bash shell. sh or sh shell. sh. How does the source and sh scripts run differently?Both sh script and bash script use a new bash environment to execute commands in the script. After the subroutine is complete, each variable or action in the subroutine will end and will not be returned to the parent program. The source script will be executed in the parent program, so all the actions will take effect in the original bash. Process control (sequence): Shell scripts is executed from top to bottom and from left to right. Process control (condition):
  • Test-e file name
  • Test-f whether the file name exists and is file
  • Whether the test-d directory file name exists and is a directory
  • Whether test-r-w-x filename has the permission to read, write, and execute
  • The two values of test n1-eq n2 are equal.
  • The two values of test n1-ne n2 are different.
  • -Gt n1 is greater than n2
  • -Lt n1 less than n2
  • -Ge is greater than or equal
  • -Le is less than or equal

 

You can use $ {0}, $ {1}, $ {2}, and $ {3} to perform Variable Interaction in the script, $ {0} indicates the script name to be executed, $ {1} indicates the first input variable, $ {2} indicates the second input variable, and $ {3} indicates the third input. variable (s)

 

If condition judgment

 

Case condition judgment

 

Process control (loop ):

While Loop

 

For Loop

 

For Loop

 

Function: 

Note: unlike traditional languages, a function must be defined at the beginning of a script so that the program can understand the function during execution. Function parameters are also transmitted using $ {1}, $ {2}, and $ {3, that is, the real parameters passed in when calling the function identified by $ {1}/$ {2}/$ {3} in the function. Instead of the parameters when the script is passed.

 

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.