pass parameter back to calling program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pass parameter back to calling program
# 1  
Old 11-18-2003
Question pass parameter back to calling program

Hi all,

I am running AIX version 4. I have a shell script that is calling another script. I want the called script to obtain a value and pass it back to the calling script. So far, I know that to pass a parameter to a called script is as such:

sh proc2.sh $1 $2 etc.

What I don't know how to do though is pass a parameter back the other way. Can any one help??

Thanks
Jules
# 2  
Old 11-18-2003
sh proc2.sh $1 $2 etc

is really off to a bad start. It would be better to call it proc2 rather than proc2.sh. And invoking it via an explicit call to sh is a very bad idea. You should make the first line of proc2 something like:
#! /usr/bin/sh
(but use your path to sh) and then do a:
chmod +x proc2
When you do these things you can then run proc2 as an executable:
proc2 $1 $2

Now if the author of proc2 decides to rewrite it in c or ksh or perl etc, you don't need to change every script that called it. Famous unix program like "touch" and "grep" started out as sh scripts.

Also $1 and $2 refer to the first and second positional parameters in the caller. It would be rare to pass those to a second program.

Since you're using sh rather than ksh, you must be using expr for any arithmetic like this:
x=1
x=`expr $x + 1`
This is one way to return a parameter. The backticks take the stdout from the program. And the other way it the exit code, which is very limited.

Here is a sample:
#! /usr/bin/sh
echo hello
exit 2

And another script would do this:
x=`sample`
echo $x
sample
echo $?
# 3  
Old 11-18-2003
You can use the exit command to pass back an error number. e.g. exit 56

This value is passed back into the calling shell and can be examined in the built in variable $?.

If you want to do something a little more complex (like a string) then the called script should echo the return value e.g.

echo $SOME_VARIABLE.

To use this value in the calling script:

RETURN_VAR=`called_script param1 10 20`

Notice the back quote(`). You are telling ksh to replace the results of the script into the variable i.e. what ever is echoed to stdout.

You could also use syntax like:

RETURN_VAR=$(called_script param1 10 20)

If you want to pass back several parameters then echo them as one string, using a delimiter to separate the values.
# 4  
Old 11-18-2003
I called the script like you said in the funny `` quotes. In the called program did the echo...all worked like a charm.

Thanks MBB for that extra bit at the end, my very next question was in fact; "What about when you have more than 1 parameter"!Smilie

Appreciate the help guys, thanks.
# 5  
Old 11-19-2003
how about this one

echo $retval|awk -v x="YOUR OUTPUT" '{print substr(x,2,4)}'

so you return the output of ur script using

retval=`your script`

and use retval as a variable to awk and the use the substring function of awk to split the output

i know echo retval doesnt look elegant tho, but this is just a sample to hint you the use of awk and its substr function
# 6  
Old 11-19-2003
probably

if retval is 2 4 5 6
echo $retval|awk '{print substr($0,2,4)'

this would result into " 4 6"
# 7  
Old 11-20-2003
Thanks for the reply LP. I must admit, I know nothing about AWK. I have tried once or twice to use it, all I get are strange errors that I can't debug. My UNIX administrator don't even know how the command works. They gave me a book once that should have explained the awk procedure, but it was a piece of rubbish - couldn't make head or tails of it.

I would love to be able to use the awk procecedure cos I understand that its really powerfull. Where do I go though to get a really good set of definitions of the syntax? Looking at your code I can understand what you are getting at, but I don't know the mechanics of what you are doing, e.g. what is the -v option and what other type of options are there, why do you print the results etc etc etc
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get front and back parameter of each characters?

I have list of words file. I trying to get a front and back parameter of each characters of words. hello .... .... Here is what I have done: awk '{ word=$1; len=length(word); tlen=2*len-1; for (i=1; i<len; i++) a="#"; for (j=1; j<=len; j++) a=substr(word,j,1); ... (15 Replies)
Discussion started by: paranrat
15 Replies

2. Shell Programming and Scripting

Pass parameter

Hi, I have following for loop , please let me know how to get ${TXP_EXT_TABLE_${i}_SQL} parameter with 1DAY and 7DAY values. for i in 1DAY 7DAY do ${NZSQL_DIR}/nzsql -h ${HOST} -time -v ON_ERROR_STOP=1 -f ${SQL_DIR}/${TXP_EXT_TABLE_${i}_SQL} > ${TMP_LOG_FILE} 2>&1 done ... (4 Replies)
Discussion started by: sandy162
4 Replies

3. Shell Programming and Scripting

How to pass the environment name while calling java program from unix script?

Hi, I'm trying to test one unix shell script in dev environment. But I'm not sure how to pass the environment in my java program calling code. I'm trying to use -DconsumerEnv="DEV" but unfortunately I get 'null' while trying to print the value from java class. System.out.println("Environment: "+... (4 Replies)
Discussion started by: Pramit
4 Replies

4. UNIX for Dummies Questions & Answers

Pass value back to unix variable

i had this unix korn shell code that connects to oracle database and execute the oracle procedure. i need to add a variable that indicates the oracle procedure failed. basically the variable is to check if the oracle procedure failed it will assign 1 and when the variable is equal to 1 it will not... (4 Replies)
Discussion started by: wtolentino
4 Replies

5. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

6. UNIX for Dummies Questions & Answers

How to pass the parameter value to a... ?

Hello I have a simple code like this one: #!/bin/ksh VER=$1 cat /usr/text | while read line do echo $line done Let's say $1=1.0.0 and the contents of text is: abcd.cfg asdf I would like the output to be like this abcd1.0.0.cfg asdf1.0.0 I am thinking of passing the... (5 Replies)
Discussion started by: khestoi
5 Replies

7. UNIX for Dummies Questions & Answers

calling process and going back to the main loop

hi everyone , i want to read an option and depending on the option call the program .For ex #! /bin/ksh export JAVA_HOME=/home/oracle/jdk1.6.0_20 echo " Please enter mod-modeler, dev - sqldeveloper" read choice if ; then echo ' SQL DEVELOPER IS STARTING NOW ... ' cd... (0 Replies)
Discussion started by: kdev
0 Replies

8. Shell Programming and Scripting

How to pass a parameter

Hi all, How to pass a parameter from a oracle pl/sql procedure parameter to shell environment and use it? (1 Reply)
Discussion started by: megh
1 Replies

9. Shell Programming and Scripting

Help required to pass the parameter

i am calling a pl/sql procedure through a shell script, there is one IN and 2 OUT parameter required to pass to the procedure to execute.. My procedure is XX_CITIDIRECT_EXP_PKG.main_proc and In parameter is p_period which I wanto to pass 'MAY-06'. Can anyone figure out, whats is wrong here ... (4 Replies)
Discussion started by: u263066
4 Replies
Login or Register to Ask a Question