The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with while loop in shell script rkrgarlapati Shell Programming and Scripting 16 07-29-2008 09:34 AM
Help shell script to loop through files update ctl file to be sql loaded dba_nh Shell Programming and Scripting 1 04-15-2008 05:00 PM
Infinite Loop in Autosys while running a shell script, Manual run is fine sharmagaurav_2k Shell Programming and Scripting 2 09-04-2007 05:20 AM
If then else loop in Shell script pankajkrmishra Shell Programming and Scripting 4 07-31-2006 06:40 AM
Shell Script loop problem MaxMouse Shell Programming and Scripting 1 07-26-2005 12:19 PM

Closed Thread
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-28-2001
Registered User
 

Join Date: Nov 2001
Posts: 2
Unhappy shell script - loop to countdown

I am taking a class in UNIX and have written a script that needs to countdown from a number that is read in from the keyboard to zero. If no number is given the start of the countdown should default to 10.

I can't get this to do the default

#! /bin/sh
echo Enter a number here to countdown from a number to 0
read x
if [ -n $x ]; then
count=$x
else
count=10
fi

while [ $count -ge 0 ]

do
echo $count seconds
count=`expr $count - 1`
done
echo Off we go!


Does anyone know what I am doing wrong?
Forum Sponsor
  #2  
Old 11-29-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
We're really not supposed to help folks with homework assignments on this board. But you've done most of the work and you're stuck on a very tricky problem.

if [ -n $x ]

will not work, If there is nothing in x the command becomes:

if [ -n ]

So you need to use

if [ -n "$x" ]

Now when threre's nothing in x, the command becomes

if [ -n "" ]
  #3  
Old 11-29-2001
Registered User
 

Join Date: Nov 2001
Posts: 2
thank you

I actually realized that it had to be something similar to this last night as I had moved onto the next scrip to work on. I like UNIX scripting, but as with every other language I have taken it is the syntax that really throws me for a loop -- hee hee no pun intended.

Thank you for helping me. I will not ask for Homework help again. I don't want to get in trouble.
Google The UNIX and Linux Forums
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0