Day: March 5, 2018

Shell脚本:实用 Shell 文档

ChinaUnix上大神網中人总结的Shell十三问?,强烈推荐,这本书讲得比较精炼,而且都是一些Shell学习中容易把握不住的一些细节难点。每一问都写得非常精彩。 同样是ChinaUnix上,wingger大神整理的Shell基础二十篇。这份文档涉及的内容比较多,我没记错的话应该有很多命令的用法,而且配备实际使用的例子,对初学者帮助甚大。 Shell脚本专家指南也是一本不可多得的好书,我是之前在学校的时候买的这本书。上面都是作者实际工作中的经验总结,你可以从中学到很多其它书上学不到的实践知识。千万不要被专家两个字吓住了,书名的意思是专家给你总结的学习指南。 注:mingxinglai也在他的博客文… Read More

Shell脚本:Bash function 还能这么玩, Something you didn’t know about functions in bash

今天看到一篇讲 Bash function 的有意思的文章,原文在这里。 在 Bash 中一般我们这么定义一个函数: function name () { ... } 这是非常常见的写法,包括我自己在内,一直把他当做类似 Python、C 等语言一样的函数定义语法。实际上这里{ ... }并不代表函数体或者函数的作用域。它只是代表里面的内容是一组命令的集合。了解这点之后,接下来就有一些比较好玩的写法了。 比如下面的函数作用是测试文件是否存在,这里就没用大括号: function fileExists () [[ -f $1 ]] 或者 function isEven () (( $1 % 2 == 0 )) 还有下面的用法: function name () ( ... )… Read More

Shell: 获取函数返回值, Returning value from called function in a shell script

  shell函数不能直接返回字符串,用以下三种方式代替! A Bash function can’t return a string directly like you want it to. You can do three things: Echo a string Return an exit status, which is a number, not a string Share a variable This is also true for some other shells. Here’s how to do each of those options: 1. Echo strings lockdir="somedir" testlock(){ retva… Read