LinuxQuestions.org
Help answer threads with 0 replies.
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 07-09-2013, 09:38 AM   #1
lidi
LQ Newbie
 
Registered: Jul 2013
Distribution: fedora
Posts: 1

Rep: Reputation: Disabled
problem with function calling functions in bash


Hi all,

I wrote a script to update some text files (which contains a list of files), but I made some mistakes.
In the script I defined a function func_ls that takes as parameters the type of file (_type_ls_='tot' or 'ok'), call the corresponding updating function (_func_lsx_=ls_ok or ls_tot), and do some other small things.
I wanted then to add the possibility to call the function with 'all', so that it would call first func_ls with 'tot' and then with 'ok' and I defined also ls_all.
As I defined it, ls_all stops after calling ls_tot and does not go on.
Can you please explain me why or give me some hints or advices ?

Thanks !

code:
func_ls() {
local _type_ls_=$1
local _path_=$2
local _func_lsx_=`det_func_ls $_type_ls_`
local _station=`det_stat_from_path $_path_`
local _filels=`det_name_filels_new $_type_ls_ $_path_`

eval "$_func_lsx_ $_path_ $_station $_filels"

## some other things to do later
}



ls_all(){
local _path_=$1
local _station=$2
local _filels=$3
func_ls 'tot' $_path_ $_station $_filels
func_ls 'ok' $_path_ $_station $_filels
}
 
Old 07-09-2013, 12:33 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Firstly the way you have it, you do not have to re-assign the arguments to variables, you can pass them to func_ls as is:

Code:
func_ls tot $@
Should be sufficient.

The question remains what the form/shape of what you want to pass to func_ls be. For instance you can put "tot" or "ok" into another variable and then pass that. Ultimately you want it to be just tot or ok without punctuation, correct? I think you should try passing it without the quotes as I've shown.

When you enter into func_ls, you can echo $1 to see what it appears like. So do that.

Also assuming that you have a main part of this script, because ls_all() is also a function, therefore it won't run without a main section. There is no designator excepting bash commands which are not bound within function framing, to make your main section.
 
1 members found this post helpful.
Old 07-09-2013, 02:45 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 would suggest that if you have resorted to using 'eval' then you should rethink the entire script.

If you explained your requirement better we may be able to assist.
 
Old 07-09-2013, 07:02 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
If you're using Bash, you could just use indexed arrays and won't need to have to use eval. You just have to put every argument of the command which would include the "command" itself to each element.
Code:
function call_a_function {
    FUNC=$1; shift   ## Equivalent to
    ARGS=("$@")      ## FUNC=$1; ARGS=("${@:2}")
    COMMAND=("$FUNC" "${ARGS[@]}")
    "${COMMAND[@]}"  ## Execute command.
}
call_a_function do_something a b c
Which is just equivalent to:
Code:
function call_a_function {
    FUNC=$1; shift
    "$FUNC" "$@"
}
or
Code:
function call_a_function {
    "$1" "${@:2}"
}
or
Code:
function call_a_function {
    "$@"
}
 
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
Calling a function in 1 sec without using any timer based function/API barunparichha Linux - Software 3 09-09-2011 02:15 PM
problem calling fftw3 library function while using gfortran compiler. Please help. buddyamit Programming 5 07-08-2011 12:07 AM
bash function calling problem arashdx Linux - Newbie 1 03-10-2009 03:42 PM
QT calling functions ... How? Four Programming 3 02-11-2007 10:32 AM
Calling functions in C++ oulevon Programming 3 01-28-2005 03:14 PM

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

All times are GMT -5. The time now is 09:09 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