Shell script learning and shell script

Source: Internet
Author: User

Shell script learning and shell script
1111111111111 clear the simple command script of the messages log file under/var/log
/Var/log/the system file under the log file must have the root permission:
$ UID is the global variable of the system:
Make & make install indicates that the previous step is successful.
Make | make install indicates that the previous operation is unsuccessful.
Three methods for clearing logs: Suitable for retaining files and clearing content
① Echo ""> test. log or echo> test. log
②> Test. log
③ Cat/dev/null> test. log
22222222222: Basic knowledge of shell scripts
Shell is a weak language: (loose syntax, not strict) more common
Common shells include standard Bourne shll (sh) and c shell (csh). The Bourne shell (sh) has been replaced by bash shell.
Create a shell script:
Tac/etc/init. d/nfs display the file upside down
Equivalent to head-1/etc/init. d/nfs
① Start with Script
The first line of a standard shell script specifies the program (Interpreter) to execute the content in the script.




333333333333 linux bash programming is generally:
① Interpreter
#! /Bin/bash
Or
#! /Bin/sh
② In linux, sh is equivalent to bash,
#! This is also called magic number. When executing a bash script, the kernel determines which program to use to explain the content in the script.
This line must be the first line at the top of the script. If it is not the first line, it is the comment
In Centos. and RedHat linux, the default shell is bash.
④ If the interpreter is not specified at the beginning of the script, use the corresponding interpreter to execute the script.
Script Annotation


#
Execution of the 44444444444shell script
Shell script execution
When a shell script runs in non-interactive mode, it first looks for the environment variable ENV, which specifies an environment file (usually. bashrc), and then start execution from the environment variable file. After the ENV file is read, SHELL starts to execute the content in the shell script.
The following three methods can be used to execute Shell scripts:
① Bash script-name or sh script-name (recommended)
② Path/script-name or./script-name (execute the script in the current path)
③ Source script-name or. script-name # Note:
The first method is the method that is often used when the script file itself does not have the executable permission (that is, the file X-bit is-). We recommend that you use bash to execute it, or the interpreter is not specified at the beginning of the file,
In the second method, you need to change the permission of the script file to executable (that is, add X-bit files). The specific method is chmod u + x.
Script-name or chmod 755 script-name and then execute the script directly through the script path.
The third method is to use the source or "." Number to read or load the specified shell script file statement, and then execute all the statements in the specified shell script file san. sh in sequence. Statement will be used as the current parent shell script father. sh process. you can change the san. the value of the variable in the sh script, or the return value of the function, is passed to the current parent shell script father. sh. This is the biggest difference between the third method and the first two methods.
Use source or ". "The executed script is loaded with the point number. After the script ends, the variable (including the function) value in the script still exists in the current shell, but sh and bash do not work. Therefore, during shell script development. Use ". "point" or "source" load the script or configuration file at the beginning of the script, and then use the following content to call the script loaded by "source" and the variables and functions in the file.
Exam:
The following commands and returned results are known. The returned result of echo $ user is ()
[Oldboy @ test ~] Cat test. sh
[Oldboy @ test ~] User = 'whoam'
[Oldboy @ test ~] Sh test. sh

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.