Bash玩转脚本1之自己的脚本安装程序
来源:互联网 发布:央视直播软件下载apk 编辑:程序博客网 时间:2024/06/06 13:22
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.inisource ./config.iniisTest=$isTestbinPath=$binPathscriptPath=$scriptPatheditor(){ 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 2fiecho "是否对"$scriptPath"目录下的脚本进行安装?"echo "安装目录为:"$binPath"(y/n)"readif [[ $REPLY == "y" || $REPLY == "Y" ]];then do_file $scriptPath add_profile $binPath echo "脚本环境安装成功!!"else echo "用户终止exit (Abort)" exit 0fi
杨光(atany)原创,转载请注明博主与博文链接,未经博主允许,禁止任何商业用途。
博客地址:http://blog.csdn.net/yang8456211
博文地址:http://blog.csdn.net/yang8456211/article/details/51020797
本文遵循“署名-非商业用途-保持一致”创作公用协议
1 0
- Bash玩转脚本1之自己的脚本安装程序
- Bash玩转脚本2之自己的脚本管理工具
- Bash玩转脚本6之Git脚本
- Bash玩转脚本5之Windows上修改环境变量的脚本系统
- Bash玩转脚本5之Windows上修改环境变量的脚本系统
- 玩转Bash脚本:选择结构之if
- 玩转Bash脚本:选择结构之case
- 玩转Bash脚本:循环结构之for循环
- 玩转Bash脚本:循环结构之while循环
- Bash玩转脚本3之几个指令有趣的筛选京东评价
- Bash玩转脚本4之搞一套完整的Android反编译与分包工具
- Bash玩转脚本7之说好的用crontab搞个自动任务
- 玩转Bash脚本:test测试语句
- 玩转Bash脚本:特殊变量
- 玩转Bash脚本:特殊变量
- 玩转Bash脚本:数值计算
- 玩转Bash脚本:变量基本概念及其初始化
- linux之bash脚本
- VC++ 绘制简单电子元件
- 基于LTPA的WAS SSO基础配置
- QT14 how to save data in sqlite database with pushbutton
- 110. Balanced Binary Tree
- odm teamserver与定制portal sso过程中遇到的Request Headers Referer属性安全性校验问题
- Bash玩转脚本1之自己的脚本安装程序
- 红帽虚拟化RHEV-PXE批量安装RHEV-H
- 第60课:使用Java和Scala在IDE中实战RDD和DataFrame动态转换操作学习笔记
- Google Test(GTest)使用方法和源码解析——Listener技术分析和应用
- printf函数bug
- BeanFactoryPostProcessor和BeanPostProcessor区别和应用场景
- 数据结构之链表地址
- linux中,某文件的权限是:drw-r--r--,用数值形式表示该权限,则该八进制数为多少?
- BAT执行PHP