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
}
2
# 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
阅读(1094) | 评论(0) | 转发(0) |