LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2021, 12:37 PM   #1
__John_L.
LQ Newbie
 
Registered: Feb 2021
Posts: 14

Rep: Reputation: Disabled
bash question w/ nested function definitions


I've created some function definitions thusly:

dfn.sh
Code:
   set -a
   fcn1 () {
      echo #1
   }
   fcn2 () {
      echo #2
   }
   fcn3 () {
      echo #3
   }
Then I issue:

main.sh
Code:
   . ./dfn.sh & wait $!
   fcn1 | fcn2 | fcn3
but I get "fcnx : command not found."

Thanks in advance for any insight you can provide.
 
Old 03-28-2021, 01:22 PM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,604

Rep: Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547Reputation: 2547
Quote:
Originally Posted by __John_L. View Post
but I get "fcnx : command not found."
The code you posted does not produce that error.

Either post the real code, or post a new example that you have first verified demonstrates the actual issue you are having.

 
Old 03-28-2021, 02:01 PM   #3
__John_L.
LQ Newbie
 
Registered: Feb 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
I issued
Code:
set -x
and obtained the following:

Code:
+ . ./main.sh
++ wait 8163
++ . ./dfn.sh
+++ set -a
[1]+  Done                    . ./dfn.sh
++ fcn2
++ fcn3
++ fcn1
++ /data/data/com.termux/files/usr/libexec/termux/command-not-found fcn3
++ /data/data/com.termux/files/usr/libexec/termux/command-not-found fcn1
++ /data/data/com.termux/files/usr/libexec/termux/command-not-found fcn2
(remainder omitted for clarity).
 
Old 03-28-2021, 02:02 PM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
I guess your code cannot work. When you make dfn.sh be sourced asynchronously, it will be done in a separate process. The functions defined there will be exported to the children of that process, and you're trying to call them from its parent.

The FPATH approach used in Ksh may work though. See the autoload.v3 example for how it could be emulated in Bash.
 
Old 03-28-2021, 08:18 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,714

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
Code:
   . ./dfn.sh & wait $!
What is the purpose of using wait?

Remove the & wait $! from your source line and try running your program again.
 
1 members found this post helpful.
Old 03-29-2021, 01:07 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
yes, would be nice to post the real dfn.sh.
As it was mentioned & and wait is completely useless here, you can safely remove them.

Actually defining functions in a script and sourcing that script (that was the dot dnf.sh) should work, so I guess the real script is not that simple. So please do not post just a few [random] lines from somewhere.....

otherwise probably:
Code:
declare -f fcn1
declare -f fcn2
...
may help
 
1 members found this post helpful.
Old 03-29-2021, 02:29 PM   #7
__John_L.
LQ Newbie
 
Registered: Feb 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
The '& wait $!" was added during my gyrations in an attempt to fix the problem. Apparently, it was the source of the problem. C'est la vie.

Thanks all.
 
  


Reply

Tags
bash function



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
[SOLVED] bash question on nested braces CelticFiddler Linux - Newbie 11 06-30-2022 07:32 AM
[SOLVED] BASH Question - nested case statements jbeiter Linux - Software 2 02-13-2012 10:08 AM
[SOLVED] Bash: how embed variable definitions within constants so they can be interpreted porphyry5 Programming 9 06-30-2011 11:30 PM
C++ function declarations and definitions... AM1SHFURN1TURE Programming 2 08-29-2005 06:57 PM
system() function in nested if else/case! natalinasmpf Programming 10 01-26-2004 06:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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