LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-02-2016, 03:04 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 400

Rep: Reputation: 0
Need help with sourcing a file inside a bash script.


Hello All,

I am written a script(A) wherein I need functions from another script(B).

However the problem is B script has functions which task $1 variable and perform the functions.

When I source B in A, B gets executed which I don't want. I only want its definitions to be inherited in A so that I can call functions from B in A directly.

The problem is when I source B in A and start executing A, B gets called and starts executing and therefore the A doesn't get completed anyway.

Is there any way to solve this problem? I mean source B and use it as and when required?
 
Old 06-02-2016, 03:07 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,839
Blog Entries: 6

Rep: Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667
It depends on how B is written.

If it contains only functions, then you should be able to load those functions.

If it includes statements outside those functions, and/or calls any of its functions, it will do that too.
 
Old 06-02-2016, 03:09 AM   #3
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Very hard to help unless you show the script. It is too early in the morning for me to get the logic behind all the 'if A then B, else B is A' stuff. Code is easier to read.

Best regards,
HMW
 
Old 06-02-2016, 03:14 AM   #4
Tonus
Member
 
Registered: Jan 2007
Location: Paris, France
Distribution: Slackware-current
Posts: 407
Blog Entries: 3

Rep: Reputation: 106Reputation: 106
Need help with sourcing a file inside a bash script.

Could you extract the functions in C and source C in A and in B ?
 
Old 06-02-2016, 10:12 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,643

Rep: Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960Reputation: 2960
Above advice from others is correct B either needs to be only definitions or the function need to be extracted and executed from a new file.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
extracting values from web page to simple text file (inside a bash script) ondoho Linux - General 5 06-29-2014 12:51 PM
Sourcing a file in a script TenaciousRock Linux - Software 9 10-08-2013 08:47 PM
Sourcing Environment File in Shell Script linux098 Linux - Newbie 6 10-22-2012 03:38 AM
[SOLVED] Bash script to parse the Windows version written inside the license.rtf file marietto Linux - General 11 09-19-2010 06:03 PM
bash script and grep inside file? agrinog Programming 3 04-02-2010 03:26 PM

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

All times are GMT -5. The time now is 06:07 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration