- 浏览: 4732633 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
我使用过的Linux命令之source - 在当前shell环境中执行指定文件中的命令
本文链接:http://codingstandards.iteye.com/blog/837935 (转载请注明出处)
用途说明
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 ~]#
问题思考
相关资料
【1】笑遍世界的测试技术 Linux Source命令及脚本的执行方式解析
【2】Simon的专栏 Linux中source命令的用法
【3】LinuxSense linux的source命令
【4】jiakechong linux source命令
发表评论
-
在Linux用tar归档压缩文件时忽略某些文件和目录
2013-02-01 10:19 16927在Linux下,常用tar对文 ... -
使用nmap扫描服务器端口的一次操作
2012-11-01 17:00 15075使用nmap扫描服务器端口的一次操作 本文来自:http ... -
我使用过的Linux命令之wget - ooo
2011-09-14 13:10 0我使用过的Linux命令之wg ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:53 9084简明 Vim 练级攻略 http://coolshell.c ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:49 1简明 Vim 练级攻略 http://coolshell.c ... -
我使用过的Linux命令之:(冒号) - 啥也不做(除了……)
2011-08-29 13:18 11984我使用过的Linux命令之: ... -
我使用过的Linux命令之date - 显示、修改系统日期时间
2011-08-25 09:21 41905我使用过的Linux命令之da ... -
我使用过的Linux命令之declare - 声明shell变量(不知道没关系、知道了就更好的内建命令)
2011-08-16 09:22 21740我使用过的Linux命令之declare - 声明shell变 ... -
我使用过的Linux命令之alias - 设置命令的别名,让 Linux 命令更简练
2011-08-11 09:31 28716我使用过的Linux命令之alias - 设置命令的别名,让 ... -
我使用过的Linux命令之ar - 创建静态库.a文件
2011-08-08 10:40 51815我使用过的Linux命令之ar - 创建静态库.a文件 本 ... -
我使用过的Linux命令之crontab - 设置例行任务(类似于Windows中的任务计划)
2011-08-04 22:26 9691我使用过的Linux命令之crontab - 设置例行任务(类 ... -
我使用过的Linux命令之chmod - 改变文件/目录的访问权限
2011-08-03 21:33 10641我使用过的Linux命令之chmod - 改变文件/目录的访问 ... -
我使用过的Linux命令之export - 设置或显示环境变量
2011-08-02 19:55 25370我使用过的Linux命令之export - 设置或显示环境变量 ... -
我使用过的Linux命令之wc - 统计文件行数、单词数或字节数
2011-07-26 10:50 28932我使用过的Linux命令之wc - 统计文件行数、单词数或字节 ... -
我使用过的Linux命令之groupdel - 删除群组
2011-07-22 22:13 9620我使用过的Linux命令之gr ... -
我使用过的Linux命令之ifconfig - 网络配置命令
2011-07-21 20:43 60373我使用过的Linux命令之ifconfig - 网络配置命令 ... -
我使用过的Linux命令之ll - 列出文件/目录的详细信息
2011-07-20 21:22 7103我使用过的Linux命令之ll ... -
我使用过的Linux命令之mkdir - 创建目录
2011-07-20 20:58 13605我使用过的Linux命令之mkdir - 创建目录 本文链 ... -
我使用过的Linux命令之perror - 解释错误代码
2011-07-18 20:29 24967我使用过的Linux命令之perror - 解释错误代码 ... -
我使用过的Linux命令之ping - 测试与目标主机的连通性
2011-07-16 10:46 26256我使用过的Linux命令之ping - 测试与目标主机的连通性 ...
相关推荐
android adb shell 命令大全 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): android create avd --name 名称 --...
� 采用了对有限内存、电池和 CPU 优化过的虚拟机 Dalvik , Android 的运行速度比想象的要快很多。 � 运营商(中国移动等)的大力支持,产业链条的热捧。 � 良好的盈利模式( 3/7 开),产业链条的各方:运营商、...
运行命令Sudo rz,即是接收文件,xshell就会弹出文件选择对话框,选好文件之后关闭对话框,文件就会上传到linux里的当前目录 。 运行命令Sudo sz file 就是发文件到windows上(保存的目录是可以配置) 比ftp命令方便...
在Perl脚本中使用eval命令来强制变量替换 11-15. 使用set来改变脚本的位置参数 11-16. 重新分配位置参数 11-17. Unset一个变量 11-18. 使用export命令传递一个变量到一个内嵌awk的脚本中 11-19. 使用getopts命令来...
与该功能相关的Linux命令:info命令 – 阅读info格式的文件source命令 – 在当前Shell环境中从指定文件读取和执行命令stat命令 – 显示文件状态信息sudoreplay命令 – 重播sudo会话日志dpkg-query命令 – 在dpkg...
与该功能相关的Linux命令:readonly命令 – 标记shell变量或函数为只读vipw命令 – 编辑某些配置文件dpkg-split命令 – 分割软件包resize命令 – 设置终端机视窗的大小source命令 – 在当前Shell环境中从指定文件...
(一般无需手动执行此命令,在运行 adb 命令时若发现 adb server 没有启动会自动调起。) 停止 adb server 命令: adb kill-server 查看 adb 版本 命令: adb version 示例输出: Android Debug Bridge version ...
该函数库实现了基本的解释器,它有一套实现变量、流程控制和过程的核心脚本命令,而且还有一组用来存取操作系统服务以运行其他程序、存取文件系统和使用网络套接字的命令。Tcl和Tk提供了一台可以在UNIX、Windows和...
本来命令是通过键盘得到输入的,但是用小于号,就能够使命令从文件中得到输入。 \ 表示未写完,回车换行再继续。 * 匹配零个或者多个字符。 ? 匹配一个字符。 [] 匹配中括号里的内容[a-z][A-Z][0-9]。 ! 事件...
如果未指定文件系统,将使用现有的文件系统格式。 Map 显示驱动器号与物理设备名称的映射。该信息在运行 fixboot 和 fixmbr 命令时非常有用。 map 命令仅在使用故障恢复控制台时才可用。 Map [ arc] ...