UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old October 18th, 2004, 02:19 PM
jstar571 jstar571 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 1 jstar571 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question new to the unix world and in need of some help

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?

Reply With Quote
  #2  
Old October 19th, 2004, 07:56 AM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
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

Reply With Quote
  #3  
Old October 19th, 2004, 08:25 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 33 m 4 sec
Reputation Power: 9
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > new to the unix world and in need of some help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway