专注各种脚本编程
Baidu
加入收藏夹
本站内容有下面分类知识,欢迎您的到来^_^
shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言 oracle
当前位置:| 主页>shell脚本欣赏>

用shell脚本计算用户输入数字的位数

百度收藏 QQ搜藏

原文: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"
上一篇:shell脚本之颠倒输入数字(趣味数学) 下一篇:用shell脚本计算用户输入的5位的数字的各位数的和

power by soyo123 2007-2008