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     Denied. " Exit 1 fi} chk_syntax () {# To determine if a file has a script syntax error local file_path= "$" clearif/bin/bash-n "$file _path"; then chmod +x "$file _path "Exit 0else while True;do read-p" File have a syntax error, whether to continue editing. (y/n): "OPTs case $opts in y| YVim + "$file _path" Chk_syntax "$file _path";; n|         N) exit 0;;  Esac done fi} modify_file () {# File modification related action local file_path= "$" local desc=$2local auth=$3judge_dir "$file _path" if [$?-eq 0] Then header=$ (head-1 "$file _path") if ["$header"! = "#!/bin/bash"]; Then echo "The is not a script file,grogram would be exit." && exit 1 elif ["$header" = = ' #!/bin/ba SH '];then if [!-Z ' auth '] && [!-Z ' $desc '];then if grep ' # Author: ' $file _path ' &&am P  grep "# Description:" "$file _path", then Sed-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" fi vim + "$file _path" Chk_synt Ax "$file _path" ELIF [!-Z "$auth"];then if grep ' # Author: ' "$file _path"; then Sed-i "1,5 s/\ (# author:\). */\1 $a uth/"" $file _path "Else sed-i" 1a \# Author: $auth "" $file _path "fi vim +" $ File_path "Chk_syntax" $file _path "elif [!-Z" $desc "];then if grep ' # Description: '" $file _path ", then Sed-i" 1,5 s/\ (# description:\). */\1 $desc/"" $file _path "Else sed-i" 2a \        # Description: $desc/"" $file _path "fi vim +" $file _path "Chk_syntax" $file _path " else vim + "$file _path" Chk_syntax "$file _path" Fi Fifi} main () {temp= ' getopt-o a:d:--lon G 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) #该选项的参数就是 $ desc=$2 [${desc:0:1} = = "-"] &A        mp;& [-Z ${desc:2:1}] && show_usage && exit 1 shift 2;; -a|--author) #该选项的参数就是 $ auth=$2 [${auth:0:1} = = "-"] && [-Z ${auth:2:1}] &am        p;& show_usage && exit 1 shift 2;;            --) Shift [$#-ne 1] && show_usage && exit 1 file_path= "$"        break;;            *) #使用其他参数给提示错误, and exit. Show_usage && exit 1 Esacdone#echo "desc: $desc" #echo "Auth: $auth" #echo "File_path: $file _path" if [! -F "$file _path"];then create_file "$file _path" && chk_syntax "$file _path" elif [-F "$file _path"];then Modi Fy_file "$file _path" "$desc" "$auth" fi} main "[Email protected]"


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.