Bash Script Editor for Linux Bash Consolidated instance

Source: Internet
Author: User

Description : This is a relatively simple bash script editor, but has been tested. The reason for writing this blog is to practice the loop control language, functions, positional parameters, local variables, and function parameter calls, as well as calls between functions, as well as parameters for obtaining user options and options.

Note: The script options and the script options parameter get method use getopt and make further judgments, consult the main () function.

#!/bin/bash# author:  Li Anjie # time: 2014-12-05show_usage () { echo  ' Usage  mkscript.sh [-d|--description  "Script description"] [-a|--author  "Script author" ] /path/to/somefile '}write_file_header () {#写文件头部信息local  file_path= "$" local desc=$2local  auth=$3cat >>  $file _path <<eod#!/bin/bash# description:  $desc #  author:  $auth #eod}create_file () {#  Create a new file Local file_path= "$" judge_dir  $file _path[ $?  -eq 0 ] && write_file_header  "$file _path"   "$desc"   "$auth"  vim +  $file _pathchk_syntax  "$file _path"  }judge_dir () {#判断目录是否存在切有写权限local  file_ Path= "$" local dirname=$ (dirname  $file _path) if [ -d  "$dirname"  ];then[ -w   "$dirname"  ] && return 0else#echo  "No such directory or"   $dirname  permission&nbsP;denied. "  exit 1 fi}chk_syntax () {#  Determine if the file has a script syntax error local file_path= "$" clearif /bin/bash  -n  "$file _path"  ;thenchmod +x  "$file _path"     exit 0else  while true;doread -p  "File has a syntax error, whether to  continue editing.  (y/n): " optscase  $opts  iny| Y) vim +  "$file _path" chk_syntax  "$file _path";; n| N) exit 0;;  esacdone   fi}modify_file () {#  file modification related operation local file_path= "$" local desc=$ 2local auth=$3judge_dir  "$file _path" if [ $? -eq 0 ] ;thenheader=$ (head  -1  "$file _path") if [  "$header"  !=   "#!/bin/bash"  ]; thenecho   "This is not a script file,grogram will be exit."  && exit 1elif [  "$header"  ==  ' #!/bin/bash '  ];thenif [ ! -z  "auth"  ] && [ ! -z  "$desc"  ];thenif  grep  "# author:"   "$file _path"  && grep  "# description:"   " $file _path ";thensed -i " 1,5 s/\ (# author:\) .*/\1  $auth/"  $file _path" sed -i   "1,5 s/\ (# description:\) .*/\1  $desc/"   "$file _path" else sed -i  "1a  \# Author:  $auth " " $file _path "sed -i " 2a \# description:  $desc "   "$file _path" fivim +  "$file _path" chk_syntax  "$file _path" elif [ ! -z  "$ Auth " ];thenif grep  # author: '  " $file _path " ;thensed -i " 1,5 s/\ (# author:\) .*/\1  $auth/" " $file _path "else sed -i " 1a \# author:  $auth " " $file _path "fivim + " $file _path "chk_syntax " $file _path "elif [ ! -z   "$desc"  ];thenif grep  ' # description: '   $file _path  ;thensed -i  ' 1,5 s/\ (#  description:\) .*/\1  $desc/" " $file _path "elsesed -i " 2a \# description:   $DESC/" " $file _path "fivim + " $file _path "chk_syntax " $file _path "elsevim + " $file _path "chk_syntax " $file _path "Fififi}main () {temp= ' Getopt -o a:d: --long author:, description: --  "[email protected]" ' [ $? -ne 0 ] && echo  -e  "\033[31merror: unknown argument! \033[0m\n"  && show_usage  && exit 1 eval set --  "$TEMP" #echo   "$TEMP" while true;do[  -z  "$"  ] && show_usage && exit 1 case  "$"  in -d|--description) #该选项的参数就是 $2desc=$2[ ${desc:0:1} ==  "-"  ] &&  [ -z ${desc:2:1} ] && show_usage && exit 1 shift 2;; -a|--author) #该选项的参数就是 $2auth=$2[ ${auth:0:1} ==  "-"  ] && [ -z $ {auth:2:1} ] && show_usage && exit 1shift 2;; --) Shift[ $# -ne 1 ] && show_usage && exit 1file_ Path= "$" break;; *) #使用其他参数给提示错误, and exit .show_usage && exit 1esacdone#echo  "desc: $desc" #echo   "AUTH: $auth   #echo   file_path:  $file _path "if [ ! -f " $file _path " ]; thencreate_file  "$file _path"  && chk_syntax  "$file _path" elif [ -f  "$ File_path  ];thenmodify_file  $file _path   $desc   $auth fi }main  [email  Protected] "


Hope this blog can help you, if there is a better way or improve the place please, leave a message. Thank you for your patient review.


This article is from the "Sanctuary of Calm" blog, please make sure to keep this source http://mastters.blog.51cto.com/6516495/1586934

Bash Script Editor for Linux Bash Consolidated instance

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.