Bash之打造自己的脚本安装器

前言


还是理所当然的前言,我一直想找一套管理脚本的“框架”,能让自己杂乱的脚本有点规整。无奈眼界尚浅,未能找到。

因此萌生自己写一点优化脚本的工具来。新手可学习。高手请指正。今天先写一个脚本的安装器,目的在于写完并新脚本之后能够在shell的不论什么位置都能够便捷使用。

安装器干了啥?

一、配置文件

config.ini主要用于配置两个文件夹。

  • 脚本的读取文件夹
  • 生成软链接的存放文件夹

二、读取脚本

    递归遍历读取scriptPath文件夹下的脚本文件,排除掉install.sh和config.ini。
do_file()
{
for file in $1/*
do
if [[ -d "$file" ]]; then
do_file "$file"
else
basename=`basename $file`
if [[ ! $basename == "install.sh" && ! $basename == "config.ini" ]];then
link_file $file
fi
fi
done
}

三、创建软链接

    为每个脚本在binPath文件夹下创建软链接,假设之前存在则会首先删除掉。并对软链接加上运行权限(这里直接加了777)
link_file()
{
filePath=$1
fileName=`basename $1`
linkName=${fileName%.*}
linkPath=$binPath"/"$linkName
if [[ -L $linkPath ]];then
echo "===>(warn):"$linkPath" is exist,remove it!"
rm $linkPath
fi
ln -s $filePath $linkPath
echo "===>(info):link file "$filePath" -----> "$linkName" successful!"
chmod 777 $linkPath
}

四、配置环境变量

    把binPath文件夹加入到环境变量中(~/.bash_profile)。这样就能够随时的訪问脚本了~
add_profile()
{
isIn=`cat ~/.bash_profile | grep $1`
echo_test "isIn is "$isIn
if [[ x"$isIn" == x ]];then
echo "\n#Setting PATH FOR LOCAL SCRIPT" >> ~/.bash_profile
echo "export PATH=\"$1:\${PATH}\"" >> ~/.bash_profile
echo "===>(info)"$binPath" is added to bash_profile successful!"
export PATH=$1:${PATH}
else
echo "===>(info)"$binPath" is already in the bash_profile!<SKIP>"
fi
}

尝试

每次新加的脚本便能够放在scriptPath文件夹,运行install.sh之后便会在binPath里面生成相应的软链接。然后就能够在终端中自由的使用了~

1.能够看到,我的文件夹以下有五个文件(包含安装脚本的配置文件)

2.运行sh install.sh run 之后

3.在binPath文件夹下生成了三个软链接~

4.并在~/.bash_profile里生成了相应的Path

5.能够看到我们在Shell的不论什么位置已经能够是用自己编写的脚本指令了~(比如pyversion。是自己写的一个改动本地python版本号的小脚本)

6.完整代码:

#!/bin/bash

# 读取config.ini
source ./config.ini
isTest=$isTest
binPath=$binPath
scriptPath=$scriptPath editor(){
echo '''
@auther: 杨光
@blog: http://blog.csdn.net/yang8456211
@email: 347702498@qq.com
'''
} help_fun(){
cat << ENTER
============= 脚本安装工具 =============
Version: 0.1
Date: 20160330
Usage: 用作初始安装自己的脚本环境
e.g.: sh install.sh run
============= 脚本安装工具 =============
ENTER
} echo_emp(){
echo -e "\033[31m"$1"\033[0m"
} echo_test(){
[[ $isTest == true ]] && echo $1
} exit_pro(){
echo "==用户退出== Abort(1)"
exit 1
} link_file()
{
filePath=$1
fileName=`basename $1`
linkName=${fileName%.*}
linkPath=$binPath"/"$linkName
if [[ -L $linkPath ]];then
echo "===>(warn):"$linkPath" is exist,remove it!"
rm $linkPath
fi
ln -s $filePath $linkPath
echo "===>(info):link file "$filePath" -----> "$linkName" successful!"
chmod 777 $linkPath
} do_file()
{
for file in $1/*
do
if [[ -d "$file" ]]; then
do_file "$file"
else
basename=`basename $file`
if [[ ! $basename == "install.sh" && ! $basename == "config.ini" ]];then
link_file $file
fi
fi
done
} add_profile()
{
isIn=`cat ~/.bash_profile | grep $1`
echo_test "isIn is "$isIn
if [[ x"$isIn" == x ]];then
echo "\n#Setting PATH FOR LOCAL SCRIPT" >> ~/.bash_profile
echo "export PATH=\"$1:\${PATH}\"" >> ~/.bash_profile
echo "===>(info)"$binPath" is added to bash_profile successful!"
export PATH=$1:${PATH} #仅仅是加到了内存中,新开终端失效
else
echo "===>(info)"$binPath" is already in the bash_profile!<SKIP>"
fi
} if [[ $# != 1 || $1 != "run" ]];then
help_fun
editor
exit 2
fi echo "是否对"$scriptPath"文件夹下的脚本进行安装?"
echo "安装文件夹为:"$binPath"(y/n)"
read
if [[ $REPLY == "y" || $REPLY == "Y" ]];then
do_file $scriptPath
add_profile $binPath
echo "脚本环境成功安装!!"
else
echo "用户终止exit (Abort)"
exit 0
fi

杨光(atany)原创,转载请注明博主与博文链接,未经博主同意,禁止不论什么商业用途。

博客地址:http://blog.csdn.net/yang8456211

博文地址:http://blog.csdn.net/yang8456211/article/details/51020797

本文遵循“署名-非商业用途-保持一致”创作公用协议

最新文章

  1. Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法(mvc部分视图的添加)
  2. MySQL修改root账号密码
  3. Java字符串常量池
  4. 【Windows】为节省系统资源,停掉不必要的服务
  5. (LinkedList)2. Add Two Numbers
  6. Python:time模块&amp;序列化&amp;生成随机数&amp;反射
  7. Linux运维相关目录
  8. http://blog.csdn.net/wxwzy738/article/details/16968767
  9. Linux&amp;UNIX上卸载GoldenGate的方法
  10. lintcode:最大子数组II
  11. C++中delete和delete[]的使用
  12. JSP include HTML出现乱码
  13. VC的话有必要认真听,但却不用急着照办
  14. Apple Watch 1.0 开发介绍 1.1 简介 开发苹果手表
  15. CoreJavaE10V1P3.8 第3章 Java的基本编程结构-3.8 控制流程(Control Flow)
  16. idea 导入项目后 有的项目目录结构不展开解决办法
  17. FusionCharts MSBar2D图
  18. 【Linux基础】常用Linux命令: cd, cp, ls, mkdir, mv, rm, su, uname
  19. 重庆3Shape Dental System技术支持
  20. mac php7.0~7.2 memcache安装

热门文章

  1. 阿里云 CentOS7.4 环境安装nginx
  2. iOS Dev (51)加急审核
  3. java根据url获取完整域名
  4. ProgressBar-style属性分析
  5. activity-栈相关属性
  6. Android 开发之锁屏弹窗
  7. python之经典猜数字
  8. GO语言学习(十六)Go 语言结构体
  9. 深度解析VC中的消息
  10. 小贝_redis web管理界面工具安装