Shell Scripting Learning

Source: Internet
Author: User

1111111111111 simple command script to clear messages log file under/var/log
/var/log/the log file when the system files, must have root permissions:
$UID is the global variable of the system:
Make && make install indicates that the previous success was performed and the following
Make | | Make install indicates that the preceding is unsuccessful and executes the following
Three ways to empty the log: suitable for keeping files and emptying content
①echo "" >test.log or Echo > Test.log
②>test.log
③cat/dev/null > Test.log
Basics of 22222222222:shell Scripts
The shell is a weakly typed language: (Syntax loose, not strict) more general
The common Shell has standard Bourne Shll (SH) and C shell (CSH) where the Bourne Shell (SH) has been replaced by the bash shell
Creation of shell scripts:
Tac/etc/init.d/nfs to display the file backwards
Equivalent: Head-1/etc/init.d/nfs
① Script Start
The first line of a canonical shell script indicates which program (interpreter) is executing the contents of the script,




333333333333linux Bash programming is typically:
① Interpreter
#! /bin/bash
Or
#!/bin/sh
② under Linux sh equals bash,
#! is also called magic number, when executing a bash script, the kernel determines which program to use to interpret the contents of the script.
, this line must be the first line at the top of the script, or a comment if it is not the first line
Centos. and Redhat Linux The default shell is bash
④ If you do not specify an interpreter at the beginning of the script, then use the corresponding interpreter to execute the script
Script comments


#号
Execution of 444444444444444shell scripts
Execution of Shell scripts
When a shell script runs in a non-interactive manner, it first looks for an environment variable, env, which specifies an environment file (usually. bashrc), and then starts execution from the environment variable file, and the shell starts executing the contents of the shell script when the env file is read.
The execution of a shell script can typically take the following three ways:
①bash script-name or SH script-name (recommended)
②path/script-name or./script-name (Execute script under current path)
③source Script-name or. script-name#, pay attention.
The first is when the script file itself does not have the executable permission (that is, file X-bit) is often used, which is recommended in bash, or the beginning of the file does not specify the interpreter,
The second method needs to change the permissions of the script file to executable (that is, the file plus x-bit), the method: chmod u+x
Script-name or chmod 755 script-name and then through the script path, you can execute the script directly.
The third method is usually to read in or load the specified shell script file statement using source or the "." Number, and then execute all of the specified shell script file san.sh in sequence. The statement will run as part of the current parent shell script father.sh process, so use source or "." The number of points, etc., can be used in san.sh the value of a variable in its own script, or the return value of a function, etc., to the current parent shell script father.sh, which is the biggest difference between the third method and the first two methods
Loading executed scripts through source or "." Points, the variables (including function) values in the script after the end of the script, still exist in the current shell, and SH and bash are not, so when doing shell scripting development. If there is a requirement in the script to refer to the contents of other scripts, or to configure the file, use the "." Dot or source to load the script or configuration file at the beginning of the script, and then use the contents of the script and the variables and functions in the file that can invoke source loading.
Written questions:
The following command is known and the result is returned, the result of Echo $user is ()
[[Email Protected]~]cat test.sh
[Email protected]~]user= ' WhoAmI '
[[Email Protected]~]sh test.sh

Shell Scripting Learning

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.