Use the source Command in Linux

Source: Internet
Author: User
Tags builtin
Recently, Cainiao is learning shell programming. it seems that it is too late. it is just a matter of time for BS to start programming. In the process of learning shell programming, I encountered a very mentally retarded problem. I had consulted others but was ruthlessly BS. I decided to come back and understand it. The problem is as follows. when we develop the SDK based on the Android source code, we will inevitably go to the sub-directories of each module, which will be one of mm. Then I compiled the code for Cainiao to learn shell programming recently. it seems that it is too late for BS to write it to myself. In the process of learning shell programming, I encountered a very mentally retarded problem. I had consulted others but was ruthlessly BS. I decided to come back and understand it.

The problem is as follows. when we develop the SDK based on the Android source code, we will inevitably go to the sub-directories of each module, which will be one of mm. Then, push the compiled items to the corresponding directory on the mobile phone. At that time, I still honestly typed cd xx .... cd xx... mm... adb pushxx ...., these repeated commands are annoying for a long time. When I saw a big bull in the company doing the same thing, he always typed some commands that I had never seen before. so I asked the guy with a thick face, and the great ox replied: this is my own script. I had an epiphany when I was a Cainiao! Although I have been familiar with shell programming before, I have never studied it well. now I have to take this opportunity to make up for it. Shell script programming is very important for our KUB programmers.

I wrote a simple shell script, as shown below, to quickly switch to the directory where the android. mk file of an Android module is located.

#! /Bin/bash

# Cd2c. sh

Cd/file/path

But when I run this script (before running the script, I need to add the execution permission chmod + xcd2c to the script. sh). I found that the execution results could not be switched to the expected Directory. at that time, I was depressed. then I tried sourcecd2c again. sh, and then the cd is in the/file/path directory. then I asked Daniel next to it, and Daniel replied, as if it had something to do with the parent shell and the child shell. then I asked again, why do you just execute the command directly? I didn't even see what source command you used. Daniel replied: Because I use alias, which is also an alias. After I asked this question, I felt how sb I was. I should review myself here. Daniel is very busy. if you don't have any questions, ask others, find the cause and try to solve it by yourself. do not seek help from others immediately. this will not help us improve our problem-solving capabilities, but will also delay others' time.

Later, I went online and found some materials to learn. the summary is as follows:

1. the source command and the dot (.) command are actually the same thing. for example, I want to execute a shell script in the current directory, source filename. sh and

../Filename. sh is the same effect. Note that there is a space between the first vertex and the second vertex in the command (this is a good idiot's annotation, so everyone should not make a mistake ).

2. let's see what the souce command has done. let's take a look at the details of the source command.

Source filename [arguments] Read and execute commands from filename in thecurrent shell environment and return the exit sta-tus of thelast command executed from filename. if filename does not contain aslash, file names in PATH are used to find the directorycontaining filename. the file searched for in PATH need notbe executable. when bash is not in posix mode, the currentdirectory is searched if no file is found in PATH. if thesourcepath option to the shopt builtin command is turned off, thePATH is not searched. if any arguments are supplied, theybecome the positional parameters when file-name is executed. otherwise the positional parameters are unchanged. the returnstatus is the status of the last command exited within thescript (0 if no commands are executed), and false if filenameis not found or cannot be read.

After reading the first sentence above, we should have understood the meaning of souece. the source command will read all the filename commands into the current shell environment and execute them in the current shell environment, therefore, source cd2c. sh will get the expected result, because cd2c. the commands in sh are executed in the current shell environment.

3. let's see what a shell script has done if it runs directly?

In the current shell environment (we call it the parent shell), if you execute a shell script, you will do the following:

A. The current shell process (parent shell) fork/exec is a sub-shell used to execute the command in filename. at this time, the parent shell process wait waits for the sub-shell to return.

B. The sub-shell reads the command and starts to execute the builtin command cd.

C. The sub-shell reads the end of the file, terminates execution, and returns to the parent shell process.

We can see that when executing a shell, the cd/file/path command is indeed executed, but it is executed in the sub-shell process, after the execution is complete, the sub-shell terminates and returns to the parent process. Therefore, the expected results are returned at this time.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------

I have talked about some source knowledge. now I will talk about the application of source. Here, we will first insert a plug-in. remember that when I first joined the work of Cainiao and set up the android development environment on linux according to the internal documents of the company, I needed to configure JDK or android_SDK, usually in ~ /. Add the export PATH =/PATH/TO/SDK_OR_JDK: $ PATH class TO the end of the bashrc file. then, run the following command in the source file ~ /. Bashrc. at that time, Cainiao was not familiar with linux, so he did not understand the meaning of this operation. Now let's take a quick look at this opportunity. If you have any questions, hurry and learn.

Man bash:

~ /. Bashrc

Theindividual per-interactive-shell startupfile, you can see ,~ /. Bash is a startup file (or startup script) of an independent shell environment ). In this ~ The/. bash file stores some settings, such as aliases and environment variables. Use source ~ /. Bashrc (or .~ /. Bashrc) immediately load the modified settings to make them take effect. Then we modify ~ /. After the bashrc file, when linux is started next time ,~ /Will the content in the bashrc file be executed? How is it executed?

Next let's take a look at another file ~ /. Bash_profile, man bash:

Thepersonal initialization file, executed for loginshells. to tell the truth, I don't know what it means, so google will come to the following conclusion ,~ /. Bash_profile: each user can use this file to input the shell Information dedicated to their own use. when a user logs on, this file is only executed once! By default, it sets some environment variables to execute the user's. bashrc file. Focus: The file is executed only once when the user logs on ~ /. The bash_profile file will be automatically executed ~ /. Bashrc. Let's take a look at this file:

/* On my computer, ubuntu11.04 ,~ No. bash_profile under the Directory is replaced by. profile */

# Exists.
# See/usr/share/doc/bash/examples/startup-files for examples.
# Thefiles are located in the bash-doc package.

# The default umask is set in/etc/profile; for setting the umask
# Forssh logins, install and configure the libpam-umask package.
# Umask022

# Ifrunning bash
If [-n "$ BASH_VERSION"]; then
# Include. bashrcif it exists
If [-f "$ HOME/. bashrc"]; then
. "$ HOME/. bashrc" # Note: Click the command.
Fi
Fi

We can see that the. profile file is called. bashrc, which ensures that the. bashrc file we modified before can be executed every time linux starts. The contents of the. bashrc file will be executed when a new shell process is started.

------------------------------- Write it here! It seems that I am really a man with a lot of nonsense. I wrote so much nonsense about such simple things ,:-)! If you find any mistakes I have written, you must correct me to avoid misleading Cainiao like me.

Goodnight everyone! Quote Lao Luo's words, love life, love android ......

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.