Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1679541
  • 博文数量: 410
  • 博客积分: 9563
  • 博客等级: 中将
  • 技术积分: 4517
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-03 19:59
个人简介

文章分类

全部博文(410)

文章存档

2017年(6)

2016年(1)

2015年(3)

2014年(4)

2013年(32)

2012年(45)

2011年(179)

2010年(140)

分类: LINUX

2011-03-16 22:03:23

现在github之类的源代码公共服务非常方便,但是有时候希望自己的密码、用户名能够不公开,可以利用shell方便第实现替换。源代码当中的私密数据用外部文件存储,不提交源代码管理,发布时用shell脚本构建。
下面的这段代码就能实现这个功能:
例如我想保护这段代码:Bash语言:
#!/bin/bash 
# FILE: gmail.sh
# USAGE: ./gmail.sh
# DESCRIPTION: 检查gmail,输出到xmobar
# a script that show how many new mails you got
# to use it in xmobar add this
# Run Com "sh" ["/path/to/mail.sh"] "mail" 300
#===============================================================================
gmail_login="##MAIL_USER##"
gmail_password="##MAIL_PASSWORD##"

dane="$(wget --secure-protocol=auto --timeout=10 -t 3 -q -O - \
       --user=${gmail_login} --password=${gmail_password} \
   --no-check-certificate   \
   | grep 'fullcount' \
   | sed -e 's/.*//;s/<\/fullcount>.*//' 2>/dev/null)"

if [ -z "${dane}" ]; then
    echo "";
else
    if [ "${dane}" -gt  "0" ]; then
        echo " ${dane}封新邮件 ";
    else
        echo "";
    fi
fi
我需要在file.list中增加文件名
  1. gmail.sh resgmail.sh
在 secret.txt中增加替换字段
  1. -- 用于保存一些机密字段
  2. -- 表示该行为注释

  3. ##MAIL_PASSWORD## password
  4. ##MAIL_USER## user
转换的脚本源代码是 maketrans
该脚本能够处理多个文件,唯一的问题是无法正确处理文件路径或者文件名有空格的情况
Bash语言:
#!/bin/bash
#===============================================================================
#
#          FILE:  maketrans
#
#         USAGE:  ./maketrans
#
#   DESCRIPTION:  用于替换机密字段来生成本地文件
#                 同目录中secret.txt.template文件为密码替换模板
#                 把 secret.txt.template 拷贝成 secret.txt,并根据
#                 实际情况填入相应的值
#                 同目录中file.list文件为替换文件目录
#
#        AUTHOR:  BaiLiang , bailiangcn@gmail.com
#       COMPANY:  DQYTV
#       VERSION:  1.0
#       CREATED:  2011-03-16 14:24:21
#===============================================================================

read_replace_var() {
    echo "开始转换"
    while read x y; do
        str="s/$x/$y/g"
        echo "$str" >>tmpsedcommand.sed
    done
}

read_filename() {
    while read soufile desfile; do
        echo "正在转换文件: $soufile -> $desfile"
        sed -f tmpsedcommand.sed "$soufile" > "$desfile"
    done

}

#清空临时命令文件
cat /dev/null >  tmpsedcommand.sed

#清除注释、空行、替换/符号为\/
# 生成sed 命令文件
sed -e '/^\s*--/d' -e '/^\s*$/d' secret.txt -e 's/\//\\\\\//g'| read_replace_var

# 逐个读入文件
sed -e '/^\s*--/d' -e '/^\s*$/d' file.list | read_filename


rm tmpsedcommand.sed


阅读(852) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~