The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 20 02-02-2009 03:13 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 08: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 08:20 AM
If then else loop in Shell script pankajkrmishra Shell Programming and Scripting 4 07-31-2006 09:40 AM
Shell Script loop problem MaxMouse Shell Programming and Scripting 1 07-26-2005 03:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-29-2001
froggwife froggwife is offline
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?
  #2 (permalink)  
Old 11-29-2001
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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 (permalink)  
Old 11-29-2001
froggwife froggwife is offline
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.
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0