clear
t=1
while
[ $t -eq 1 ]
do
clear
echo -e -n "Nhap so:\t"
read so
if [ ${#so} -eq 3 ]; then
a=$(($so % 10))
b=$((($so / 10) % 10))
c=$((($so / 100) % 10))
echo -en "Tong cac ky so
la: `expr $a + $b + $c`"
else
echo -en "\nNhap so
trong pham vi tu 100 -> 999."
fi
while [ $t -ne 0 ]
do
echo -en "\nBan co muon
tiep tuc? (Nhap 0 hoac 1)\t"
read t
if [ $t -eq 1 ];then
break
fi
done
doneTags: Lập trình Shell, lập trình Linux, Ubuntu, Script tính tổng
1 comments
#! /bin/bash
Reply#Tinh tong cac ky so cua mot so duoc nhap vao (1234->10)
n=$1
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Tong la $sum"