Recall the first bash script

Source: Internet
Author: User

This is a Miscellaneous article. I want to talk about my understanding of bash, the soul of the Linux operating system.

It is said that there are more than 50 shell scripts (Chen Hao's cool shell online has a blog post http://coolshell.cn/articles/8619.html pick up, now, with the linux program, in addition to using makefile, occasionally use cmakelist.txt, more programs are using bash scripts to achieve semi-automatic debugging, just like in vs below, every time you modify and re-compile the same, you always GCC or G ++, and then. /test run, and then check whether there is any error. The compilation error has to be modified again. rm-RF test is very tedious. If a small script is test. SH:

#!/bin/bashgcc -o test -g test.cif [ -e test ];then   ./test   rm testfi


In this way, you only need to execute the script sh test. Sh every time to debug the program. If an error occurs, you can directly modify vim or gedit.


For more straightforward applications, see an article I wrote with shell scripts and makefile examples of installing and debugging programs in Linux. It is true that, debugging with GDB is a more in-depth topic.


After all, let's get into the topic. That is, the first script I used at work that year is to decompress and update related files in the system on the motherboard, or to update the system version, however, the shell script used for the first time has many shortcomings:

1. There is a lack of necessary script instructions, such as entering the command sh test. Sh. If a parameter is required, you need to add the necessary judgment:

If [$ #-ne 1]; then

Echo "Usage :......"

Fi


2. The variable name style is not uniform. According to my current thinking, it is best to write all the variable names in the directory in upper case, while other variable names in lower case, but better words.

 

3. there is no habit of outputting command execution errors to logs. In fact, after executing some important commands, add> error. log 2> & 1 can help with analysis, or write a script for error. log for automated analysis.


In a word, this article is just a mix of feelings about how poor the first script for work is. However, people are always making progress, so life is also wonderful because of diligence and learning ~


Mutual Encouragement !!!


#! /bin/bash# By GuJinjin# 2010/08/11# Define Path Variablescm_DIR=/export/bta/canmorefs_DIR=/export/bta/canmore/fsroothost_DIR=/export/hostsdks_DIR=/mnt/bta/sdks# define variableb1=cmb2=sdvb3=gvl# cleanup fsrootrm -rf $fs_DIR/*# Update fsroot # $1 is the name of archive# $2 is the build numbertar -zxvf $host_DIR/$1 -C $fs_DIR/# Update oflashcp -rf $sdks_DIR/oflash $fs_DIR/echo "********** cp oflash successfully ! **********"# Update redboot_flash_nor.bin or redboot.binif [ -e $sdks_DIR/$2/redboot_flash_nor.bin ];thencp -rf $sdks_DIR/$2/redboot_flash_nor.bin $fs_DIRecho "********** cp redboot_flash_nor.bin successfully ! ********** "elif [ -e $sdks_DIR/$2/redboot.bin ];thencp $sdks_DIR/$2/redboot.bin $fs_DIR/redboot_flash_nor.binecho "********** cp redboot.bin successfully ! ********** "else echo "********** ERROR ! ********** No such a related file! ********** "fi# Update cefdk,config for cm, sdv & gvlif [ "$3" = "$b1" ];thencp -rf $sdks_DIR/conf.canmore $fs_DIR/cp -rf $sdks_DIR/$2/gen3.bin $fs_DIRecho "********** cp conf.canmore successfully ! ********** "elif [ "$3" = "$b2" ];thencp -rf $sdks_DIR/conf.sodaville $fs_DIRcp -rf $sdks_DIR/$2/gen4.bin $fs_DIRecho "********** cp conf.sodaville successfully ! ********** "elif [ "$3" = "$b3" ];thencp -rf $sdks_DIR/conf.groveland $fs_DIR/cp -rf $sdks_DIR/$2/gen4.5.bin $fs_DIRecho "********** cp conf.groveland successfully ! ********** "else echo  "********** Error Input parameters ! **********"fi# Update 101,102,103 for sdv or cmif [ "$4" = "3" ];thencp -rf $sdks_DIR/$2/bzImage /tftpboot/101cp -rf $sdks_DIR/$2/bzImage /tftpboot/102cp -rf $sdks_DIR/$2/bzImage /tftpboot/103rm -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/fsroot103echo "********** 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/102rm -rf $cm_DIR/fsroot101rm -rf $cm_DIR/fsroot102      cp -rf $cm_DIR/fsroot $cm_DIR/fsroot101      cp -rf $cm_DIR/fsroot $cm_DIR/fsroot102echo "********** cp bzImage fsroot10* for 101,102 successfully ! **********"elif [ "$4" = "1" ];then      cp -rf $sdks_DIR/$2/bzImage /tftpboot/103rm -rf $cm_DIR/fsroot103    cp -rf $cm_DIR/fsroot $cm_DIR/fsroot103echo "*********** cp bzImage fsroot10* for 103 successfully ! **********"else        echo  "********** Error Input parameters ! **********"fiecho "******************** Completed ! ********************"



Zookeeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.