LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-09-2008, 07:48 AM   #1
ppr:kut
Member
 
Registered: Aug 2006
Location: Netherlands
Distribution: Slackware
Posts: 356

Rep: Reputation: 91
bash programming question - hand over variables to another script


Hey guys!

I searched the internet over and over, but couldn't find a solution, basically because of not knowing exactly how I should entitle it :/. I try to describe. I have the following script (just a test case):

Code:
#!/bin/bash
TEST=${TEST:-nope}
echo $TEST
I want to call this script from another one, changing the TEST variable via commandline parameter. So something like that:

wrapper.sh TEST=yes

wrapper.sh:
Code:
#!/bin/bash
SCRIPT="/home/user/test.sh"
$1 $SCRIPT
I already know that this code, as it's written here, does not work. IS there any solution to this, or does it simply not work?
 
Old 02-09-2008, 08:18 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Your sample program doesn't match the question you posted.

It performs the command you pass as an argument to the script.

Here is a similar example:
Code:
cat indprog.sh
#!/bin/bash

$1 visudo.tex

jschiwal@hpamd64:~> ./indprog.sh wc
 199  912 7304 visudo.tex
jschiwal@hpamd64:~> ./indprog.sh ls
visudo.tex
If you want a script to create a variable, source the script; and in the script, export the variable.

Code:
cat >testprog.sh
#!/bin/bash
VAR1='ABC123'
export VAR1
jschiwal@hpamd64:~> chmod +x testprog.sh
jschiwal@hpamd64:~> ./testprog.sh
jschiwal@hpamd64:~> echo $VAR1

jschiwal@hpamd64:~> . ./testprog.sh
jschiwal@hpamd64:~> echo $VAR1
ABC123
Or you could have the program output the value:
VAR1=$(./script)

Is this what you had in mind?
Code:
cat test2.sh
echo -n 'VAR2=TEST2'
jschiwal@hpamd64:~> V=$(./test2.sh )
jschiwal@hpamd64:~> eval $V
jschiwal@hpamd64:~> echo $VAR2
TEST2

Last edited by jschiwal; 02-09-2008 at 08:26 AM.
 
Old 02-09-2008, 08:25 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,011

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
If you change test.sh to
Code:
#!/bin/bash
TEST=${1:-nope}
echo ${TEST}
and wrapper.sh to
Code:
#!/bin/bash
SCRIPT=test.sh
${SCRIPT} ${1}
then it'll do what I think you want it to.
 
Old 02-09-2008, 08:46 AM   #4
ppr:kut
Member
 
Registered: Aug 2006
Location: Netherlands
Distribution: Slackware
Posts: 356

Original Poster
Rep: Reputation: 91
Thanks for the fast responses .

Quote:
Your sample program doesn't match the question you posted.
Well, yes. As I said above, part of my problem is I don't know how to entitle it.

Quote:
It performs the command you pass as an argument to the script.
Exactly! And that is my problem. I call "wrapper.sh TEST=yes" and the output I'm waiting for is "yes".
Instead I get "TEST=yes: command not found"

I would like to tell bash somehow, that $1 isn't a command, but a variable I want to pass on to test.sh

tronayne:
Almost. But fact is, I can't change test.sh (for my purpose). And I do really need to pass the variable before the actual script.
 
Old 02-09-2008, 09:39 AM   #5
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,011

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Almost. But fact is, I can't change test.sh (for my purpose). And I do really need to pass the variable before the actual script.
OK, you've got two choices: export TEST in wrapper.sh or export TEST in your .profile file.

If you do the first, all child processes of wrapper.sh (and whatever any additional child processes are called from the children) will be able to use that variable; the variable will cease to exist on exit. If you do the second then any process in the stream (wrapper.sh, test.sh, etc., etc.) will be able to use or change the content of that variable and it will still available after everything exits; i.e., you'll be able to echo ${TEST} after everything runs and see the final value.
 
Old 02-09-2008, 10:00 AM   #6
ppr:kut
Member
 
Registered: Aug 2006
Location: Netherlands
Distribution: Slackware
Posts: 356

Original Poster
Rep: Reputation: 91
Code:
#!/bin/bash
SCRIPT="/home/user/test.sh"
export $(echo $1)
$SCRIPT
Thanks very much. The above script does work .
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 09:57 AM
splitting variables In Bash Programming neorion Programming 2 08-04-2004 05:28 PM
New to bin/bash variables programming help jobesd Programming 3 03-24-2003 05:02 AM


All times are GMT -5. The time now is 06:03 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration