via: http://blog.csdn.net/gui694278452/article/details/8870469
via: http://blog.csdn.net/simon_dong618/article/details/1581132
用途说明
source命令是bash中的内建命令,它等同于点命令(.),用于读取和在当前shell环境中执行指定文件中的命令,执行完毕之后退出码为该文件中 的最后一个命令的退出码(Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.)。指定的文件可以没有执行权限。
在当前shell中执行和在子shell中执行的区别是,后者定义的变量和函数在执行结束后就消失了,而前者却可以保留下来。有时候我们修改了/etc /profile里面的内容,如增加了环境变量,那么要立即生效的话,就必须使用source命令或者点命令在当前shell中执行一下。
常用参数
格式: . filename [arguments]
格式: source filename [arguments]
在后面的示例中会分别对各种情况举例演示。
使用示例
示例一
[root@web imx_web3q]# help source
source: source filename [arguments]
Read and execute commands from FILENAME and return. The pathnames
in $PATH are used to find the directory containing FILENAME. If any
ARGUMENTS are supplied, they become the positional parameters when
FILENAME is executed.
[root@web imx_web3q]#
示例二 修改/etc/profile之后使之立即生效
[root@web imx_web3q]# vi /etc/profile
[root@web imx_web3q]# . /etc/profile
[root@web imx_web3q]#
示例三 在PATH中搜索命令
man source中说道:如果filename不包含斜杠(/),那么从PATH环境变量指定的那些路径搜索filename,这个文件不必是可执行 的。(If filename does not contain a slash, file names in PATH are used to find the directory containing filename. The file searched for in PATH need not be executable.)如果在PATH中找不到指定文件,当bash不是posix模式时,将在当前目录下搜索该文件。(When bash is not in posix mode, the current directory is searched if no file is found in PATH.)如果shopt里的sourcepath关闭,则不在PATH中搜索指定文件。(If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched.)
[root@new55 ~]# shopt
cdable_vars off
cdspell off
checkhash off
checkwinsize on
cmdhist on
dotglob off
execfail off
expand_aliases on
extdebug off
extglob on
extquote on
failglob off
force_fignore on
gnu_errfmt off
histappend off
histreedit off
histverify off
hostcomplete off
huponexit off
interactive_comments on
lithist off
login_shell on
mailwarn off
no_empty_cmd_completion off
nocaseglob off
nocasematch off
nullglob off
progcomp on
promptvars on
restricted_shell off
shift_verbose off
sourcepath on
xpg_echo off
[root@new55 ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/opt/apache/apache-ant-1.8.1/bin:/usr/java/jdk1.6.0_21/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin :/root/bin
[root@new55 ~]# ls -l /usr/bin/sj.sh
-rwxr-xr-x 1 root root 453 09-15 04:46 /usr/bin/sj.sh
[root@new55 ~]# cat /usr/bin/sj.sh
#!/bin/sh
listpids()
{
#ps -ef|grep java|grep -v grep
COLUMNS=1024 ps h -C java -f
}
showpids()
{
while read u p pp t1 t2 tty cpu cmd;
do
ls -l /proc/$p/cwd
echo $p $cwd $cmd
echo
done
}
showpidof()
{
while read u p pp t1 t2 tty cpu cmd;
do
if ls -l /proc/$p/cwd | grep -q $1; then
echo $p
elif echo $cmd | grep -q $1; then
echo $p
fi
done
}
if [ "$1" ]; then
listpids | showpidof $1 | xargs
else
listpids | showpids
fi
[root@new55 ~]# sj.sh
lrwxrwxrwx 1 root root 0 12-09 19:11 /proc/6832/cwd -> /root/work55/cms_server
6832 0:02 /usr/java/jdk1.6.0_21/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start
[root@new55 ~]# listpids
-bash: listpids: command not found
[root@new55 ~]# chmod -x /usr/bin/sj.sh
[root@new55 ~]# source sj.sh
lrwxrwxrwx 1 root root 0 12-09 19:11 /proc/6832/cwd -> /root/work55/cms_server
6832 0:02 /usr/java/jdk1.6.0_21/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start
[root@new55 ~]# listpids
root 6832 5994 0 19:11 pts/2 Sl+ 0:02 /usr/java/jdk1.6.0_21/jre/bin/java -classpath /opt/apache/apache-ant-1.8.1/lib/ant-launcher.jar -Dant.home=/opt/apache/apache-ant-1.8.1 -Dant.library.dir=/opt/apache/apache-ant-1.8.1/lib org.apache.tools.ant.launch.Launcher -cp start
[root@new55 ~]# chmod +x /usr/bin/sj.sh
root@new55 ~]#
示例四 位置参数
If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged.
[root@new55 ~]# cat >source.sh
XYZ=123
echo "args: $@"
Ctrl+D
[root@new55 ~]# ./source.sh
-bash: ./source.sh: 权限不够
[root@new55 ~]# . source.sh
args:
[root@new55 ~]# echo $XYZ
123
[root@new55 ~]# . source.sh hello world
args: hello world
[root@new55 ~]#
示例五 退出码
The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.
[root@new55 ~]# ls x.sh
ls: x.sh: 没有那个文件或目录
[root@new55 ~]# . ./x.sh
-bash: ./x.sh: 没有那个文件或目录
[root@new55 ~]# echo $?
1
[root@new55 ~]# cat >x.sh
return 13
Ctrl+D
[root@new55 ~]# . ./x.sh
[root@new55 ~]# echo $?
13
[root@new55 ~]#
-------------------------------------------------------------------------------
source命令也称为“点命令”,也就是一个点符号(.)。source命令通常用于重新执行刚修改的初始化文件,使之立即生效,而不必注销并重新登录。
用法:
source filename 或 . filename
source命令除了上述的用途之外,还有一个另外一个用途。在对编译系统核心时常常需要输入一长串的命令,如:
make mrproper
make menuconfig
make dep
make clean
make bzImage
…………
如果把这些命令做成一个文件,让它自动顺序执行,对于需要多次反复编译系统核心的用户来说会很方便,而用source命令就可以做到这一点,它的作用就是把一个文件的内容当成shell来执行,先在linux的源代码目录下(如/usr/src/linux-2.4.20)建立一个文件,如make_command,在其中输入一下内容:
make mrproper &&
make menuconfig &&
make dep &&
make clean &&
make bzImage &&
make modules &&
make modules_install &&
cp arch/i386/boot/bzImage /boot/vmlinuz_new &&
cp System.map /boot &&
vi /etc/lilo.conf &&
lilo -v
文件建立好之后,每次编译核心的时候,只需要在/usr/src/linux-2.4.20下输入: source make_command
即可,如果你用的不是lilo来引导系统,可以把最后两行去掉,配置自己的引导程序来引导内核。
顺便补充一点,&&命令表示顺序执行由它连接的命令,但是只有它之前的命令成功执行完成了之后才可以继续执行它后面的命令。
相关推荐
Linux_3.x.x_4.x.x_VCP_Driver_Source.zip 是一个包含Linux设备驱动程序源代码的压缩包,专门针对CP210X系列USB转串口芯片。这个驱动程序适用于Linux内核版本3.x.x到4.x.x,旨在使系统能够识别并正确通信与使用基于...
Linux 的 source 命令是一种常用的命令,用于读取并执行指定文件中的命令,但是有时候在使用 source 命令时,可能会出现找不到命令的情况。今天我们将为大家介绍 Linux 的 source 命令找不到的解决方法。 问题描述 ...
### Linux Shell 命令详解 #### 文件与目录操作命令 **ls命令**:用于显示当前目录中的文件和目录信息,或指定目录的信息。 - `ls`:显示当前目录下的文件和目录。 - `ls -a`:显示所有文件,包括以`.`开头的隐藏...
`yum update_source`是`yum`命令的一个特定用法,涉及到的是源码更新的过程。本文将深入探讨`yum`的基本操作,`update_source`的含义,以及如何使用`update_source.sh`脚本来维护和更新系统源。 首先,`yum`的核心...
在"linux_device_drvier_source.zip"这个压缩包中,包含的是一系列关于Linux设备驱动编程的源代码和可能的章节文件。这些章节可能涵盖了许多关键概念和技术,下面将详细阐述其中可能涉及的知识点。 1. **设备模型**...
安装OpenJDK 8U-jdk_x64_linux_hotspot_8u265b01.tar.gz通常涉及下载压缩包,然后在Linux终端中通过tar命令解压到指定目录,接着将解压后的路径添加到系统PATH环境变量中,以便在任何地方运行Java命令。 总之,...
完成编辑后,执行`source ~/.bashrc`或`source ~/.bash_profile`使修改生效。 然后,你可以通过输入`jprofiler`命令启动JProfiler。首次运行,JProfiler会提示你创建一个新的配置文件,按照提示进行即可。 ...
Linux_3.x.x_VCP_Driver_Source.zip是一个包含CP210X USB转UART驱动程序源码的压缩包,主要用于在Linux操作系统上实现USB到串行接口的通信。CP210X是一款由Silicon Labs(原名Cypress Semiconductor)开发的USB到...
在Linux系统中,`ping`命令是一个非常基础且重要的网络诊断工具,用于检查网络连通性。它通过发送ICMP(Internet Control Message Protocol)回显请求数据包到目标主机,并等待接收回应,以此来判断目标主机是否可达...
编译成功后,他们可以通过insmod或modprobe命令将驱动加载到内核,从而使Linux系统能够识别和使用AX88772C控制器,实现USB接口的网络连接功能。 总的来说,AX88772C_Ethernet_asix_linux driver_usb是一个针对ASIX ...
如果"change_source"角色有依赖于其他角色,可以在"requirements.yml"文件中列出,然后通过`ansible-galaxy install -r requirements.yml`命令安装。 六、角色的使用与调用 在Ansible的主剧本中,可以通过`- name: ...
- 重新加载配置:`source ~/.bashrc` 或 `source ~/.bash_profile`。 - 检查安装:`java -version`,应显示OpenJDK 11的信息。 **开发与调试**: - 使用`javac`命令进行编译,`java`命令运行程序。 - `javadoc`用于...
source /etc/profile ``` 使用 Libreoffice 转换 Word 到 PDF 使用 Libreoffice 转换 Word 到 PDF 格式,可以采用以下方式: ``` soffice --headless --convert-to pdf:writer_pdf_Export /wordtopdf/yx.docx --...
标题中的"wc.rar_linux_unix wc实现_wc -m 源码_wc source code_wc unix"指出,这个压缩包包含了与Linux和Unix系统中的`wc`命令相关的源代码,特别是关于`wc -m`选项的实现。`wc`命令是Unix和Linux系统中用于统计...
在命令行界面,使用tar命令可以轻松完成这一过程,例如:“tar -zxvf Sourcetrail_2020_1_117_Linux_64bit.tar.gz”。 总的来说,Sourcetrail是一款针对开发者设计的强大工具,它提升了代码浏览和导航的效率,简化...
Linux 的 source 命令找不到解决方法 Linux 系统中,source 命令是一个非常常用的命令,用于读取和执行指定文件中的命令。然而,有时候我们可能会遇到 source 命令找不到的情况,今天我们就来讨论这个问题。 问题...
### Linux入门常用命令详解 在IT领域,尤其是系统管理和软件开发中,掌握Linux操作系统的基本命令是必不可少的技能。本文将深入解析Linux系统中的一些基础但极为重要的命令,这些命令是每位初学者和专业人员都应该...
修改后,执行 `source ~/.bashrc` 或 `source /etc/profile` 使更改生效。 4. **验证安装**: 使用 `java -version` 和 `javac -version` 命令检查Java和Java编译器的版本,如果正确安装,会显示JDK1.8.0_291的...
2. **解压源代码**:使用`tar`命令解压缩文件`NAMD_2.14_Source.tar.gz`,例如`tar -zxvf NAMD_2.14_Source.tar.gz`。 3. **配置编译**:进入解压后的目录,使用`configure`脚本来设置编译选项,比如指定编译器、库...
5. **多平台兼容**:large_image_source_pil是跨平台的,可以在Windows、Linux和Mac OS等操作系统上运行,提供一致的用户体验。 三、安装与使用 在Python环境中,安装large_image_source_pil库非常简单,可以通过...