Linux Shell Learning Notes

Source: Internet
Author: User
Tags array length stdin

The first line of the shell script illustrates the path to the interpreter: #!/bin/bash
Run the script two ways: Use the bash command to run the shell file, or grant the script file execution permissions, you can directly execute the file
When the shell starts, a set of commands is executed to define the question text, color, etc., and the command is stored in the ~/.BASHRC.
Login Shell placed in ~/.bash_profile
Shell history File ~/.bash_history
Each command or command sequence in bash is delimited by using semicolons or newline characters.

echo for terminal printing
printf for terminal printing
Env View all terminal-related environment variables
cat/proc/$PID/environ View the environment variables of the process (PID is set to the ID of the process, always an integer)
Pgrep process name Gets the ID of the process

Variable assignment and variable equality operations to distinguish between
Variable assignment: Var=value
Variable equality operation: var = value

Export to set environment variables
Export path= "$PATH:/home/usr/bin"
Or
Path= "$PATH:/home/usr/bin"
Export PATH

Common environment variables are: PATH HOME PWD USER UID SHELL

How to get the length of a variable: ${#var}

Mathematical operations using Let (()) [] mode, advanced operation using EXPR,BC

When you use let, the variable name does not need to be added again before the
no1=4;
no2=5;
Let Result=no1+no2

Let support + + self-add operation--self-reduction operation let no1++ let no1--let no1+=6 let no1-=6

The [] operator [] can also use the $ prefix
Result=$[no1 + NO2] result=$[$no 1 + 5]

When using (()), the variable name needs to be added $
result=$ ((no1 + 50))

When expr is used for basic arithmetic operations, floating-point numbers are not supported and integers are supported
BC is also a computing tool

StdIn is 0 stdout for 1 stderr2
> Output text is redirected or saved to a file the contents of the destination file are emptied
>> text appended to the target file

REDIRECT Errors and outputs: 1>out.txt 2>err.txt
redirect errors and outputs to a file 2>&1 OUT.txt or &> out.txt

View File Contents Cat Tmp.txt
$? Gets the status code executed by the command echo $?

To change a file's permissions: chmod a.txt Add executable permissions to the file: chmod +x a.out


Display output in console and redirect to a file overwrite original file: Cat A.txt | Tee OUT.txt

Display output in the console and redirect to a file to append content to the original file: Cat A.txt | Tee-a OUT.txt

Use stdin as the command parameter. Just take-as the command's filename argument: $ cmd1 | CMD2 | CMD-

redirect Files as command input: cmd < file

Redirect The text block inside the script: the content between Cat<<eof>log.txt and EOF is entered into Log.txt as part of the stdin
Cat<<eof>log.txt
LOG FILE HEADER
Eof

Create a custom file descriptor: Exec 3<input.txt #使用文件描述符3打开并读取文件input. txt
Create and use a custom file descriptor procedure: [The Append mode is very similar]
EXEC 3<input.txt
Cat <&3

Declaration of the array: array_var= (1 2 3 4) or array_var[0]= "Test1" array_var[1]= "Test2"
Output array contents: ${array_var[0]} or Index=1 ${array_var[$index]}
Print all values in the form of a manifest: ${array_var[*]} or ${array_var[@]}
Print array length (number of) ${#array_var [*]}

Define an associative array: declare-a Ass_array
Methods for adding elements: ass_array= ([Index1]=val1 [Index2]=val2] or Ass_array[index1]=val1 ass_array[index2]=val2

Using aliases Alias
Alias new_command= ' command Sequeue ' example: Alias install= ' sudo apt-get install '

Linux Shell Learning Notes

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.