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

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

Bash玩转脚本1之自己的脚本安装程序的更多相关文章

  1. 转 Oracle DBCA高级玩法:从模板选择、脚本调用到多租户

    但凡是学过Oracle的同学,对DBCA(Database Configuration Assistant, DBCA)都不会陌生,有了这个工具,使得创建数据库成为可能.而DBCA本身有图形和静默两种 ...

  2. Linux/Unix shell 脚本中调用SQL,RMAN脚本

    Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...

  3. shell脚本--制作自己的服务脚本

    首先注意一下,我用的环境是centos6.5,中间有一些操作和在Ubuntu上有一些地方的操作是不同的, 编写脚本 首先看一个实例:假设有一个test的服务,可以通过命令对test进行启动.关闭或者重 ...

  4. 【Shell脚本编程系列】Shell脚本开发的习惯和规范

    1.开头指定脚本解释器 #!/bin/sh或#!/bin/bash 2.开头加版本版权信息 #Date #Author #Mail #Function #Version 提示:可配置vim编辑文件时自 ...

  5. 180807-Quick-Task 动态脚本支持框架之Groovy脚本加载执行

    Quick-Task 动态脚本支持框架之Groovy脚本加载执行 上一篇简答说了如何判断有任务动态添加.删除或更新,归于一点就是监听文件的变化,判断目录下的Groovy文件是否有新增删除和改变,从而判 ...

  6. linux,shell脚本中获取脚本的名字,使用脚本的名字。

    需求描述: 写shell脚本的过程中,有时会需要获取脚本的名字,比如,有的时候,脚本 中会有usage()这种函数,可能就会用到脚本的名字. 实现方法: shell脚本中,通过使用$0就可以获取到脚本 ...

  7. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  8. Linux脚本中调用SQL,RMAN脚本

    Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...

  9. Linux(centos 6.5) 调用java脚本以及定时运行的脚本实例及配置文件具体解释

    Linux(centos 6.5) 调用java脚本以及定时运行的脚本实例 一.调用java程序脚本(默认已经搭建好了Java环境) 1.jdk 安装路径 /usr/jdk/jdk1.7/-- 2.j ...

  10. 【在 Nervos CKB 上做开发】Nervos CKB脚本编程简介[2]:脚本基础

    CKB脚本编程简介[2]:脚本基础 原文作者:Xuejie 原文链接:Introduction to CKB Script Programming 2: Script 本文译者:Shooter,Jas ...

随机推荐

  1. bug 7715339 登录失败触发 ‘row cache lock’ 等待

    Bug 7715339 - Logon failures causes "row cache lock" waits - Allow disable of logon delay ...

  2. C++ 为什么要virtual析构函数

    class A { public: A() { printf("A()\n"); } virtual ~A() { printf("~A()\n"); } }; ...

  3. 44.delete用法

    声明+delete:函数禁止使用.可以使一个类禁止释放

  4. 后台vb校验是否GUID

    '校验是否GUID Private Function IsGUID(ByVal strGUID As String) As Boolean Dim regexTemp As System.Text.R ...

  5. 关于Webpack详述系列文章 (第二篇)

    1.缩小文件搜索范围 1.1.1 include & exclude module:{ rules:[ { test:/\.js$/, use:['babel-loader?cacheDire ...

  6. 使用Python开发轻量级的Web框架以及基于WSGI的服务器来实现一个网站页面

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目说明 二丶数据准备 三丶使用网络TCP开发一个基于WSGI协议的Web服务器 四丶使用python3开发一个轻量级的 ...

  7. RFID的工作流程

    工作流程 1.阅读器通过发射天线发送一定频率的射频信号, 2.当射频卡进入发射天线工作区域时产生感应电流,射频卡获得能量被激活: 3.射频卡将自身编码等信息通过卡内置发送天线发送出去 4.系统接收天线 ...

  8. Microsoft SQL Server Version List(SQL Server 版本)

    原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...

  9. iOS Threading编程指南 官方文档翻译第一篇(序言)

    序言   Thread是能够使多个code paths 在同一个APP内并发运行的几种技术之一.虽然新的技术为并发运行提供了先进.高效的工具(例如operation 对象和GCD),但是OS X和iO ...

  10. Java exception handling best practices--转载

    原文地址:http://howtodoinjava.com/2013/04/04/java-exception-handling-best-practices/ This post is anothe ...