|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
I have been working on a looping script using while & if statments?
and it just seems to be driving me crazy....anyone give me a pointer so I head into the right direction?The trick of the script is to count down from a number that the user selects till zero. when its at one..the object is singular, and when its more then 1 or zero, its plural ie 1 cow 0 cowS, 2 cowS and stopping at zero any suggestions? |
|
#2
|
|||
|
|||
|
Errr... it was harder to think of pointers than actually coding it. So here's one that works. I've left comments out so you can work out what it's doing (and why
)Code:
#!/bin/ksh
if [[ $# -ne 1 ]]; then
echo ERROR: you need to supply a number
exit 10
fi
typeset -i PARAM=$1
while [[ ${PARAM} -ge 0 ]]
do
if [[ ${PARAM} -eq 1 ]]; then
echo ${PARAM} cow
else
echo ${PARAM} cows
fi
((PARAM=${PARAM} -1))
done
|
|
#3
|
|||
|
|||
|
believe an old unix monkey
ksh is still NOT standard to sure run on all platform, be simply! use the good, old burn so your probl: Code:
#!/bin/sh
# don't forget on modern *nix is /bin a symlink to /usr/bin
# for backw compatibility reasons
${value:=$1}
#check the (or read a new) valid value
END=s
while [ $value -gt 0 ]
do case $value in 1) END= ;; esac
echo $value cow$END
value=`expr $value - 1`
#a faster way: value=`echo $value - 1|bc`
done
ps: (does not matter here but) case is 30% faster then if Last edited by guggach : October 19th, 2004 at 08:33 AM. Reason: typo |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > new to the unix world and in need of some help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|