Chinaunix首页 | 论坛 | 博客
  • 博客访问: 444558
  • 博文数量: 293
  • 博客积分: 4204
  • 博客等级: 上校
  • 技术积分: 3060
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-21 10:27
个人简介

nobody

文章分类

全部博文(293)

文章存档

2014年(27)

2013年(105)

2012年(41)

2011年(109)

2010年(11)

分类:

2011-02-25 10:27:45

All the functions here are written by others.
1
 
function y_or_n_p ()
{
 local ans

    [ ! -t 0 ] && return 1

    while read -p "$*" -e ans ; do
case "${ans}" in
        y* | Y* ) return 0 ;;
        n* | N* ) return 1 ;;
        \! )      return 2 ;;
        q* | Q* ) return 3 ;;
        *) echo "Please answer one of \`y', \`n', \`q', or \`"\!"'" 1>&2 ;;
esac
   done
}

#:docstring yes_or_no_p:
# Usage: yes_or_no_p QUERY
#
# Like y_or_n_p, but require a full `yes', `no', `yes!', or `quit' response. 
#:end docstring:


function yes_or_no_p ()
{
    local ans

    [ ! -t 0 ] && return 3

    while read -p "$*" -e ans; do
        ans="$(echo ${ans} | tr '[A-Z]' '[a-z]')"

        case "${ans}" in
        yes )   return 0 ;;
        no )    return 1 ;;
        yes\! ) return 2 ;;
        quit )  return 3 ;;
        *) echo "Please answer \`yes', \`no', \`yes"\!"', or \`quit'" 1>&2 ;;
       esac
   done
}
# isnum returns True if its argument is a valid number,
# and False (retval=1) if it is any other string.
# The first pattern requires a digit before the decimal
# point, and the second after the decimal point.

# BASH NOTE: make sure you have executed `shopt -s extglob' before
# trying to use this function, or it will not work

isnum() # string
{
    case $1 in
    ?([-+])+([0-9])?(.)*([0-9])?([Ee]?([-+])+([0-9])) )
        return 0;;
    ?([-+])*([0-9])?(.)+([0-9])?([Ee]?([-+])+([0-9])) )
        return 0;;
    *) return 1;;
    esac
}

isnum2() # string
{
    case $1 in
    ?([-+])+([[:digit:]])?(.)*([[:digit:]])?([Ee]?([-+])+([[:digit:]])) )
        return 0;;
    ?([-+])*([[:digit:]])?(.)+([[:digit:]])?([Ee]?([-+])+([[:digit:]])) )
        return 0;;
    *) return 1;;
    esac
}

isint() # string
{
    case $1 in
    ?([-+])+([0-9]) )
        return 0;;
    *) return 1;;
    esac
}

isint2() # string
{
    case $1 in
    ?([-+])+([[:digit:]]) )
        return 0;;
    *) return 1;;
    esac
}

4

if [ $# != 2 ]; then 
    cat <
Syntax: $0 file.bin M
pads the file with trailing zeroes until its size is multiple of M
EOF
    exit -1 
fi
#
SIZE=`wc -c "$1" |awk '{print $1}'`  #输出wc -c 命令的第一列,也就是文件的size.
#另外,能获得文件size的命令还有ls stat等,不过wc 比较简洁
REM=$[SIZE%$2]
if [ $REM != 0 ]; then 
    PAD=$[$2-REM]
    echo padding $1 with $PAD zeroes
    head -c $PAD /dev/zero >>$1 #将指定长度个0 (byte为单位)追加到文件尾部
fi
5  tobin
X=$1
# if odd pad left with one zero
if [ $[${#X}%2] != 0 ]; then X=0$X; fi
#
S=${#X}
while [ $S != 0 ]; do
    Y=${X:$[S-2]:2}
    printf "\x$Y"
    S=$[S-2]
done


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