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 Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bourne Shell script - log for users loggin on and off noodlesoup Shell Programming and Scripting 14 09-08-2006 10:30 PM
Bourne: How to invoke an alias from within a shell script techshots Shell Programming and Scripting 2 06-04-2006 12:38 AM
cd from a Bourne Shell Script - Please Help fawqati Shell Programming and Scripting 10 05-25-2006 03:26 AM
Bourne Shell Script dmhonor914 UNIX for Dummies Questions & Answers 2 12-10-2003 12:25 PM
bourne shell script psrinivas Shell Programming and Scripting 2 12-06-2001 03:38 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 10-18-2001
dezithug dezithug is offline
Registered User
  
 

Join Date: Oct 2001
Location: Los Angeles
Posts: 6
Question Bourne shell script need help please ?

i have this assignment.. and i mad this script but there is something wrong with it.. if anyone can tell me.. watz going on... i would appreciate it.. tHnX in advance..
Code:
count=1
val=$2
op=$1
ans=0
if [ $op = "-e" -o $op = "-o" ]
then
        if [ $op = "-e" ]
        then
                while [ $count -le $val ]
                do
                        ans=`expr $count % 2`
                        if [ $ans -eq 0 ]
                        then
                        echo "$count \c "
                        count=`expr $count + 1`
                        fi
                done
        elif [ $op = "-o" ]
        then
                while [ $count -le $val ]
                do
                        ans=`expr $count % 2`
                        if [ $ans -ne 0 ]   
                        then
                        echo "$count \c "
                        count=`expr $count + 1`
                        fi
                done
        fi
else
        while [ $count -le $val ]
        do
                echo "$count \c "
                count=`expr $count + 1`
        done
fi
ThnX again

added code tags for readability --oombera

Last edited by oombera; 02-20-2004 at 11:51 AM..
  #2 (permalink)  
Old 10-19-2001
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
One thing I did notice is that if you are using Linux, you should change the echo statements to "echo -e". That will let the \c operators work.

What exactly are you trying to do here? I'm having a tough time reading the script (look, it's late, and I've been plugging some overtime on top of my normally long days...). It looks like you could use some comments...

Have you leaned how to use "case" yet? If so, you may look into using that - you could cut a lot of the confusing "if" statements out of there...
  #3 (permalink)  
Old 10-19-2001
dezithug dezithug is offline
Registered User
  
 

Join Date: Oct 2001
Location: Los Angeles
Posts: 6
well see.. i 'm using SCO UNIX .. nd.. everything works.. fine.. except in the loop something iz wrong.. when i try to run it.. it will juss keep on going... for some reason... nd its soo confusing..

we haven't learnt CASE yet.. i know it would be easier.. but.. i donno how to use CASE..

if yoiu still can think of something.. dat would be gr8

ThnX
  #4 (permalink)  
Old 10-23-2001
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Well, first off, it helped me a lot to look at the code with indents in it:
Code:
#!/bin/ksh
count=1 
val=$2 
op=$1 
ans=0 
if [ $op = "-e" -o $op = "-o" ] 
then 
     if [ $op = "-e" ] 
     then 
     while [ $count -le $val ] 
     do 
     ans=`expr $count % 2` 
          if [ $ans -eq 0 ] 
          then 
          echo "$count \c " 
          count=`expr $count + 1` 
          fi 
     done 
     elif [ $op = "-o" ] 
     then 
     while [ $count -le $val ] 
     do 
     ans=`expr $count % 2` 
          if [ $ans -ne 0 ] 
          then 
          echo "$count \c " 
          count=`expr $count + 1` 
          fi 
     done 
     fi 
else 
while [ $count -le $val ] 
do 
echo "$count \c " 
count=`expr $count + 1` 
done 
fi
Now, the first thing I usually try when trying to figureout what my script is doing is to add a "set -x" line at the top of the script. That will echo out each step the shell is performing, so you can see what's going on...

So, The way I ran the script, (I'll give an example of what I think I saw trying to use the" -e 4 "option) it got stuck in a loop at:
+ ans=1
+ '[' 1 -eq 0 ']'
+ '[' 1 -le 4 ']'
++ expr 1 % 2
Ok, interesting... let's look at this. So, what the script is doing step by step:
Code:
     if [ $op = "-e" ]                       # This is true in our case
     then 
     while [ $count -le $val ]          # while 1 is less than or equal to 4
     do 
     ans=`expr $count % 2`         # ans = 1 % 2, or ans = 1
          if [ $ans -eq 0 ]                  # if ans (1) = 0, then continue... nope - can't continue
          then 
          echo "$count \c " 
          count=`expr $count + 1` 
          fi                                         # We're done, time to loop while waiting for ans to equal 0
Do you see what you problem is? Hint: $ans will always equal 1 in this case

Here's another (messier, but more info) way of doing it:
Code:
if [ "$op" = "-e" -o "$op" = "-o" ]
echo " if [ $op = -e -o $op = -o ]"
        then
echo "     then"
        if [ "$op" = "-e" ]
echo "     if [ $op = -e ] "
        then
echo "     then"
        while [ "$count" -le "$val" ]
echo "     while [ $count -le $val ] "
        do
echo "     do"
        ans=`expr $count % 2`
echo "     ans=`expr $count % 2` "
                if [ "$ans" -eq "0" ]
echo "          if [ $ans -eq 0 ] "
                then
echo "          then"
                echo -e "$count \c "
echo "          echo -e $count \c"
                count=`expr $count + 1`
echo "          count=`expr $count + 1`"
This way, you can watch your script in action... It almost looks as if it doesn't know when to exit, right?

You may need a little re-designing, but I think you're pretty much on the right track

(Ooh, and I appologize, I changed some of the "echo"'s to "echo -e"'s, since I was looking at this on a Linux system...

Does this help any?
  #5 (permalink)  
Old 10-24-2001
dezithug dezithug is offline
Registered User
  
 

Join Date: Oct 2001
Location: Los Angeles
Posts: 6
see.. i still donno watz.. wrong.. but.. wat now i'm trin to do iz.. break da program in to lil programz.. and.. trin only -e option.. only for that.. and forget about other options.. hope that will help lol.. but.. thnX nwyz.. i tried.. your code.. but still didnt get.. wat happend.. i know its.. the modulus.. thing screwed up..
ahh.. lol.. i've been workin on this for like 3 weekz.. now.. lol. and its due on nov. 8th.. i have.. some more time..


thnx.. again though.. i appreciate it
  #6 (permalink)  
Old 10-24-2001
dezithug dezithug is offline
Registered User
  
 

Join Date: Oct 2001
Location: Los Angeles
Posts: 6
thank you veryyyyy muchhhhh !!!!

i finally got it.. to work whoooooooooooooooohooooooooooooooooooooooooooo.. lol.. damn.. dat took me.. long nuff. ..lol.. such a simple program. nd took me.. bout 3 weekz.. damn..

nwy..z. thNx for your help bro.. i appreciate it... very much..



Pz out!!

ThNx Again !!
  #7 (permalink)  
Old 10-24-2001
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
Right on! Can you post your solution to it for us to see?
Sponsored Links
Closed Thread

Bookmarks

Tags
linux

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 07:50 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