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 09-05-2017, 06:23 AM   #1
ohijames
LQ Newbie
 
Registered: Jun 2010
Posts: 17

Rep: Reputation: 0
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.
 
Old 09-05-2017, 06:54 AM   #2
wpeckham
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

Rep: Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844Reputation: 2844
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.
 
Old 09-05-2017, 07:41 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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.
Old 09-05-2017, 07:42 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,916
Blog Entries: 13

Rep: Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948
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:
Code:
set -xv
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.
Old 09-05-2017, 09:16 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,603
Blog Entries: 4

Rep: Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895Reputation: 3895
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.
Old 09-05-2017, 11:58 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,366

Rep: Reputation: 6154Reputation: 6154Reputation: 6154Reputation: 6154Reputation: 6154Reputation: 6154Reputation: 6154Reputation: 6154Reputation: 6154Reputation: 6154Reputation: 6154
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.
Old 09-05-2017, 01:17 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,302

Rep: Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698
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.
Old 09-05-2017, 01:26 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,916
Blog Entries: 13

Rep: Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948Reputation: 4948
I agree with the example by pan64, but will add my defensive coding comments:
  1. 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.
  2. 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.
  3. 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.
 
  


Reply

Tags
arguments, linux, shell script, unix


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
LXer: Shell Scripting Part 3: Decision Control Structures in Shell Scripts LXer Syndicated Linux News 0 05-05-2015 11:01 PM
calling a shell script present on another server using perl script. anandg111 Linux - Server 4 07-18-2012 01:23 AM
Help with UNIX Shell Scripts Pampano Linux - Newbie 7 04-22-2012 04:17 AM
Calling shell scripts from PHP as other users Thin Programming 1 12-02-2005 09:17 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM

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

All times are GMT -5. The time now is 08:15 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
Open Source Consulting | Domain Registration