问题如下,当我们在基于Android源码开发的时候,总免不了会进入到各个模块的子目录下mm一把。然后把编译出来的东西push到手机上对应的目录下面。当时,我还是老老实实的敲 cd xx ....cd xx...mm...adb push xx....,总这么敲这几行重复的命令,时间一长就觉得烦了。看到公司某大牛在做同样事情的时候,老是敲一些我从来没见过的命令,遂菜鸟我厚着脸皮问人家,大牛回一了句:这是我自己写的脚本。当时菜鸟我顿悟啊!虽然之前就了解shell编程,但是没有好好学习过,现在借这个机会一定要好好恶补下了。shell脚本编程对于我们KUB的程序员来说还是很重要的。
source filename [arguments] Read and execute commands from filename in the current shell environment and return the exit sta- tus of the last command executed from filename. If filename does not contain a slash, file names in PATH are used to findthe directory containing filename. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, thePATH is not searched. If any arguments are supplied, they become the positional parameters when file- name is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 ifno commands are executed), and false if filename is not found or cannot be read.
上面讲了一些source的知识,现在说说source的应用场合。这里先打个插,还记得菜鸟我刚刚参加工作的时候,自己按照公司内部的文档,搭建linux上的android开发环境需要配置JDK或者android_SDK的时候,常常需要在~/.bashrc文件的末尾加上export PATH=/PATH/TO/SDK_OR_JDK:$PATH类是这样的命令,然后在source一下这个~/.bashrc ,当时菜鸟对linux的了解连皮毛都谈不上,所以根本不了解这么做的意义。现在接这个机会赶紧的补习一下吧。如果有不明白的同学赶紧学学吧。
man bash 我们会看到下面的解释:
~/.bashrc
The individual per-interactive-shell startup file,可以看出,~/.bash是一个独立的shell环境的启动文件(或者说启动脚本)。在这个~/.bash文件中,保存了一些设置例如别名,环境变量等。每次修改.bashrc后,使用source ~/.bashrc(或者 . ~/.bashrc)就可以立刻加载修改后的设置,使之生效。那么我们修改~/.bashrc文件后,下一次启动linux的时候,~/.bashrc文件中的内容会被执行到吗?又是如何被执行的呢?
下面来看看另一个文件 ~/.bash_profile,man bash 关于这个文件的解释如下:
The personal initialization file, executed for login shells,说实话不知道是什么意思,所以google之,得到如下结论,~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。看重点,该文件仅仅在用户登录的时候执行一次,而且~/.bash_profile文件会去主动执行~/.bashrc。来看下该文件:
/*在我的电脑上,ubuntu11.04,~目录下没有.bash_profile取而代之的是.profile*/
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc" #注意看,点命令
fi
fi
可以看到.profile文件有调用.bashrc,这样就保证我们之前修改过的.bashrc文件,在linux每次启动的时候都能被执行到。而.bashrc文件的内容在启动新的shell进程的时候会被执行。