linux eval和set

一、Linux eval命令用于重新运算求出参数的内容。

eval可读取一连串的参数,然后再依参数本身的特性来执行。

例如:eval echo "ciao tomorrow" ; ls 既执行了 echo "ciao tomorrow"命令 又执行了ls 命令

[root@master ~]# eval echo "ciao tomorrow" ; ls
ciao tomorrow
2019-01-08.tar   Documents          Pictures           Templates
2.txt            Downloads          playbook_vars.yml  templates.yml
a.csv            fail.yml           Public             template.yml

二、linux set命令用来修改 Shell 环境的运行参数,也就是可以定制环境。

1、直接运行set,会显示所有的环境变量和 Shell 函数,例如:

[root@master ~]# aaa=100
[root@master ~]# echo $aaa
100
[root@master ~]# set | grep aaa
aaa=100

2、set命令的-e参数,linux自带的说明如下:
"Exit immediately if a simple command exits with a non-zero status."
也就是说,在"set -e"之后出现的代码,一旦出现了返回值非零,整个脚本就会立即退出。有的人喜欢使用这个参数,是出于保证代码安全性的考虑。但有的时候,这种美好的初衷,也会导致严重的问题。

[root@master ~]# cat hh.sh
set -e
echo "ciao tomorrow"
ls ls
echo "ciao tomorrow"
[root@master ~]# sh hh.sh
ciao tomorrow
ls: cannot access ls: No such file or directory

ls ls出错,后面一句echo未输出

3、2. set -o pipefail
对于set命令-o参数的pipefail选项,linux是这样解释的:
"If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status,or zero if all commands in the pipeline exit successfully. This option is disabled by default."

设置了这个选项以后,包含管道命令的语句的返回值,会变成最后一个返回非零的管道命令的返回值。听起来比较绕,其实也很简单

参考:

http://blog.51cto.com/sf1314/2062784

猜你喜欢

转载自blog.csdn.net/Man_In_The_Night/article/details/86658305
今日推荐