Linux. Shell programming notes-getting started

Source: Internet
Author: User

This note is based on the introduction of linuxShell programming to a book proficient in learning experiments and related notes.

PDF File Download: http://download.csdn.net/detail/ruishenh/6586391

Chapter 1 describes three methods to run a linux program.

1. Run the file directly with executable permissions.

2. directly call the command interpreter to execute the program

3. Use source to execute the file

Linux can execute three types of commands: built-in commands, shell functions, and external commands.

1. the built-in command is the command of the shell program itself. These commands are integrated into the shell interpreter. For example (cd)

2. Shell functions are a series of program code written in shell language, which can be referenced like other commands.

3. External commands are shell-independent execution programs. For example, find, grep, echo. sh. When executing an external command, shell creates a replication process for the current shell to execute. In the execution process, processes are created and extinct. The external command execution process is as follows.

① Use the fork function interface of POSIX system to create a command line shell Process copy (sub-process ).

② In the running environment of sub-processes, find the location of External commands in the linux File System. If the external command provides a full path, skip this step.

③ In the child process, replace shell with a new program to copy and execute (exec). At this time, the parent process enters sleep and waits for the child process to complete execution.

④ After the sub-process is executed, the parent process then reads the next command from the terminal.

Note

(1) The child process is the same as the parent process at the initial stage of creation, but the child process cannot change the parameter of the parent process to child.

(2) Only built-in commands can be used to change the attribute settings of the command line shell (environment variable ).

When you use source to execute shell scripts, instead of creating sub-processes, you can directly execute them in the parent process!

Linux shell variable

Variable is defined in many programming languages and is accompanied by a range of variables. The essence of a variable is a key-value Pair (key = value)

The shell variable name starts with a letter or underline, followed by any length of characters, numbers, or underscores. Unlike many other programming languages, shell variable names have no length restrictions. Linux Shell does not differentiate variables by type. All values are strings, and they are the same as variable names. The values have no character length limit. Bash also allows comparison and integer operations. The key factor is: whether the string value in the variable is a number.

Variable type: local variable and global variable.

Local variables must be specified when declared. Global variables do not need to be modified. (Environment variable)

Echo output

$ echo 'abc'$ echo $JAVA_HOME 

Export Import variable

exportPATH=/usr/local/pig/pig-0.12.0/bin:$PATH 

The env function and the set function are different. The env function displays environment variables, while the set function displays all local variables, including the user's environment variables. For example, if you set callback Var = 123 in the command line, the set function will display the var variable while the Env function does not.

(Var is a local child change, not an environment variable ). If you use the export var = 123 command, the set and Env functions can both display the var variable.

Language type

Computers cannot directly understand advanced languages, but can only directly understand machine languages. Therefore, you must translate advanced languages into computer languages to execute programs written in advanced languages.

Languages are generally compiled and interpreted.

Compiled languages include exe files. Binary file. Or java program. class file (jar file)

Interpretation types include js, python, and shell.

Advantages of Linuxshell

1. Concise

The kernel outer environment of Linux Shell makes any advanced operations possible.

2. Easy Development
Since it follows the unix philosophy, it is easy to develop and optimize it to the present.

3. Ease of transplantation

Can run on any unix/linux

# Is added to the shell script #! Introduce the interpreter to explain

For example

#!/bin/bash #!/bin/sh#!/bin/rm#!/bin/more

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.