PJzhang: Basics of shell 2 courses -two

Cat Ning! ! !

 

Section 6: Redirection

Standard input, standard output, error output

Input redirection symbol '<'

Output redirection symbol '>' (again after clearing the input), '>>' (constant current content, the last line is added), '2>' (if execution error, an error print out contents), '&>' (regardless of the output correct errors, all output to the specified file them)

Once you have entered 123,456, Ctrl + D, 2 Lines

 

cat /etc/passwd

 

 The number of rows wc -l </ etc / passwd, the file contents can be displayed

 

 First, setting a variable to write, then reads the contents of a variable

Creating 1.txt, content is a line 123, can be implemented to read data from the document

The variable is written to the file 2.txt

 

 Additional characters

 

 The file error information is entered into the 3.txt

 

 &> Can be wrong and correct in all appended to 4.txt, here it is an additional, not covered

 

 Create a file 3.sh, as follows, to generate a file a.sh, the content is echo 'hello bash'

 

 Section 7: variable assignment

Rules of variables: alphanumeric characters and underscores, not the beginning of a number, and the variables have specific meaning, because it is on posters

It is a weak shell variable type variable

The left and right sides of the equal sign of the assignment can be no spaces

Variable name equal to the variable value

 

 let assign values ​​to variables, not used, we can understand

let a=10+20

Command to the variable, not commonly used, meaning not great

l = ls

Command result to a variable, with a lot of $ () or two anti-quotation marks, once executed, the results of multiple use

letc=$(ls -l /etc)

Variable values ​​are special characters like space may be used including double and single quotes

Practice

 

Double quotes, single quotes

 

Section 8: variable reference and scope

} $ {Variable name referred to a reference variable, may be omitted and braces

 

 If you add the remaining characters after the variable name, you need to use braces, otherwise the program would be misleading

 

Variable assignment, bash is to enter the sub-shell, exit a child process exits, into the parent process

 

 Variable assignment, shell scripts to read variables

 

No two front and two rear reading can be achieved

 

 

 Gets the parent of the child process variable assignment, export realization

 

 unset variables can cancel the assignment

 

 

Section 9: environment variables, location variables and predefined variables

Environment Variables

 

 Print out the contents of the current variable, PATH is the command search path

 

 Create a file 5.sh, print hello bash, show directory disk space

 

 ./ obvious indicate the search path to find the command under the root directory, no root directory

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

 

 Shell script can be executed under the root directory

 

 Currently defined variables, take effect only for the current terminal (any folder location), closed end or open a new terminal is not in force,

If the export processing of a variable in a directory, it will remain in effect

 

 Convenient interface of the terminal

 

 set command

 

 $? Represent the judgment of a command is not executed successfully, such as ifconfig, $$ displayed pid the current process, $ 0 to display the current name

 

 Create a file 6.sh

 

 The results are not the same in different operating modes

 

 7.sh create a file, you can make the characters appear in the order parameter

 

 Modify 7.sh

 

 Handle a single character argument

 

 If the content is empty is underlined

 

 

 

 

 

Autonomously judgment is not empty

 

 

 

Section 10: Environment Variables profile

A common configuration file, / etc / are generic, all home directories ~

/etc/profile

/etc/profile.d/

~/.bash_profile

~/.bashrc

/etc/bashrc

System start or when the terminal starts to load environment variables, you can view the contents described in English

vim / etc / profile, add echo / etc / profile in the first row

 

 vim / etc / bashrc, top row add echo / etc / bashrc, once started can be displayed

 

vim ~ / .bashrc, the top row is added echo ~ / .bashrc

 

vim ~ / .bash_profile, the top row is added echo ~ / .bash_profile

 

 /etc/profile.d/ directory, different shell types

 

 Switch to the root user, su - root, it can also be seen that the order in which they perform, not load all SU root profiles, each time a new path variable, source can update status

 

 

 

The second end of treatment.

 

Guess you like

Origin www.cnblogs.com/landesk/p/11619996.html