A Question On Recursion In Ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A Question On Recursion In Ksh
# 1  
Old 01-11-2007
A Question On Recursion In Ksh

Hi Folks,

I would just like to know how recursion works in ksh or inside a shell in general. I created the following script, but it works(runs recursively) only for 64 times:
----------------
#! /usr/bin/ksh
displaymessage()
{
echo "displaying count: $cnt "
echo "wait for 1 second..."
sleep 1
cnt=$cnt+1
main

}

main()
{
displaymessage
}

#start here
typeset -i cnt=1
main
---------------

The 'main' function calls the 'displaymessage' function, whereas the displaymessage calls 'main' inside it (so is recursive).

The error that i get is: "main: recursion too deep"

Question: How do I make a perfect recursion???
# 2  
Old 01-11-2007
Recursion is allowed (where i work) at a depth of 9 iterations. Basically what your script is doing is:

run main,
---> call message,
---------->run main,
-------------->call message

That is the it re-runs the main command again, it runs itself and thus can never exit, and the functions never exit.

What you need to do is control this using a loop in main, so possibly:


Code:
#! /usr/bin/ksh
displaymessage()
{
echo "displaying count: $cnt "
echo "wait for 1 second..."
sleep 1
cnt=$cnt+1

}

main()
{
while true
do
    displaymessage
done
}

#start here
typeset -i cnt=1
main

This User Gave Thanks to Cranie For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh prompt yes/no question

I am writing a code like this in KSH . #!/bin/ksh echo is this SQL query ? yes or no read answer case $answer in yes|Yes|y) echo got a positive answer ;; no|n) echo got a 'no' ;; esac Here after ... (2 Replies)
Discussion started by: talashil
2 Replies

2. UNIX for Dummies Questions & Answers

Question on using a variable in KSH

Hi all, The below command tries to copy ".tgz" instead of "hello_test.tgz" -- It seems as if the underscore gets in the way. I tried with different ways of using quotes, with no luck, unfortunately...it's probably very simple, but may I ask how this would be done: How would the below be... (3 Replies)
Discussion started by: chatguy
3 Replies

3. Shell Programming and Scripting

Expression recursion level question

Hi. I am receiving this error message for the highlighted line (let "total=$total+$sales"). line 11: let: total+sales:expression recursion level exceeded (error token is "total+sales") counter=0 sales=0 total=0 echo "enter sales price" read sales total=total+sales while test $sales ; do... (5 Replies)
Discussion started by: Ccccc
5 Replies

4. Shell Programming and Scripting

KSH: recursion into subdirectories?

Hello, I'm scripting a newbie. I'm using KSH on HP-UX. I'm trying to write a script that will change a whole directory of file names into UPPER CASE. I have the "convert to upper case" part of it working fine: ls | while read filename; do typeset -u uppercase uppercase=${filename} ... (2 Replies)
Discussion started by: Wotan31
2 Replies

5. Shell Programming and Scripting

ksh if block question

Hi, I am looking at a script, and it contains lines like: if ] ... This is getting me confused. Why do we need $ before (echo $* | egrep -c 'DG')? Why can't we simply have: if ] ... i.e. no $ here before the ()... Thanks. J (3 Replies)
Discussion started by: JamesByars
3 Replies

6. UNIX for Dummies Questions & Answers

ksh question

How can I know if my system has ksh feature? #!/usr/bin/ksh Which command we allow me to see? Please advise! (1 Reply)
Discussion started by: bobo
1 Replies

7. Shell Programming and Scripting

Conversion question about ksh

Hi all, New to ksh and had a few questions to see if this is doable in ksh or if I am going to have to call out to a tcl procedure. I have an Ascii file I want to convert to hex then search and remove all hex chars '0A' and then convert back to Ascii. Here is an example of an Ascii file I am... (2 Replies)
Discussion started by: hgjdv
2 Replies

8. Shell Programming and Scripting

KSH scripting question

Good day, I am trying to write a script that completes an ssh challenge to a specific server and writes output to a log. We have shared ssh keys for the script user. My challenge is a simple hostname check; ssh $host hostname My problem arrises when there is an actual challenge, be it... (3 Replies)
Discussion started by: M-Pacificorp
3 Replies

9. Shell Programming and Scripting

Question about KSH line.

Wondering what this line meant, especially the 2>&1 and ${RUN_DIR} parts: ${RUN_DIR}/<filename> 2>&1 Where <filename> is the location and name of a file. (1 Reply)
Discussion started by: CapsuleCorpJX
1 Replies

10. Shell Programming and Scripting

How to ask a question to a user in ksh

Hello, I am writing a script in ksh(for HP Unix)where a user must answer to a question . So I want to know kow to test if the user do not answer , so if he enter "REturn". I don't know how to test "space characters" or "empty characters Thanks for your help (2 Replies)
Discussion started by: steiner
2 Replies
Login or Register to Ask a Question