Recursive function call problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursive function call problem
# 1  
Old 01-26-2009
Lightbulb Recursive function call problem

This is shell script I have made to lists out directory contents and filenames for any given directory (without using ls command). There is some problem in dirfunc function call which I have marked 1 is not working. Can anybody suggest what is the problem there and how should I correct it.

Code:
dirfunc()
{ path=$1/$2
  if test -d $path
  then
   echo "--------------------------------------------------------------------------------"
   dir=Sub-$3
   echo " $dir:   $path    "
   dir $path > inven
   declare -a no
   awk 'BEGIN{ t = 0}
     { { for(i=1 ; i<=NF ; i++)
      {
       no[t+$i] = $i
       dirfunc $path no[t+$i] $dir    _____________________(1)not working
      }
        t = t + NF
      }
    
     }' inven 
   echo "--------------------------------------------------------------------------------"
  else
   if test -f $path
   then
   echo "    $2   : File in $dir "
   fi
  fi
}

dirfunc /users/mti2007/apakhale/Desktop demodir.d Directory

Last edited by otheus; 01-28-2009 at 07:26 AM.. Reason: [code] tags, removed unix.com img buttons
# 2  
Old 01-26-2009
There are a couple of things I'm unfamiliar with here.
First of all -- my shell script doesn't have a "declare" command.

Secondly, if it did, I couldn't pass a variable into awk the way
you are doing in this script.

Why not do something like this:

find $dir_name -print

Are you looking for some special formatting?
# 3  
Old 01-28-2009
Ah, you're trying to invoke a shell function from within awk. I'm sorry, that simply won't work. The best you could do, is define the shell function separately, put it in a RC startup file, create the awk command separately and put it in it's own file. Then the shell function invokes the awk script, and the awk command invokes the shell in such a way that the RC file is read, the function therefore loaded and executed.

But surely, there is a better approach to what you are trying to do. As quirkasaurus suggested, look up the find command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

recursive function

Hello forum members, Please wirte a sample program for print the 1 - 100 and 100 -1 using recursive function. Thanks & regards Siva Rangnanath (2 Replies)
Discussion started by: workforsiva
2 Replies

2. Shell Programming and Scripting

AWK Problem in recursive function

Hi, I have a file like this SPF_HC00001|iCalcular_Monto_Minimo|--->|SPF_HC00028|pstcObtener_Monto_Minimo SPF_HC00004|iCalcular_Incrementos|--->|SPF_HC00032|pstcObtener_Num_Incrementos SPF_HC00005|iCalcular_Articulo_167_Reformado|--->|SPF_HC00031|pstcObtener_Por_CB_Inc... (2 Replies)
Discussion started by: kcoder24
2 Replies

3. Shell Programming and Scripting

Recursive function and arrays

I have the following function in a bash script that fails to return the sorted array. I think the problem lies in the recursion not correctly passing the arrays, but I can't tell what I'm doing wrong. Anyone see the problem? function quicksort () { local array=( `echo "$1"` ) local... (7 Replies)
Discussion started by: tkg
7 Replies

4. Shell Programming and Scripting

awk , function call problem

#!/bin/bash awk ' function ad(t,r){ return (t+r); } BEGIN{ print ad(5,3); } { print ad(5,3); } ' Doesn't print anything for the last print ad(5,3); (6 Replies)
Discussion started by: cola
6 Replies

5. Shell Programming and Scripting

Recursive function in unix

Can someone tell me, how do i write a recursive code using shell ( eg like 'for' loop in C) which outputs the record to a database table as one row per iteration? (7 Replies)
Discussion started by: goutam_igate
7 Replies

6. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

7. Shell Programming and Scripting

Recursive call to find files and directories in Shell script from current path.

################################################################ Copy this script to your path from where you want to search for all the files and directories in subdirectories recursively. ################################################################# code starts here... (2 Replies)
Discussion started by: Ramit_Gupta
2 Replies

8. Shell Programming and Scripting

Problem with Recursive function

Hi all, I have to move all the files in a tree directory structure to a single directory. Inorder to know which file is from which directory , i'll have to add the name of the directory to the file name. For this i wrote a recursive function which is as follows... (4 Replies)
Discussion started by: malle
4 Replies

9. Programming

recursive function

Hi everyone, i need your input on this. We can express a function recursivly like this A(n) = (2 n = 0 5 n = 1 A(n − 1) + A(n − 2) % 47 n > 1 How would i go about constructing a recursive function for this?... (1 Reply)
Discussion started by: bebop1111116
1 Replies

10. Shell Programming and Scripting

Recursive method call getting terminated ???

Hello people, Need help !!! What am I doing wrong here ? I am writing a function to recursively list the files under a folder and it's sub-folders. Problem is once it list the files under the innermost folder, it terminates. What do I need to do so that it returns and list files under the... (3 Replies)
Discussion started by: tipsy
3 Replies
Login or Register to Ask a Question