LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-24-2010, 09:34 AM   #1
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101
Blog Entries: 1

Rep: Reputation: 15
bash calling recursively a script


Hi guys, i got this issue
I have a script which can get one argument, or many. If only one argument then, it call itself with the default behavior, however i can not find the way to call the script within its own body and passing some arguments to it:

This is the body of the decision tree:
Code:
if [ -z "$1" ]
	then 
        echo "Usage: $0 arg_name functions_to_run"
        exit
	else
		if [ $# -le 1 ] 
			then
				echo "0_o Not enough arguments, this function will run at default: $1 func1 func2 func3"
				$* $1 func1 func2 func3
				exit
			else
				if [ $# -ge 2 ]
							then
								FOO=("$@")
								for (( i=1; i<=$# -1; i++ ))
									do
										flujo $1 ${FOO[$i]} &
									done
							else
								echo "o_0 I am a ghost 0_o"	
				fi
		fi
fi
I tried with ./$0, but did not work
 
Old 03-24-2010, 09:49 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
There is no dependable way for a script to find its own file as explained here.

One workaround would be to use a function and call it recursively.
 
Old 03-24-2010, 09:11 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not real sure I understand the question??

Firstly is 'flujo' a program / command on your machine??

Secondly, please explain this part further:

Quote:
i can not find the way to call the script within its own body
If catkin's response about using recursive nature is what you are after then you would need to do
one of two things (assuming I am getting any of this):

1. Write a wrapper script to call this one and then recursively call yours
2. Wrap the actions in your script into a function and call it recursively from within your script.
 
Old 03-25-2010, 10:05 AM   #4
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
about flujo

yep flujo is a function, which is called several times, with different arguments taken from the input when the script is called

What i was thinking is to made a default behavior, like if you have only one argument, to recall the script with three arguments added besides the first one.

let us say:

Code:
%name_of_script hola
if $# greater that one
then run script as normal
else call script with $1 new_arg1 new_arg2...
I was thinking lazy i know, right now i did an if fork to call the function internally with the arguments for the default operation, considering $#. How ever the doubt assaulted me, recursion for scripts is not possible then, i know the functions can be made recursively though.
 
Old 03-25-2010, 10:49 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
No need for the script to call itself again, simply adjust the arguments in this instance (but this is almost certainly not the best way of achieving whatever it is you want to achieve)
Code:
default_arg2=<something>
default_arg3=<something>
[[ $# -eq 1 ]] && set "$1" "$default_arg2" "$default_arg3"
<parse arguments>
<other stuff>
 
Old 03-25-2010, 04:43 PM   #6
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
i got lost

your code look interesting, care of explain it?

If i get it right, you actually add the arguments to the $@, array that allocates the input arguments given to the script, when called ...

Last edited by patolfo; 03-25-2010 at 04:47 PM.
 
Old 03-25-2010, 11:59 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by patolfo View Post
your code look interesting, care of explain it?

If i get it right, you actually add the arguments to the $@, array that allocates the input arguments given to the script, when called ...
Exactly. You can test it
Code:
for arg in "$@"
do
    echo "'$arg'"
done
The purpose of the double and single quotes is to make any leading or trailing whitespace visible.
 
Old 03-29-2010, 11:25 AM   #8
patolfo
Member
 
Registered: Jan 2006
Distribution: Debian-Sarge r2-k.2.6.8-2.386
Posts: 101

Original Poster
Blog Entries: 1

Rep: Reputation: 15
yes it is

Quote:
Originally Posted by catkin View Post
Exactly. You can test it
Code:
for arg in "$@"
do
    echo "'$arg'"
done
The purpose of the double and single quotes is to make any leading or trailing whitespace visible.
Yes it is what i wanted

Thanks a bunch!
 
Old 03-29-2010, 11:38 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by patolfo View Post
Yes it is what i wanted

Thanks a bunch!
You're welcome You can mark the thread solved using the Thread Tools menu.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script calling makefiles with CFLAGS prepended paulcsf Linux - Software 5 02-06-2010 06:24 PM
Bash script to transfer folders recursively without overwriting via FTP laurens Linux - Newbie 9 11-13-2009 07:40 AM
calling director, bash script Spinoz Linux - Newbie 6 09-30-2009 07:23 PM
calling user input in bash script dbmacartney Linux - General 3 05-19-2008 06:55 AM
bash script calling mplayer afrodocter Programming 13 08-23-2005 10:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration