码迷,mamicode.com
首页 > 其他好文 > 详细

回忆曾经写过的第一个Bash脚本

时间:2014-08-01 00:01:00      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:linux   bash   

这盘文章,算是杂谈吧,想谈谈我对于Linux操作系统的灵魂之笔 Bash的理解。

 据说Shell脚本有50种之多(陈皓老师的酷壳网上有一篇博文 http://coolshell.cn/articles/8619.html 提到),现在,用Linux调试大大小小程序,除了使用Makefile,偶尔用用CMakeList.txt, 更多程序是用bash 脚本来实现半自动化调试,就像在VS下面,每次修改重新编译一样,你总是 gcc 或者 g++,然后 ./test 运行,然后再看看有没有错,编译出错又得修改, rm -rf test, 很是繁琐,如果 一个小巧的脚本 test.sh 即可搞定:

#!/bin/bash

gcc -o test -g test.c

if [ -e test ];then
   ./test
   rm test
fi


这样,你只要每次执行脚本  sh test.sh 即可调试程序,如果出错直接vim 或 gedit 修改即可。


关于更直白的应用,可参见我之前写的一篇文章,上面有 Shell脚本,Makefile在Linux环境下安装编译和调试程序的示例,诚然,你用gdb调试 那是更深入的话题了。


啰嗦了这么多,还是切入主题吧,即我当年在工作中的第一个使用的脚本,即将主板上系统中的相关文件进行解压更新,亦或是系统的版本更新,但这个第一次使用的Shell脚本存在很多不足之处

1. 缺乏必要的脚本使用说明部分,如输入命令 sh test.sh, 若是需要参数,就需要加入必要的判断:

if [ $# -ne 1 ];then  

   echo "Usage: ......"

fi


2. 变量名风格不统一,按照我现在的思维,目录的变量名最好都大写,而其他变量名小写,不过最好是单词

 

3. 没有命令执行错误 输出到log 的习惯,其实在一些重要命令 执行后,加上 >error.log  2>&1 可以帮助分析,也可以写个脚本来对error.log 进行自动化分析。


总之能,本篇杂谈也就是感慨一下第一个用于工作的脚本是多么的拙劣,不过,人总是在进步,所以 生活也因勤奋好学而精彩~


共勉!!!


#! /bin/bash
# By GuJinjin
# 2010/08/11

# Define Path Variables
cm_DIR=/export/bta/canmore
fs_DIR=/export/bta/canmore/fsroot
host_DIR=/export/host
sdks_DIR=/mnt/bta/sdks

# define variable
b1=cm
b2=sdv
b3=gvl

# cleanup fsroot
rm -rf $fs_DIR/*


# Update fsroot 
# $1 is the name of archive
# $2 is the build number
tar -zxvf $host_DIR/$1 -C $fs_DIR/


# Update oflash
cp -rf $sdks_DIR/oflash $fs_DIR/
echo "********** cp oflash successfully ! **********"


# Update redboot_flash_nor.bin or redboot.bin
if [ -e $sdks_DIR/$2/redboot_flash_nor.bin ];then
	cp -rf $sdks_DIR/$2/redboot_flash_nor.bin $fs_DIR
	echo "********** cp redboot_flash_nor.bin successfully ! ********** "

elif [ -e $sdks_DIR/$2/redboot.bin ];then
	cp $sdks_DIR/$2/redboot.bin $fs_DIR/redboot_flash_nor.bin
	echo "********** cp redboot.bin successfully ! ********** "
else 
	echo "********** ERROR ! ********** No such a related file! ********** "
fi



# Update cefdk,config for cm, sdv & gvl
if [ "$3" = "$b1" ];then
	cp -rf $sdks_DIR/conf.canmore $fs_DIR/
	cp -rf $sdks_DIR/$2/gen3.bin $fs_DIR
	echo "********** cp conf.canmore successfully ! ********** "

elif [ "$3" = "$b2" ];then	
	cp -rf $sdks_DIR/conf.sodaville $fs_DIR
	cp -rf $sdks_DIR/$2/gen4.bin $fs_DIR
	echo "********** cp conf.sodaville successfully ! ********** "

elif [ "$3" = "$b3" ];then
	cp -rf $sdks_DIR/conf.groveland $fs_DIR/
	cp -rf $sdks_DIR/$2/gen4.5.bin $fs_DIR
	echo "********** cp conf.groveland successfully ! ********** "

else 
	echo  "********** Error Input parameters ! **********"
fi


# Update 101,102,103 for sdv or cm
if [ "$4" = "3" ];then
	cp -rf $sdks_DIR/$2/bzImage /tftpboot/101
	cp -rf $sdks_DIR/$2/bzImage /tftpboot/102
	cp -rf $sdks_DIR/$2/bzImage /tftpboot/103

	rm -rf $cm_DIR/fsroot10*
	cp -rf $cm_DIR/fsroot $cm_DIR/fsroot101
      cp -rf $cm_DIR/fsroot $cm_DIR/fsroot102
      cp -rf $cm_DIR/fsroot $cm_DIR/fsroot103
	echo "********** cp bzImage fsroot10* for 101,102,103 successfully ! **********"

elif [ "$4" = "2" ];then

      cp -rf $sdks_DIR/$2/bzImage /tftpboot/101
      cp -rf $sdks_DIR/$2/bzImage /tftpboot/102

	rm -rf $cm_DIR/fsroot101
	rm -rf $cm_DIR/fsroot102
      cp -rf $cm_DIR/fsroot $cm_DIR/fsroot101
      cp -rf $cm_DIR/fsroot $cm_DIR/fsroot102
	echo "********** cp bzImage fsroot10* for 101,102 successfully ! **********"

elif [ "$4" = "1" ];then

      cp -rf $sdks_DIR/$2/bzImage /tftpboot/103

	rm -rf $cm_DIR/fsroot103    
	cp -rf $cm_DIR/fsroot $cm_DIR/fsroot103
	echo "*********** cp bzImage fsroot10* for 103 successfully ! **********"

else
        echo  "********** Error Input parameters ! **********"
fi


echo "******************** Completed ! ********************"





回忆曾经写过的第一个Bash脚本,布布扣,bubuko.com

回忆曾经写过的第一个Bash脚本

标签:linux   bash   

原文地址:http://blog.csdn.net/gujinjinseu/article/details/38324513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!