exec eval source
来源:互联网 发布:淘宝店最高等级是什么 编辑:程序博客网 时间:2024/04/30 15:04
转自:http://zhgw01.blog.163.com/blog/static/10414812200912025735381/
eval
eval "translates" a value buried inside a variable, and then runs the command that was buried in there
eval arg1 [arg2 arg3]就是将参数组合起来,并替换其中的变量,这样得到的结果作为一条命令进行运行
for i in 1 2 3do eval myvar="$i" echo "$myvar"done# this gives 123# why? because there is no metavalue or special meaning to 1 or 2 or 3
for i in ls dfdo eval myvar="$i" echo "$myvar"done# here you get output from the ls command and the df command如果不使用eval的话,结果就是ls跟df了
另外一个用eval要注意的是:
比如 nn=aa; aa=final; eval dn=\$$nn; echo $dn 会给出final
但是 eval dn=$"$nn";echo $dn给出 aa
eval dn="$"$nn; echo$dn给出final
原因据说是eval对\后面或者“”里面的字符串不进行扩展,而仅仅只是在其它需要扩展的扩展完成后将“”或者\去掉,最后再作为命令执行
所以eval dn="$"$nn先扩展出dn="$"aa,再去点""得到dn=$aa,然后将这个作为命令执行
eval dn=$"$nn"由于“”不进行扩展,而$"xxx"得到的结果是xxx,所以得到dn=$nn作为命令执行
exec
exec starts another process - BUT - it exits the current process when you do this kind of thing
使用exec,shell就不会调用fork,直接以后面的命令的内容代替当前shell的代码
#!/bin/bashexec echo "leaving this script forever $0" # Exit from script here.# ----------------------------------# The following line never happensecho "This echo will never echo."
source
When you run a command in the shell - like another script or a command like ls -
the shell creates a subprocess (called child process). Any environmentvariable that got defined or changed down in the child is LOST FOREVERto the parent process.
However if you source a script (there are two ways) you force thescript to run in the current process. That means environment variablesin the script you ran are NOT LOST.
# script1MYVAR="HI there!"# end script1
# script2# first, run script 1script1# now is the MYVAR variable set? No -why? it got set in the child, when the child exited it GOT LOSTecho "MYVAR= $MYVAR"# now source script1 :: putting a lonesome dot or the command "source" # in front of the script name have the same effect. script1# . script1 == source script1 these two are the SAME THINGsource script1# now is the MYVAR variable set? This time we see that MYVAR has a valueecho "MYVAR= $MYVAR"
- eval, exec, source区别
- exec eval source
- bash shell: the difference among 'exec', 'eval', 'source'
- exec和eval语句
- python exec和eval
- eval+exec+compile
- python exec/eval/execfile
- Python exec, eval
- Python exec 和 eval
- eval、exec、execfile
- Exec 与 eval 语句
- eval exec execfile 使用方法
- exec 与 eval
- Python 中的 exec 和 eval
- python eval,和 exec模块
- 内建函数-eval & exec
- python exec, eval 方法整理
- exec eval filter lambda set
- s:checkboxlist s:checkbox 初始值设置
- 【Java TCP/IP Socket】UDP Socket
- TextView键盘输入 弹出指定高度 点击return收回虚拟键盘
- SSL证书SOCKET登陆JAVA
- python 加载libpython2.6.so.1.0 报错
- exec eval source
- mysql表排重
- SPOJ 371 Boxes 最小费用流
- DVD标准参考网站
- 机器学习完整过程案例分布解析,python代码解析
- 无可用源||调试时源文件与模块生成时的文件不同||无法单步调试
- 经纬财富:淮北纸?白?银?投?资?的?十?八?条?小?技?巧
- 深入理解JavaScript系列 ----(9):根本没有“JSON对象”这回事!
- js读取xml数据