linux eval和set

148 篇文章 2 订阅

一、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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值