LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-08-2019, 03:37 AM   #1
iammike2
Member
 
Registered: Oct 2018
Location: SE-Asia/Europe
Distribution: Raspi OS bullseye-arm64
Posts: 69

Rep: Reputation: Disabled
Bash some questions please


Guys,


sorry to bother you again. I have a couple of Bash questions


Suppose I have 2 scripts like this


script1

Code:
#!/bin/bash
echo "In script 1"
source script2.sh
echo "back in script 1"
exit 0

script2

Code:
#!/bin/bash
echo "in script 2"
exit 0
Question 1:
Is using "source" the best way to call another script or ???
I would prefer to have the scripts in the same Dir as the "Main" script, but if that is not a good idea, please let me know

Question 2:

Must I use exit 0 (or any other errorlevel) or use return to leave SCRIPT2 or is that not necessary ?

Question 3:

And must I use exit "number" at the end of the main script or is that also not necessary (not doing anything with the output (yet )

Thx again guys, really appreciated.

Last edited by iammike2; 10-08-2019 at 03:39 AM.
 
Old 10-08-2019, 04:02 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,310
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
1) The built-in command source executes the other script in the current shell environment. So that would override whatever you have in the shebang in the other script. Maybe that makes a difference, maybe not.

For example, if the first script runs with #!/bin/bash and the second script with #!/bin/sh, calling the second script with source will make it run under bash instead of sh.

2) Unless you called the second script with exec, it will return to the first script where it was called. However, the exit code it carries with it will depend on what you have at the end, see next.

3) A script will return the exit code from whatever command was its final one, unless it was overridden. So using exit command overrides the last command in the script and returns a final code for the script as a whole.

Code:
#!/bin/sh
/bin/false
versus

Code:
#!/bin/sh
/bin/false
exit 0
The first script above will return 1 and the second script will return 0. So exit is useful if you want to track whether the script as a whole succeeded or failed.
 
1 members found this post helpful.
Old 10-08-2019, 04:24 AM   #3
iammike2
Member
 
Registered: Oct 2018
Location: SE-Asia/Europe
Distribution: Raspi OS bullseye-arm64
Posts: 69

Original Poster
Rep: Reputation: Disabled
Thx.

Your examples made it perfectly clear !!
 
Old 10-08-2019, 04:25 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
the answer is no, you do not need to do that, but you can do that. It always depends on the situation, the requirements. Every case you listed is legal and usable.
 
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
Can you give a dog a bone? i need some help with some questions TheRavishingEgg Linux - Newbie 1 08-17-2017 06:23 AM
14.1 - some questions about errors at startup - and yes some programs now don't work Jeebizz Slackware 16 11-11-2013 07:41 AM
[LFS 7.3] Some questions on rationale behind some toolchain packages DeeGee Linux From Scratch 4 06-13-2013 03:30 PM
Doing some video processing and have some questions... blenderfox Linux - Newbie 2 04-17-2013 02:45 AM
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM

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

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