Bash to build your own script installer

Source: Internet
Author: User
Tags shebang

Bash to build your own script installer

Objective

It is a matter of course, I always want to find a set of management script "framework", can make their own messy script a little regular, but still shallow, unable to find. Therefore, the initiation of their own to write a little optimization script tool, Novice can learn, master please correct me. Write a script installer today to make it easy to use anywhere in the shell after you've finished writing and new scripts.

What did the installer do?

One, configuration file
config.ini主要用于配置两个目录。
    • Read Directory for Scripts
    • Generate a soft-link storage directory

Second, read the script
    递归遍历读取scriptPath目录下的脚本文件,排除掉install.sh和config.ini。
do_file(){    forin $1/*    do         if[[ -d "$file" ]]then            "$file"        else            basename=`basename $file`            if[[ ! $basename == "install.sh" && ! $basename == "config.ini" ]];then                link_file $file            fi        fi    done}
Third, create a soft link
    为每一个脚本在binPath目录下创建软链接,如果之前存在则会首先删除掉,并对软链接加上执行权限(这里直接加了777)
Link_file() {filepath= $Filename= ' basename $' Linkname=${filename%.*}Linkpath=$binPath"/"$linkName    if[[-L$linkPath]]; Then        Echo "===> (Warn):"$linkPath"is exist,remove it!"Rm$linkPath    filn- S $filePath $linkPath    Echo "===> (Info): Link file"$filePath"----->"$linkName"successful!"chmod777 $linkPath}
IV. Configuring environment variables
    把binPath目录添加到环境变量中(~/.bash_profile),这样就可以随时的访问脚本了~
Add_profile() {isin= ' cat ~/.bash_profile | grep $`Echo_test"IsIn is"$isIn    if[[X"$isIn"= = x]]; Then        Echo "\n#setting PATH for LOCAL SCRIPT">> ~/.bash_profileEcho "Export path=\"$: \${path}\ "">> ~/.bash_profileEcho "===> (info)"$binPath"is added to Bash_profile successful!"        ExportPath= $:${path}     Else        Echo "===> (info)"$binPath"is already in the bash_profile!<skip>"    fi}
Try

Each new script can be placed in the ScriptPath directory, after the execution of install.sh will be in the binpath to generate the corresponding soft link, and then can be used in the terminal free of ~

1. You can see that there are five files under my directory (including the configuration file for the installation script)

2. After executing sh install.sh run

3. Generated three soft links in the BinPath directory ~

4. The corresponding path is generated in the ~/.bash_profile

5. You can see that we can be anywhere in the shell is already written with our own script instructions ~ (for example, Pyversion, is written by a native version of a modified local python)

6. Complete code:

#!/bin/bash# Read Config.iniSource./config.iniistest=$isTestBinpath=$binPathScriptpath=$scriptPathEditor(){Echo "'' @auther: Yeung Kwong @blog: http://blog.csdn.net/yang8456211 @email: [email protected] '"'}Help_fun() {cat << ENTER ============= Script Installation tool ============= Version:0.1Date:20160330Usage: Used as the initial installation of its own scripting environment e.g.: SH install.sh run ============= script installation tool =============enter}echo_emp(){Echo - e "\033[31m" $"\033[0m"}echo_test(){    [[$isTest==true]] &&Echo  $}Exit_pro(){Echo "= = User Quits = = Abort (1)"    Exit 1}Link_file() {filepath= $Filename= ' basename $' Linkname=${filename%.*}Linkpath=$binPath"/"$linkName    if[[-L$linkPath]]; Then        Echo "===> (Warn):"$linkPath"is exist,remove it!"Rm$linkPath    filn- S $filePath $linkPath    Echo "===> (Info): Link file"$filePath"----->"$linkName"successful!"chmod777 $linkPath}Do_file(){ forFileinch  $/* Do         if[[- D "$file"]]; Then             Do_file"$file"        ElseBasename= ' basename$file`if[[ !$basename=="install.sh"&&!$basename=="Config.ini"]]; ThenLink_file$file            fi        fi     Done}Add_profile() {isin= ' cat ~/.bash_profile | grep $`Echo_test"IsIn is"$isIn    if[[X"$isIn"= = x]]; Then        Echo "\n#setting PATH for LOCAL SCRIPT">> ~/.bash_profileEcho "Export path=\"$: \${path}\ "">> ~/.bash_profileEcho "===> (info)"$binPath"is added to Bash_profile successful!"        ExportPath= $:${path} #只是加到了内存中, new open terminal failure    Else        Echo "===> (info)"$binPath"is already in the bash_profile!<skip>"    fi}if[[$#!=1|| $!="Run"]]; Then     Help_fun EditorExit 2fiEcho "Yes"$scriptPath"script in the directory to install?"Echo "The installation directory is:"$binPath"(y/n)"Readif[[$REPLY=="Y"||$REPLY=="Y"]]; Then      Do_file$scriptPathAdd_profile$binPath    Echo "script environment installed successfully!!"Else    Echo "User terminates exit (Abort)"    Exit 0fi

Yeung Kwong (Atany) Original, reproduced please indicate bloggers and blog links, without the permission of the blogger, prohibit any commercial use.
Blog Address: http://blog.csdn.net/yang8456211
Post Address: http://blog.csdn.net/yang8456211/article/details/51020797
This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Bash to build your own script installer

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.