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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-05-2017, 06:23 AM
|
#1
|
LQ Newbie
Registered: Jun 2010
Posts: 17
Rep:
|
Calling sub scripts from control script in Unix shell
I'm working on a control script that calls multiple other sub-scripts. However, when I try to run one of the sub-scripts with arguments the script doesn't see the arguments
. ./subscript.sh var1 var2
when I reference $1 from the sub script "echo $1" it comes up blank
Initially I thought $1 from the sub script was referencing $1 from the control script but then I realised $1 from the control script has a value.
I'm not sure if it has to do with the method in which I am calling the sub scripts with the double dots.
. ./subscript.sh var1 var2
Help and suggestions are all welcome.
O.
|
|
|
09-05-2017, 06:54 AM
|
#2
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 6,016
|
Just a little research should have resulted in several ideas that might serve. One is running the subscripts in a subshell using ( shell param1 param2 ) syntax. The power of the paren. If you need the value of the output then you can wrap it in an if statement or assign the output to a variable.
|
|
|
09-05-2017, 07:41 AM
|
#3
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
Original post deleted.
Sourcing instead of executing through shell is not the problem.
jlinkels
Last edited by jlinkels; 09-05-2017 at 12:07 PM.
|
|
2 members found this post helpful.
|
09-05-2017, 07:42 AM
|
#4
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,916
|
It would be a lot more helpful if you showed more of your code using [code][/code] brackets to enclose it.
Enable bash debugging with the statement: after your #!/bin/sh statement.
Answer the questions: What are var1 and var2 for us? Meaning, how did you populate them? Or are they an abstraction which you used to try to explain your script?
|
|
1 members found this post helpful.
|
09-05-2017, 09:16 AM
|
#5
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
|
You can also use functions in the shell script if you want to keep everything in one place and not call extra scripts.
Code:
#!/bin/sh
set -x -e
foo() {
echo "foo has '$1' as the first argument.";
}
date +"%F";
foo bar;
date +"%T";
exit 0;
|
|
2 members found this post helpful.
|
09-05-2017, 11:58 AM
|
#6
|
Moderator
Registered: Aug 2002
Posts: 26,366
|
In addition:
From the man bash page
Quote:
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command exe‐
cuted from filename. If filename does not contain a slash, file
names in PATH are used to find the directory containing file‐
name. The file searched for in PATH need not be executable.
When bash is not in posix mode, the current directory is
searched if no file is found in PATH. If the sourcepath option
to the shopt builtin command is turned off, the PATH is not
searched. If any arguments are supplied, they become the posi‐
tional parameters when filename is executed. Otherwise the
positional parameters are unchanged. The return status is the
status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or
cannot be read.
|
It is possible to call your script using source but as rtmistler pointed without context or seeing some of your code we do not know where it is going astray. As already stated do var1 and var2 exist and have values?
|
|
1 members found this post helpful.
|
09-05-2017, 01:17 PM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,302
|
or you just need to use:
Code:
subscript.sh "$var1" "$var2"
But I do not really understand your problem, would be nice to give a better example or sample code.
In your case $1 should contain the string var1 (not the content of var1, but the name of variable itself - if you used it without $).
|
|
1 members found this post helpful.
|
09-05-2017, 01:26 PM
|
#8
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,916
|
I agree with the example by pan64, but will add my defensive coding comments: - I call a script or command with the full pathname, because I've been bitten before. While I realize this argument can go either way, regarding a proper $PATH environment setup to be uniform, my self-experience is with custom embedded Linux, so things are literally where I've placed them. If they are not, there is a problem.
- Similarly I also ensure that if my script changes directories, that I save the $HOME from where I started. Or rather I save the `pwd` as a variable I typically name as HOME and then return there upon exit.
- I also try to do single entry, single exit code, for scripts as well as programs. Sometimes difficult to achieve, but when I break that rule, I explain it in comments.
EDIT: I also agree with Turbocapitalist in that you might consider functions over calling another script.
Last edited by rtmistler; 09-05-2017 at 01:27 PM.
|
|
|
All times are GMT -5. The time now is 08:15 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|