Passing arguments to the subshell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing arguments to the subshell
# 8  
Old 03-22-2010
There are several problems with your code:

1) You use a here-document which isn't closed in the second script:

Code:
bteq > ps_generate_sql.log <<- EOF

This says: execute the "bteq"-command, redirect its output (stdout) to "ps_generate_sql.log" and use everything up to "- EOF" as input to it. As there is no second "- EOF" I'm not sure if this will work at all.

2) Another thing is the naming of the here-document: "- EOF" is problematic as whitespace has special meaning in the shell.

3) There is absolutely no reason to use global variables and make one script dependent on the environment another script sets. This is about as error-prone as it can be.

4) The variable you pass cannot be used, because there are different program enviroments at work: a variable expansion can only take place in the shell, but you try to get the SQL-interpreter to expand it - that won't work. You will have to create a temporary copy of the SQL-file with the variable you want to expand, do the replacement by "by hand" (that is: using some UNIX utility) and then throw away this modified copy. I have modified your script accordingly.

5) There are some minor things you should habitually do in your scripts: let them exit with some exit-code and explicitly so, scripts should not just terminate because the end of the input file is reached; always define your variables and do not take them for granted ("typeset"); always use quoting as a means of protection of whitespace and other special characters within your arguments.

Rewrite the scripts the following way:

Code:
#!/bin/ksh

typeset PLAN="$1"

print - "PLAN is: \"$PLAN\""
inner_shell.ksh "$PLAN"

exit $?

inner_shell.ksh:

Code:
#!/bin/ksh

typeset PLAN="$1"
typeset CmdFile="/home/i276764/ps_refresh/PS_generate_SQL.sql"   # i suppose?
typeset CmdTmp="/tmp/PS_generate_SQL.sql"
typeset -i ErrLvl=0

print - "$0 called with \"$PLAN\""
sed 's/\${PLAN}/'"${PLAN}"'/' $CmdFile > $CmdTmp

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

.run file="${CmdTmp}" 

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

ErrLvl=$?
rm -rf $CmdTmp

exit $ErrLvl

/home/i276764/ps_refresh/PS_generate_SQL.sql (i believe? if not, change the name and the mechanism of copying/modifying a "blueprint-file" shown above accordingly)

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}))

I hope this helps.

bakunin
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