code placement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting code placement
# 8  
Old 01-14-2004
Thank you both so very much!! Works like it's supposed to and how I imagined it would!Smilie
# 9  
Old 01-14-2004
not reading the negative argument

I have one more question about the following lines of code:

#!/bin/ksh

if [ "$1" ]; then
num=$1
while [ $num -gt 0 ]; do
echo $num
((num=num-1))
done
elif [ "$1" ]; then
num=$1
while [ $num -le 0 ]; do
echo Command line argument must be a positive number and not zero
done

else
echo You must enter one number on the command line
fi

I don't receive any error messages when I run this, but when I enter a negative number on the command line, nothing happens. Any suggestions?
# 10  
Old 01-14-2004
If you are checking for positive numbers there are better ways to do it. But you could just change your if statement to:

if [ "$1" -gt 0 ]; then
just like you have in the while loop

Then make your echo statement read must be a postive number, and drop the elif.
# 11  
Old 01-14-2004
what the script is supposed to do is that I want it to accept an integer and do the following:
1. If it's positive and greater than zero, count down to one
2. If no argument is entered at the command line, then to display a message.
3. If a negative number is entered at the command line, I want a message to display saying to enter a positive number. I just can't get this step to work.

I'm just trying to learn loops and how to nest them inside of each other.
# 12  
Old 01-15-2004
That will accomplish everything in that list.


if [ "$1 -gt 0 " ]; then
num=$1
while [ $num -gt 0 ]; do
echo $num
((num=num-1))
done
else
echo Please Enter a Positive Integer
fi
# 13  
Old 01-15-2004
Quote:
Originally posted by scott78
I'm just trying to learn loops and how to nest them inside of each other.
Try this out, scott: Smilie
Code:
if [ "$1" ]; then
  num=$1
  if [ "$1 -gt 0" ]; then
    while [ $num -gt 0 ]; do
      echo $num
      ((num=num-1))
    done 
  else
    echo "Enter a positive number!"
  fi
else 
  echo "Enter a number!"
fi

# 14  
Old 01-15-2004
This: if [ "$1 -gt 0" ]; then
is not going to do what you think it might do.

But also, why are there two errors messages? First we reject input because it's not an integer, then we reject input because it's not a positive integer. User's don't like it when the requirements seem to change like that. Error messages really need to go to stderr as a rule anyway. How about:
Code:
#! /usr/bin/ksh

if [[ $1 = ?(+)+([0-9]) ]] ; then
      ((num=$1))
      while ((num)) ; do
            echo $num
            ((num=num+1))
      done

else

      echo enter a positive integer  >&2
fi
exit 0

Now as much as I love structured programming, I would never code like that in real life. I think that an early exit is fine. The real goal of structured programming is easily readable code. I only want to nest one structure inside another when I must. So I would actually go with
Code:
#! /usr/bin/ksh

if [[ $1 != ?(+)+([0-9]) ]] ; then
        echo enter a positive number >&2
        exit 1
fi

((num=$1))
while ((num)) ; do
       echo $num
       ((num=num+1))
done

exit 0

And to answer the question before it's asked, ?(+)+([0-9]) is a ksh pattern for a positive integer. ?() optionally matches any of the patterns inside the parentheses, so ?(+) will optionally match a plus sign. +() matches one or more of the patterns inside the parentheses, so +([0-9]) matches one or more numbers.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Parameters placement on stack in C

I am trying to illustrate the reverse order of parameters on the stack when passed to a function in C: #include <stdio.h> void p(int p1, int p2, double p3) { printf("params:\n" "1) %p offset = %li\n" "2) %p offset = %li\n" ... (5 Replies)
Discussion started by: migurus
5 Replies

2. Shell Programming and Scripting

[solved] awk: placement of user-defined functions

Hi folks, is there any recommendation, especially from a point of performance, about where to place a user-defined function in awk, like in BEGIN{} or if it is only need once at the end in END{}? Or doesn't it matter at all since, awk is so clever and only interprets it once, wherever it is... (3 Replies)
Discussion started by: zaxxon
3 Replies

3. UNIX for Advanced & Expert Users

Unix linked-list placement

Hi, I am programming in kernel, and I want to use a double linked list that holds infos that every process could access and modify THIS list. So, I suppose it is a 'global' variable since every process(thread) can reach it, I am wondering where to put it? by changing some of the kernel files? (1 Reply)
Discussion started by: louisTan
1 Replies

4. Cybersecurity

Snort/NTOP Placement

I have been asked to place 2 (1 NTOP & 1 SNORT) boxes within our network as part of our tool kit for network monitoring and Intrusion detection. Out network is very simplistic and it layed out like this: internet | | Cisco 1811 Router (8x Layer 2 switch ports) ... (0 Replies)
Discussion started by: metallica1973
0 Replies

5. Programming

How to override Window Manager placement of windows.

Hello, everyone! Is it possible to create a window in X11/WM, but override the position Window Manager sets for the window. I'm not sure how to use 'override_redirect' flag, and what to do in order to use it. But the problem with the flag is also that it probably will disable all decorations... (2 Replies)
Discussion started by: AOne
2 Replies

6. Solaris

Data placement in SVM

Is it possible to place the data in inner or outer edge with SVM ( solaris volume manager ) or VxVM like we can do in AIX LVM ? (1 Reply)
Discussion started by: fugitive
1 Replies

7. Shell Programming and Scripting

Kinda a noob: Automatic window sizing and placement

I am attempting to create a script that runs automatically upon logging in and opens and places windows in appropriate places. I have the script running such that it starts during login, but I cannot get things how and where I want them. This should be relatively simple, I just can't figure it out... (7 Replies)
Discussion started by: wydileie
7 Replies

8. UNIX Desktop Questions & Answers

Controlling icon placement?

Hi, I'm using xterm and aixterm with Mwm on AIX, and having trouble controlling icon placement when minimising windows. Basically when I run an xterm or aixterm I want to be able to specify where the icon will be if minimised. The man page mentions the "#geometry Geometry" option to aixterm,... (1 Reply)
Discussion started by: cunningdavid
1 Replies

9. AIX

Aixterm icon placement?

Hi, I'm using xterm and aixterm with Mwm on AIX, and having trouble controlling icon placement when minimising windows. Basically when I run an xterm or aixterm I want to be able to specify where the icon will be if minimised. The man page mentions the "#geometry Geometry" option to aixterm,... (1 Reply)
Discussion started by: cunningdavid
1 Replies
Login or Register to Ask a Question