Linux Bash 简单字符串处理

 Published On February 12, 2015

需求

  • 维护Jekyll博客的时候,本地预览每次都要打命令jekyll s
  • 每次push 到 github上时,我们都要写一串命令git add --allgit commit -m <message>git push。特别繁琐。
  • 我们可以在平时的小修改中用日期-时间来作为commit的message
  • 由于Github不兼容代码高亮插件rouge,push到远程的时候我们只能用pygments作为代码高亮插件,这没啥。但苦逼的是,由于windows与pygments不兼容,我们在本地预览只能用rouge。所以我们要经常切换

高亮问题在Github一直没解决 ╮(╯▽╰)╭,我也是在 Github 上看到的临时办法

任务

  • 我们设计一个Bash小程序,来完成上述的命令自动输入字符串切换

我们先来预习基本知识


字符串的删改

Tutorial

sed -i 's/new string/old string/g' *.txt

从键盘读取变量

read variable

去掉前缀空格

Reference Source

input="$input" | sed 's/^ *//'input="$input" | sed 's/^[ \t]*//'

在这里,

  • s/ : 替换命令,替换每行中被^[ \t]匹配到的部分
  • ^[ \t]* : 搜索匹配 ( ^ 指每行的开头开始; [ \t]* 匹配包括缩进在内的一串空格)
  • // : 删掉所有匹配项

判断字符串是否为空

三种写法

if [ -z "$str" ]; then
   echo "Str is null"
fiif [ "$str" == "" ];then
   echo "Str is null"
fiif [ ! "$str" ];then
   echo "Str is null"
fi

将当前日期作为commit message

如果读入了一个空串(即输入全是空格或者只有回车,read函数返回一个空串, 有点像c语言里的std::cin)那么我们就把当前时间作为commit message。

input=`date +%Y-%m-%d-%H-%M-%S`

注:shell在赋值的时候变量前不加$,而在引用的时候变量前加$作为标识符。

`date +%Y-%m-%d-%H-%M-%S`

是执行 shell 下的 date 命令,%Y,%m,%d,都是命令 date 的参数,分别对应年月日时分秒。左右加 ` 表示中间执行的是shell命令(否则将 date +%Y-%m-%d-%H-%M-%S 作为字符串处理。将返回值赋给input。

附上最终代码

 1 sed -i 's/# highlighter: pygments/highlighter: pygments/g' _config.yml
 2 sed -i 's/highlighter: rouge/# highlighter: rouge/g' _config.yml
 3 echo 'Input commit message:'
 4 read input
 5 
 6 if [ "$input" == "" ]; then
 7         input=`date +%Y-%m-%d-%H-%M-%S`
 8 fi
 9 
10 git add --all
11 git commit -m "$input"
12 git push

Tags: Bash

Comments: