您正在查看: mac 分类下的文章

bash snippets

Little Useful Functions

The Real Dir of Script itsef

sometimes we need to change dir to the real path of a script and do something. But this little thing is very hard to make it works in lots of Linux Distribution and Mac OS X. The snippets below is work on CentOS, Ubuntu and Mac OS X 10.10.

#!/bin/sh
# read link 
_script="$(readlink -n ${0})"
if [[ $_script = '' ]]; then
    # when call script without symbolic, readlink will return empty string
    pushd `dirname $0` > /dev/null
    _script=`pwd`
    popd > /dev/null
else
    _script="$(dirname $_script)"
fi
echo 'the real path is ' $_script

command usage

echo color

16color syntax:

# \033 is the Escape symbol in ASCII, it can also written in \x1b
echo -e "\033[${attr};${clbg};${clfg}m${renderText}\033[0m"
# -e explain as use string escape
# if you let \033[ in a variable, -e is not necessary. such as
start="\x1b["
end="\x1b[0m"
echo "${start}${attr};${clbg};${clfg}m${renderText}${end}"

which attr is control number, clbg is background color, clfg is foreground color.
The detail is in tip_colors_and_formatting

mac os使用homebrew来管理后台服务

在linux下我们经常通过 service 或者 /etc/init.d/来管理我们的后台服务软件,并使用包管理器安装这些软件。 在mac下有homebrew这个好用的工具来安装软件,但是一直没有找到好的管理这些软件的方法,知道我发现了homebrew的一个services插件。

安装方法:

通过下面的命令行可以安装services插件

brew tap gapple/services
安装好之后就可以非常方便的使用,例如

$ brew services start mysql
==> Successfully started mysql (label: homebrew.mxcl.mysql)
Alt text