Passing arguments to the subshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing arguments to the subshell
# 1  
Old 03-22-2010
Passing arguments to the subshell

I have a shell script which is invoked by passing an argument. The outer shell script calls another subshell and I want the argument passed down to flow down to the subshell.


E.g

Invoking a shell ======>> abc_refresh.ksh NM

Below is the content of abc_refresh.ksh

Code:
Value1=$1
Echo $Value
inner_shell.ksh     <<=========  executing another shell inside the shell.

I want the $Value to be passed and recognized by a sql script inside the inner_shell.ksh.

Thanks in advance

edit by bakunin: and thank you - in advance - for enclosing scripts (or script-parts) in code-tags yourself from now on.

Last edited by bakunin; 03-22-2010 at 04:54 PM..
# 2  
Old 03-22-2010
Code:
export Value1=$1
echo $Value1
inner_shell.ksh

# 3  
Old 03-22-2010
A shell script is - apart from control structures like "if ... then" or "do ... done" - like commands executed on the shells command line. Therefore there is nothing special to take into account when passing arguments to a subshell. Do it like you would do with any other program. For example:

PassArgs.ksh
Code:
#! /bin/ksh

print - "Argument passed to PassArgs.ksh: $1"

/path/to/PassArgsSub.ksh "$1"

exit 0

PassArgsSub.ksh
Code:
#! /bin/ksh

print - "Argument passed to PassArgsSub.ksh: $1"

exit 0

You could even pass several (or all) arguments to subprocesses by using "$2", "$3", etc. (or "$*"). You can try it by replacing "$1" with e.g. "$*" in the above example scripts.

I hope this helps.

bakunin
# 4  
Old 03-22-2010
I tried the export option.....The inner_shell.ksh has a sql script and I want to pass the argument value to the sql code. But it is not recognizing it. Any ideas?
# 5  
Old 03-22-2010
can you show both the scripts?
# 6  
Old 03-22-2010
Why don't you post the scripts (or the relevant portions of them) here? It is - if you allow me this analogy - difficult to suggest a move in a chess position you are not allowed to even look at - even for a good player.

Second, why don't you give my suggestion a try? Exporting a variable is like creating a global variable in a normal programming language: bad programming style and very probably leading to problems sooner or later.

bakunin
# 7  
Old 03-22-2010
Below is the content of the abc_refresh.ksh shell

Code:
#!/bin/ksh

PLAN=$1 

export PLAN

echo $PLAN

inner_shell.ksh

Below is the content of inner_shell.ksh

Code:
#!/bin/ksh

echo "Script On"


bteq > ps_generate_sql.log <<- EOF
.run FILE=/home/i27/tdata_stg_logon.txt
.set errorout STDOUT
.set width 2900

.run file=/home/i276764/ps_refresh/PS_generate_SQL.sql 

.IF errorlevel > 8 then .goto badreturn
.Quit 0

Here is the content of the sql file which uses the argument

Code:
                                   select 
			lst.pln
			,lst.databasename
			,lst.table_name
			,lst.view_typ
			from TABLE1 lst
			left join TABLE2 cnt
			on lst.pln=cnt.pln
			and lst.table_name=cnt.table_name
			and lst.view_typ=cnt.view_typ
			where cnt.table_name is null
			and view_typ = 'views_all'
			and pln in ($PLAN))     <<<======Argument used here

I just used the export option for now but can change it to the one given by bakunin

edit by bakunin: grrrr - this is the last time i edit code-tags into your posts - the next time i'll resort to giving infractions for ignoring me.

Last edited by bakunin; 03-22-2010 at 09:23 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing multiple arguments

Hi, I know with getopts you can pass arguments from the command line ./script -ab -c apple But it doesn't support 2 or more arguments for ONE option. Is there any other way to do this? Thanks (2 Replies)
Discussion started by: testa500
2 Replies

2. Shell Programming and Scripting

Passing arguments--Error

Hi, i have a file.txt with data Bangalore Chennai Hyd filename of the script is: new.sh result=`cat file.txt | grep $1` if then echo pass else echo fail fi i am executing the file in the cmd line as "sh new.sh Bangalore" o/p is pass if i give "sh new.sh delhi" o/p is... (6 Replies)
Discussion started by: harsha85
6 Replies

3. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

4. Shell Programming and Scripting

Passing arguments at runtime

Hi .. Can any one please tell how to pass argument to shell script at runtime? I want to implement funcnality just like bc, where we can provide input while script is running and can be used later in the same script. Thanks in advance... (1 Reply)
Discussion started by: kunjalhg
1 Replies

5. Shell Programming and Scripting

Passing arguments to csh

I have noticed this thing using csh when passing arguments Suppose I call a csh script using ../Scripts/plot-model.csh -vmod="npt02-z30.vmod" -R="0/80/0/30" -c="0/4.5" -aspr="1:10" Somehow the " get removed when doing $argv ending up with -vmod=npt02-z30.vmod... (0 Replies)
Discussion started by: kristinu
0 Replies

6. UNIX for Dummies Questions & Answers

Passing arguments

I need to pass arguments to a shell script.My batch is calling some java program. ################# x=$1 y=$2 java -classpath program ################### if first parameter and second parameter is null then java -classpath program if first parameter is not null and second parameter is... (3 Replies)
Discussion started by: mnjx
3 Replies

7. Shell Programming and Scripting

passing arguments

Hi I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like ./abc def /file <directory location> In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Discussion started by: zmfcat1
4 Replies

8. Shell Programming and Scripting

Passing Arguments-Help

Hi, I have a script which adds the user credentials to an ldap server. Im passing the variables as below.. /path/my_script $uname $pwd $environ ${deposit} If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Discussion started by: Tuxidow
5 Replies

9. Shell Programming and Scripting

passing of a varibale to subshell

Hi All, I need some info. Could you please tell me how to use the variable of a parent shell in the subshell. Also can we modify the variable in the subshell ? If yes, will the modified variable visible in the parent shell I am using two prg. a.sh #!/usr/bin/ksh temp_var="abhishek"... (3 Replies)
Discussion started by: AbhishekG
3 Replies

10. UNIX for Dummies Questions & Answers

passing arguments

I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts: #!/bin/ksh read fname #if (( $# > 0 )); then $fname | ls -l #fi this produces a long listing of all the files in my current... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question