最近用shell script写了一个100行的脚本,用于自动启动 data recording service,自动打包上传数据。 用shell script做一些轻量级的task确实好用,但就是有不少坑。
目前掌握了通过flag传参,构造function,通过shell script完成一系列的自动化任务。
- 比较字符串 vs 比较数值
比较字符串:if [ $string == "true" ]; then
一定要有空格!
比较数值:[ $x -ne 0]
- 路径问题
如果用crontab启动脚本的话,默认的pwd是home. 需要自己改成project directory.
ProjectDir=$(cd "$(dirname "$0")";pwd)
#change working directory to the prject folder.
echo "pwd:${ProjectDir}"
cd ${ProjectDir}
- getopts
通过加flag从而更灵活地传入参数。
https://segmentfault.com/a/1190000010171506
http://blog.chinaunix.net/uid-21505614-id-2173248.html