Bourne shell script need help please ?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Bourne shell script need help please ?
# 1  
Old 10-18-2001
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 Smilie

added code tags for readability --oombera

Last edited by oombera; 02-20-2004 at 11:51 AM..
# 2  
Old 10-19-2001
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  
Old 10-19-2001
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  
Old 10-23-2001
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  
Old 10-24-2001
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  
Old 10-24-2001
thank you veryyyyy muchhhhh !!!!

Smilie Smilie 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.. Smilie



Pz out!!

ThNx Again !!
# 7  
Old 10-24-2001
Right on! Can you post your solution to it for us to see?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

New user needs help with bourne shell script

This is my question, below the question is the template Write and execute a Bourne shell script called homework that will From within the script, create three background processes: a) (2 points) one that saves a long listing of your hidden files to a file named hiddenlist b) (2 points) ... (4 Replies)
Discussion started by: luislozoya
4 Replies

2. Shell Programming and Scripting

Open a file from within a Bourne shell Script

I am suppose to make a program that cuts the extension off of a file. It uses that .ext to decide which program needs to be used to open the file. This only finds the name of the program from a list of programs I made. My problem is when it has the name of the program it needs to use, how can I... (3 Replies)
Discussion started by: dordrix
3 Replies

3. Shell Programming and Scripting

bourne shell script error on line containing declare...

Hi, Get the following error when running a shell script with following statement. Syntax error at line 150 : `(' is not expected 150: declare -a VPO_SEV=(Normal Warning Minor Major Critical) it runs fine using bash, so I guess the script should be using bash but is there a... (1 Reply)
Discussion started by: wilsonee
1 Replies

4. Shell Programming and Scripting

help with bourne shell script

Attempting to write a script to eventually notify me via email for when there is packetloss across the backbone. I am looking for values greater than 0% in the mtr field. #!/bin/sh target=www.google.com date +"%D"_"%T" >> /home/rich/mtr.log echo "----------------------------------------" >>... (1 Reply)
Discussion started by: closedown
1 Replies

5. Shell Programming and Scripting

Bourne Shell script - log for users loggin on and off

Hello all, I'm new to shell scripting and want to make a script that I can write to log the users logging on and off the a unix system. I have had a good look over the past few days to crack it, I think I am getting close. I want a script that runs an infinite loop to check every 5 seconds... (14 Replies)
Discussion started by: noodlesoup
14 Replies

6. Shell Programming and Scripting

Bourne: How to invoke an alias from within a shell script

Bourne: How to invoke an alias from within a shell script If I type in the alias in the command line, it runs If I insert that same alias into my shell script and run the shell script, the alias is not invoked. Help please. (2 Replies)
Discussion started by: techshots
2 Replies

7. Shell Programming and Scripting

cd from a Bourne Shell Script - Please Help

Dear Bourne Shell Expert, I am trying to change the current working directory from within a Bourne Shell script. Simply enough i thought ! As I am sure you are well aware, Inside the script i echo `pwd` and it seems ok, but the shell spawns another shell to execute this and as such, when my... (10 Replies)
Discussion started by: fawqati
10 Replies

8. Shell Programming and Scripting

Trouble using substr function with Bourne shell script

Hi, I'm a newbie to UNIX scripting and I'm having some trouble compiling my script. I'm using the Bourne Shell and cannot seem to use the substr function correctly. I'm trying to extract the last two digits of a year that's stored in a variable based off of a condition. I've searched the... (4 Replies)
Discussion started by: E2004
4 Replies

9. UNIX for Dummies Questions & Answers

Bourne Shell Script

Hello, I'm throwing this out there as a novice to the Unix world...I've been working on a project that requires me to ouput (using the echo command) a list of names in a single column format, but the problem is the input is in row format followed by a blank space...If anyone could give me a... (2 Replies)
Discussion started by: dmhonor914
2 Replies

10. Shell Programming and Scripting

bourne shell script

Hi all, Can somebody answer the following query Thanks, Srinivas A shell program that takes one or any number of file directory names as input; sorts the directories given as parameters jointly in the ascending or decending order of choice For EX : dips abc etc desc will sort the files... (2 Replies)
Discussion started by: psrinivas
2 Replies
Login or Register to Ask a Question