原文:http://bash.cyberciti.biz/academic/calculate-number-of-digits-in-input/
以下是该shell脚本的源代码:
#!/bin/bash
# Shell program to calculate the number of digits in a
# number read from the user
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
echo -n "Enter number : "
read n
sd=0
# store number of digit
nd=0
on=$n # store $n so that we can use it later
# use while loop to caclulate the number of digit
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 ))
nd=$(( $nd + 1)) # calculate all digit in a number till n is not zero
done
echo "Numnber of digit in a $on is $nd" |