Chapter One first knowledge of shell program

Source: Internet
Author: User

1. Three ways to execute shell scripts:

1.1 Make the file executable with executable permissions and run the file directly.

1.1.1 Add x permission to a file

1.1.2./test.sh

1.2 Invoking the command interpreter execution program directly

1.2.1 Source test.sh or. There's a space behind the test.sh.

1.3 Executing a file using source

This way, when the script is executed, the child process is not created and executed directly in the parent process. Both of the above will create child process execution scripts.

During the execution of the script, there is no process creation and extinction. Use the source command when you need to modify the current shell itself variable.

Command type of 2.linux shell

2.1 commands can be executed in 3 ways: Built-in commands, shell functions, and external commands.

(1) The built-in command is the command that the shell program itself contains, and when these commands are executed, there is no process creation and extinction.

(2) The Shell function is a list of program code, written in the shell language, which can be referred to as other commands.

(3) An external command is an executable program that is independent of the shell. such as Find, grep, echo.sh.

3. Variables

The variables in 3.1 Linux are non-differentiated and limited in size, and belong to strings.

3.2 Variables are divided into two parts: local variables and global variables

3.2.1 Local variables are defined with local declaration local aa=10

3.2.2 Environment variables are one of the global variables, global variables are visible in the global scope, and are not required to be declared when defined

Environment variables: In general, each process has its own "environment", which is made up of a set of variables in which the process may need to reference information

Defining Environment Variables: Export

Main parameter:-f//For function name in "variable name"

-N//deletes the specified variable. It is not actually deleted and is not exported to the subsequent execution environment.

-P//list all environment variables that the shell assigns to the program

Behavior pattern: The Export command modifies the environment variables of the current shell process. If the export command is called to execute in a script, the Export command changes the environment of the parent shell process

Not affect the volume. The validity period remains only until the current process dies, and if you want to persist it, you can write the export statement to the shell login startup file.

Bash Startup/Logon files:

/etc/profile//System-wide default value, mostly used to set the environment

/ETC/BASHRC//Bash-specific, system-wide functions and aliases

$HOME/.bash_profile//User defined, environment default settings, can be found in each user's HOME directory

$HOME/.BASHRC//user-defined bash initialization files, in each user's HOME directory. Only interactive shell and user scripts

Before reading this file.

$HOME/.bash_logout//Login files, user-defined instruction files, in each user's HOME directory. It is read when the shell is logged in.

Temporarily define environment variables: env

-I//enables the shell to empty all environment variables inherited by the parent shell when executing the script

-U//Remove a variable from the environment variable

Set//View all variables, including local variables and global variables

unset

-V//default is this option, delete variable

-F//delete the specified function

4. Common Environment variables:

HOME user's exclusive directory, in Linux with "~" extension

Search path for the path external command

Histsize save you are the number of orders

LOGNAME The current user's login name

HOSTNAME refers to the host name

Shell type used by Shell current user

Lang/language language-related environment variables

Mail storage directory for the current user of mail

PS1 basic prompt, root user is "#", Ordinary user is "$". Also the path shown at the beginning

PS2 attached prompt, default is ">"

5.linux Shell is interpreted language

5.1 There are two ways of translating, one is compiling, the other is explaining. The two ways of translating are different, and the compiled language requires a specialized compilation process before the program is executed.

A file that compiles a program into a machine language.

Interpreted language is different, it is translated when the program executes.

Many large and medium-sized programs are written in a compiled language, such as C + +. Java and so on. This is a bit more efficient. The disadvantage is that it is difficult to perform some simple operations on the previous layer because the compilation language

Running with the bottom of the machine

Scripting languages are interpreted languages. such as Shell, Python, awk, and so on. This level of execution is higher than the compiled language, so you can easily perform some advanced operations.

6. Summary:

6.1 Linux shell scripts should be "#! "At first, this machine tells the command-line shell which interpreter to choose to interpret the script. This mechanism provides a programming specification that improves the

The flexibility to write scripts, for example, you can choose to write scripts in other languages

The 6.2 Linux shell runs in the environment. Environment variables are read from the startup file when a user logs on to start the first shell (login shell). Different shell startup files are not the same. Environmental change

Can be changed by the Export command during operation. The env command, in order to run commands, can temporarily create new environment variables.

6.3 The Linux shell runs, it creates a child process that is exactly the same as the parent process, and the child process's environment variable inherits the parent process. Operations on their environment variables in all child processes do not affect

to the parent process. For example, the execution of a CD command only alters the environment variables of a child process.

6.4 $PATH is one of the environment variables of Linux. $PATH often contain the directory where the Linux executables are located, and when the shell accepts the command to find the command as a non-internal command, and gives

The full path, the command file is searched in the $path variable once, in the order of the previous to the back, until it is found.

6.5 You can write changes to the environment variables in export to/etc/profile or $home/.profile. The latter priority is higher than the former. For example, export path= $HOME/bin: $PATH,

That is, add the $home/bin directory to the $path path, which automatically loads the change of the environment variable each time the user logs in.

6.6 Compiled languages are more efficient than interpreted languages. However, the explanatory language is higher than the compiled language in terms of ease of development, portability and simplicity. Because the Linux shell runs with

Kernel, so you can easily perform advanced operations on some file systems. The specific language used depends on the needs of the program.

6.7 Unix/linux's shell language is a widely used scripting language, common in system maintenance. If a reasonable use of the script is maintained within the scope supported by the POSIX interface, you may get a high

of portability. If you have a problem in time, you can make a small change to do what you want to do.

Chapter One first knowledge of shell program

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.