Function call with argument doubt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Function call with argument doubt
# 1  
Old 08-28-2009
Error Function call with argument doubt

Hi all,

I am having a problem with user defined function call. I am new into the concept of shell script UDFs.

My function is:
Code:
iterate_directory()
{
cd $1
k=0
for i in *
    do
        if [ -d "$i" ]
        then
            ARR[k++]=${i}
        fi
    done
echo ${ARR[*]}
}

This is for iterate a directory and stored all sub-directories in an array

I called this function like this
Code:
directory="/tmp/"
array1=iterate_directory "$directory"

Please correct me, where I went wrong.
# 2  
Old 08-28-2009
Hi,

You forgot to mention the shell you are using. Assigning a value to an array by calling a function does not work. Neither in bash or in ksh. As bash and ksh function variable scope is global by default, your array ARR is visible outside your function.
# 3  
Old 08-28-2009
Quote:
Originally Posted by ripat
Hi,

You forgot to mention the shell you are using. Assigning a value to an array by calling a function does not work. Neither in bash or in ksh. As bash and ksh function variable scope is global by default, your array ARR is visible outside your function.
Oh, I am sorry.

I am using bash shell
Code:
GNU bash, version 3.2.48(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

if I drop that echo in my function is it going to work ?

Last edited by canishk; 08-28-2009 at 03:40 AM..
# 4  
Old 08-28-2009
No, as
Quote:
Originally Posted by canishk
Code:
array1=iterate_directory "$directory"

is not correct. You can not assign a value to an array that way as the function only returns a code, not a value.
Just do something like this:
Code:
iterate_directory()
{
cd $1
k=0
for i in *
    do
        if [ -d "$i" ]
        then
            array1[k++]=${i}
        fi
    done
}

# 5  
Old 08-28-2009
Power

Quote:
Originally Posted by ripat
No, as

is not correct. You can not assign a value to an array that way as the function only returns a code, not a value.
Just do something like this:
Code:
iterate_directory()
{
cd $1
k=0
for i in *
    do
        if [ -d "$i" ]
        then
            array1[k++]=${i}
        fi
    done
}

So there is no use of functions, if there is no means of re-usability then what is the need of functions ?Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditionally replace nth argument of every function call

I have very large perl source code file and I want to replace every occurrence function say foo,The function foo has some arguments and I want to replace 2nd argument,the current argument is hex integer and i want to replace it to equivalent string.Also I want to replace function name foo with... (4 Replies)
Discussion started by: pravint
4 Replies

2. Programming

Doubt with signals and sleep function

Hi , I have a doubt with signals and sleep function. In a program i have this while(1) { //do some work sleep(1); }And in a thread i have something like this union sigval data; char message; char msg; data.sival_int=0; while(1) { ... (4 Replies)
Discussion started by: bacesado
4 Replies

3. UNIX for Advanced & Expert Users

Doubt with fork() system call

Hi I wrote a simple fork program to illustrate the fork() system cal. here it is #include<stdio.h> #include<sys/stat.h> #include<sys/types.h> main() { int flag; flag=fork(); if(flag==0) { printf("Child \n"); printf("Process id= %d\n",getpid()); ... (3 Replies)
Discussion started by: badsha6642
3 Replies

4. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

5. Programming

Trivial doubt about C function pointer

Hi, In the below C code, #include <stdio.h> void print() { printf("Hello\n"); } int main() { void (*f)() = (void (*)()) print; f(); (*f)(); } I wonder, how the syntaxes "f()" and "(*f)()" are treated as same without any error? Is this an improvement or ANSI/ISO... (1 Reply)
Discussion started by: royalibrahim
1 Replies

6. Shell Programming and Scripting

pass function as argument to a function

I have the following code : function1 () { print "January" } function2() { case $1 in January) print "Dzisiaj mamy styczen" ;; *) ;; } main() { (1 Reply)
Discussion started by: presul
1 Replies

7. Shell Programming and Scripting

Passing argument to system call in awk script

So, I have this script. It reads a CSV file that has a mixture of object names with IP addresses (parsing out that part I have working), and object names which have a DNS name. I want to be able to run a "dig +short" based off of the name given to me in the line of the awk script, and then deal... (6 Replies)
Discussion started by: mikesimone
6 Replies

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

9. UNIX for Advanced & Expert Users

how to use exceptfds argument in select system call

Hi, Can any one tell me how to use the fourth argument of select system call.I saw example "port forwarding" on the net,but it was too complex for me to understand.Can any one explain me about the usage of exceptfds argument of select system call with simple example. Thanks. (2 Replies)
Discussion started by: bvijaya
2 Replies

10. UNIX for Dummies Questions & Answers

Pass argument to function

Hi, Can someone please explain to me how I can get a function to recognize a file given as an argument to a script. Suppose the script has the argument as follows: sh script file and the function is as follows: function display_file () { cat $1 } and it s then called #main program... (1 Reply)
Discussion started by: Knotty
1 Replies
Login or Register to Ask a Question