The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing values from sql to shell script sachin.gangadha Shell Programming and Scripting 3 04-22-2008 11:11 PM
Shell Script: want to insert values in database when update script runs ring Shell Programming and Scripting 1 10-25-2007 03:06 AM
Values of value($$X) in unix shell script ganesh_111 Shell Programming and Scripting 5 09-22-2006 06:25 AM
Passing Parameters and getting values back from a c program to Shell script Rajeshsu High Level Programming 5 08-22-2005 03:12 AM
Returning Values (shell Script) jennifer01 Shell Programming and Scripting 3 11-29-2001 06:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-05-2006
dhananjaysk dhananjaysk is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 35
Passing Values from a shell script

Hi all

I want to know how to pass value from one shell script to another
suppose i have script named A.This 'A.sh' calls 'B.sh' .Now some calculations
are done by B.sh ..Say in variable X.
Now i want to pass the value of this X variable to 'A.sh'...

A.sh ----> B.sh ( X variable )

A.sh <---- B.sh ( send value of X variable to A.sh )

How can I do it...
Please help asap
  #2 (permalink)  
Old 04-05-2006
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
I can think of two ways to do this:
Method 1 -- Use command substitution:-

A.sh:
var=10
var=`B.sh $var`
echo $var

B.sh:
echo `expr $1 + 2`


Ouput=12

Method 2 -- Use '.' to run the child script:-

A.sh:
var=10
. B.sh
echo $var

B.sh
let var=$var+2

Output=12


Hope this helps,
Abhishek Ghose
  #3 (permalink)  
Old 04-05-2006
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
Forgot to mention---the "Output" mentioned above is a result of running "A.sh"
  #4 (permalink)  
Old 04-05-2006
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Also ,you can use export:

Code:
$ cat A.sh
export VAR=10
B.sh

$ cat B.sh 
let VAR=$VAR+2
  #5 (permalink)  
Old 04-06-2006
dhananjaysk dhananjaysk is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 35
Whats the meaning

Quote:
Originally Posted by Abhishek Ghose
Forgot to mention---the "Output" mentioned above is a result of running "A.sh"
Hi Abhishek,
Its working Thanks a lot..

I want to know what does it Imply when I invoke a shell script using
"." notation

i.e . B.sh
( does it mean to run the script in the current shell ? )

But if i use
X=`B.sh` ..My script B.sh hangs and I have to use ctrl + Z
What is the reason for it ..
Also I am not passing any variable to B.sh ... But I want to get the variable value from B.sh in A.sh
( I am new to Unix )
Thanks.
Dhananjay
  #6 (permalink)  
Old 04-06-2006
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
"." runs the script in current shell, as you said. The reason for doing this is we want B.sh to recognise the variable "var"(which can only be recognised in the shell that it was defined in)

If you are not planning to pass any variable, then rather use metohd 2. But a caveat: Method 2 implicitly assumes that your script understands certain variable names (like "var" in my examples). Basically , this means that you might not be able to re-use B.sh. Also, better check whether the variable in question is null first. In such a case, make B.sh throw out an error message and abort execution.

In which scenario did it hang? When the scripts were written according to method 1 or method 2?
A statement like X=`B.sh` might hang in scenario 1, as it expects a commandline input. But not sure.
  #7 (permalink)  
Old 04-06-2006
dhananjaysk dhananjaysk is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 35
Please see the example

Quote:
Originally Posted by Abhishek Ghose
"." runs the script in current shell, as you said. The reason for doing this is we want B.sh to recognise the variable "var"(which can only be recognised in the shell that it was defined in)

If you are not planning to pass any variable, then rather use metohd 2. But a caveat: Method 2 implicitly assumes that your script understands certain variable names (like "var" in my examples). Basically , this means that you might not be able to re-use B.sh. Also, better check whether the variable in question is null first. In such a case, make B.sh throw out an error message and abort execution.

In which scenario did it hang? When the scripts were written according to method 1 or method 2?
A statement like X=`B.sh` might hang in scenario 1, as it expects a commandline input. But not sure.
My script code as follows
--------------------------------------------
. database_checking.sh

echo "Database Name" $db_name
echo "set feed off verify off pagesize 0
select username from user_users;"|sqlplus -s /@$db_name|read user_name
---------------------------------------------------------------

My database_checking.sh code follows which return db_name variable to above code....which is furthur used for retrieving username ...
-----------Start------------------------------------------------
db_var=1
while test $db_var -eq 1
do
echo "\n\t\t\tEnter Database Name"
echo "\t\t\t[ Example:marketbld.uk.d.ch ] or Press [ 'Q'/'q'] to exit :\c"
read db_name
func_meta_ch "$db_name"

if [ "!$db_name!" != "!!" -a $var_meta_ch -eq 0 ]
then
expr $db_name + 2 >/dev/null 2>&1
db_val=$?

if [ $db_val -ne 0 ]
then
echo "set feed off verify off pagesize 0
select count(1) from mktactivity;"|sqlplus -s /@$db_name|read sqldb_name

expr $sqldb_name + 2 >/dev/null 2>&1
db_cnt=$?

if [ "!$sqldb_name!" = "!ERROR:!" ]
then
echo "\t\t\tError: Database name entered does not exist."
else
if [ $db_cnt -ne 0 ]
then
echo "\t\t\tError: Database name entered does not exist."
else
db_var=2
fi
fi
else
echo "\t\t\tError: Database name entered does not exist."
fi
else
echo "\t\t\tError: Invalid Database name."
fi
done
echo $db_name

------------End ------------------
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0