Passing argument to a script while executing it within current shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing argument to a script while executing it within current shell
# 1  
Old 06-02-2011
Passing argument to a script while executing it within current shell

Hi Gurus,

I have written a script set_env.ksh to which I pass an argument and set the oracle login credentials based on the argument I pass.

The script has code as below.

Code:
ORACLE_SID=$1
DB_SCHEMA_LOGON=$DB_SCHEMA_USER/$DB_SCHEMA_PASSWORD@$ORACLE_SID; export DB_SCHEMA_LOGON;
echo $DB_SCHEMA_LOGON

It works well when I call it as below

Code:
$ set_env.ksh DEV

and also works when I call the script from within another script.

But when I run this script within the current shell as below,

Code:
. ./set_env.ksh DEV

it does not get the argument passed ($# returns 0).... Any idea why it behaves so ? How can I pass the arguments while executing the script from within current shell itself.

Any help appreciated.

Thanks,
Sabari Nath S

Last edited by pludi; 06-02-2011 at 06:18 PM..
# 2  
Old 06-02-2011
Which shell are you using?

As a workaround you could define and use a function for that:
Code:
set_env() {
  ORACLE_SID=$1
  DB_SCHEMA_LOGON=$DB_SCHEMA_USER/$DB_SCHEMA_PASSWORD@$ORACLE_SID
  export DB_SCHEMA_LOGON;
  echo "$DB_SCHEMA_LOGON"
  }

Code:
set_env DEV

# 3  
Old 06-03-2011
My home shell is sh... But as radoulov told, I am using the setting inside a script (which has #!/usr/bin/ksh in the beginning) and executing the script in sh with DEV as the argument...
# 4  
Old 06-06-2011
Yes, so you could do one of the following:

1. Change your shell (ksh, bash or zsh should be fine).
2. Use a function.
3. Invoke the script like this (*):

Code:
set -- DEV
. ./set_env.ksh

* The command will reset the positional parameters.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue while executing C executable with argument in shell script

Hi All, I am new to this forum and also shell script :) My task is I have to read CSV file get the data from the file and use the data to call c executable with data as argument.And the output from c executable should be stored to new CSV file Please find below my code testfunction() {... (14 Replies)
Discussion started by: ravjot28
14 Replies

2. Shell Programming and Scripting

Passing variable as an argument to another script

Hi, I am trying to pass a variable as an argument to another script. While substitution of variable, I am facing a problem. varaiable "a" value should be -b "FPT MAIN". When we pass "a" to another script, we are expecing it to get substitue as ./test.sh -b "FPT MAIN". But, it is getting... (9 Replies)
Discussion started by: Manasa Pradeep
9 Replies

3. Shell Programming and Scripting

passing argument in script?

hi, I want to implement some function to perform following task if ; then $TEXT = "Data_0" else $TEXT = $1 fi if ; then $Lines = 45 else $Lines = $2 fi Kindly suggest, thanks (11 Replies)
Discussion started by: nrjrasaxena
11 Replies

4. Shell Programming and Scripting

Passing argument from Java to Shell script

Hi All, I want to pass array of argument from Java to a shell script.I can use process builder api and its exec() method to call the script,but the question is how to receive the parameter in the script. Thanks in advance ---------- Post updated at 10:00 PM ---------- Previous update was... (1 Reply)
Discussion started by: Abhijeet_Atti
1 Replies

5. Shell Programming and Scripting

passing either of argument, not both in shell

Hi, I have a requirement to work on script, it should take either of arguments. wrote it as below. #!/bin/bash usage() { echo "$0: missing argument OR invalid option ! Usage : $0 -m|-r|-d } while getopts mrdvh opt; do case "$opt" in m) monitor_flag=monitor;;... (1 Reply)
Discussion started by: ramanaraoeee
1 Replies

6. UNIX for Dummies Questions & Answers

Passing command output as an argument to a shell script

Hi, I have a very small requirement where i need to pass command output as an argument while invoking the shell script.. I need to call like this sh testscript.sh ' ls -t Appl*and*abc* | head -n 1' This will list one file name as ana argument.. I will be using "$1" in the shell... (2 Replies)
Discussion started by: pssandeep
2 Replies

7. UNIX for Dummies Questions & Answers

Passing command line argument between shell's

Hi, I am facing a problem to pass command line arguments that looks like <script name> aa bb "cc" dd "ee" I want to pass all 5 elements include the " (brackets). when I print the @ARGV the " disappear. I hope I explain myself Regards, Ziv (4 Replies)
Discussion started by: zivsegal
4 Replies

8. Shell Programming and Scripting

passing argument to shell script that reads user inputs

Hi, Lets say I have a script "ss" which does this read abc echo $abc read pqr echo $pqr Now if I want to pass and argument to only "abc" how do I do it. If I do echo "somevalue" | ss, it does not prompt for pqr and its value comes out as blank. Any help is appreciated Thanks P (6 Replies)
Discussion started by: patjones
6 Replies

9. Shell Programming and Scripting

Problem when passing argument to a shell script

Hi all, I'm new to Shell scripting. In my shell script for Bourne shell, the script accepts a date parameter which is optional. If the value is supplied, the supplied value should be assigned to a variable. If not, the current date will be assigned to the variable. My script is like this. #!... (9 Replies)
Discussion started by: sumesh.abraham
9 Replies

10. Shell Programming and Scripting

Passing argument from one script to other

Dear All, I have one script which accepts database name and user_id from the user, i have another script that will unload the data from all the tables based on the user_id accepted by the user. How can i pass the user_id from the 1st script to the other. My OS is sun solaris. Thanks in... (3 Replies)
Discussion started by: lloydnwo
3 Replies
Login or Register to Ask a Question