Jun's Blog

Output, activities, memo and etc.

Rails type Framework developed by Bash

関連記事はこちら。
http://www.moongift.jp/2012/03/20120317-2/

GitHubでソースが公開されているので、
https://github.com/emasaka/shails

https://github.com/emasaka/shails/blob/master/bin/shails
から読んで見ると面白いです。
bashはローカル変数が使えるからいいよね〜。

つーか、bashでオブジェクト志向プログラミングやってるんだが・・・。
クレイジー。
https://github.com/emasaka/shails/blob/master/SHas/Object.sh


ところで、自分が8年前にbashでなくsh(B Shell)で作った遊びプログラムを見つけた。

シェルスクリプトはチープな環境でも動くことが大事と自分は思っているので、シェルスクリプト=B Shellがメインだ。

B Shellで作った入力チェックライブラリ。
文字列のチェックはsed正規表現でやってる。
遊びですね(笑)

# commands
CAT="/bin/cat"
CUT="/bin/cut"
SED="/bin/sed"
EXPR="/usr/bin/expr"
TEST="/usr/bin/test"
USERADD="/usr/bin/useradd"

# error statues
OK="0"
ERR_ARG="1"
ERR_NOT_FOUND="2"
ERR_FILE_FORMAT="3"
ERR_UNKNOWN="99"

# other defines
PW_PATH="/etc/passwd"
PW_SEP=":"
PW_FIELD_NO_USERNAME="1"
USERS_DAT_FILE="users.dat"
USERS_DAT_SEP=":"
USERS_FIELD_NO_USERNAME="1"
USERS_FIELD_NO_UID="2"
USERS_FIELD_NO_GF="3"
USERNAME_LEN_MIN="1"
USERNAME_LEN_MAX="10"
UID_LEN="5"
UID_MIN="10000"
UID_MAX="19999"
GF_LEN_MIN="1"
GF_LEN_MAX="20"
USER_GROUP="xxxxxx"
USER_SHELL="/bin/tcsh"
HOME_DIR="/home"

# synopsis: getlength string
# description: get the length of the first arugment value to stdout.
# return value: return ${OK} if get target string length size.
get_length() { 
    [ "${#}" -ne "1" ] && return "${ERR_ARG}"
    echo "`"${EXPR}" "${1}" : ".*"`"
    [ "${?}" -ge "2" ] && return "${ERR_UNKNOWN}"
    return "${OK}"
}

# synopsis: is_dight string
# description: check for a digit (0 through 9).
# return value: return ${OK} if check result is only OK.
is_dight() {
    [ "${#}" -ne "1" ] && return "${ERR_ARG}"
    [ "${1}" = "" ] && return "${ERR_ARG}"
    "${TEST}" "`echo "${1}" | "${SED}" -e 's/^[0-9]*$//'`" = ""
    return "${?}"
}

# synopsis: is_alpha string
# description: check for a digit (0 through 9).
# return value: return ${OK} if check result is only OK.
is_alpha() {
    [ "${#}" -ne "1" ] && return "${ERR_ARG}"
    [ "${1}" = "" ] && return "${ERR_ARG}"
    "${TEST}" "`echo "${1}" | "${SED}" -e 's/^[A-Za-z]*$//'`" = ""
    return "${?}"
}

# synopsis: is_alnum string
# description: check for an alphabetic character.
# return value: return ${OK} if check result is only OK.
is_alnum() {
    [ "${#}" -ne "1" ] && return "${ERR_ARG}"
    [ "${1}" = "" ] && return "${ERR_ARG}"
    "${TEST}" "`echo "${1}" | "${SED}" -e 's/^[0-9A-Za-z]*$//'`" = ""
    return "${?}"
}

# synopsis: is_gf_type string
# description: check for an valid gcos-field character.
# return value: return ${OK} if check result is only OK.
is_gf_type() {
    [ "${#}" -ne "1" ] && return "${ERR_ARG}"
    [ "${1}" = "" ] && return "${ERR_ARG}"
    "${TEST}" "`echo "${1}" | "${SED}" -e 's/^[0-9A-Za-z ]*$//'`" = ""
    return "${?}"
}

# synopsis: _test [string]
# description: test entry for debug.
# return value: return ${OK}.
_test() {
    _LEN="`get_length "${1}"`"
    _STS="${?}"
    echo -n "get_length:`"${TEST}" "${_STS}" = "${OK}" && echo -n "OK"`"
    echo " length:${_LEN}"
    echo "is_dight:`is_dight "${1}" && echo "OK"`"
    echo "is_alpha:`is_alpha "${1}" && echo "OK"`"
    echo "is_alnum:`is_alnum "${1}" && echo "OK"`"
    echo "is_gf_type:`is_gf_type "${1}" && echo "OK"`"
    return "${OK}"
}

# uncomment next line if debug, and run '$ sh common.sh target'.
#_test "${1}"