How to access variable from one file to another in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to access variable from one file to another in shell script?
# 1  
Old 10-04-2010
How to access variable from one file to another in shell script?

Hi,

I had written shell script to access config file as input parameter.
Contents of ConfFile.cfg are
Code:
 
USER=ora
PASSWORD=ora

Shell script is DBConnect.ksh
Code:
 
#!/bin/sh
usage="`basename $0` <configuration_file_name>"
DB_Connect()
{
 sqlplus -s ora/ora << EOF
 CREATE TABLE TBL1 (Name String, B_Date date)
 EOF
}
if [ $# -eq 1 ]; then
   if [ -f $1 ]; then
   . $1
   DB_Connect    
 else 
  echo "ERROR : Configuration file $1 is not available"
  exit 1   
 fi
 
else
 echo "usage : $usage"
fi

1] How can I use USER and PASSWORD variable values in my shell script?
2] How DB connection will happen (My shell script and DB are on same server)
3] Please chek the syntax of writting DDL statements in shell script?

Thanks in advance
# 2  
Old 10-04-2010
the script is named as .ksh, but you have #!/bin/sh, which means it will use whatever is your default shell. Use "#!/bin/env ksh"

Avoid using USER, it is already set. Try in the terminal:
Code:
echo $USER

Use user=blabla instead.

To use the variables: $user
# 3  
Old 10-04-2010
You ca source ConfFile.cfg in the second script like . ConfFile.cfg
# 4  
Old 10-04-2010
How to access variable from one file to another in shell script?

#!/bin/sh
if [ ! -r $ConfigFile -a ! -s $ConfigFile ] ; then
echo " Config file Setup Doesn't Exit...."
exit 11
fi

username=`grep -w "username" $ConfigFile | cut -d"=" -f2`
pwd=`grep -w "passwd" $ConfigFile | cut -d"=" -f2`

DB_Connect()
{
sqlplus -s $username/$pwd << EOF
CREATE TABLE TBL1 (Name String, B_Date date)
EOF
}

DB_Connect
# 5  
Old 10-05-2010
I am able to get the values,
Code:
echo $USER
echo $PASSWORD

as
Code:
ora 
ora

I am getting systax error for line,
Code:
sqlplus -s $USER/$PASSWORD << EOF

as,
Code:
0403-057 Syntax error at line 37 : `<' is not matched.

Could you please help me out.
# 6  
Old 10-05-2010
Make sure you have a line with EOF in column 1 to match the << EOF
# 7  
Old 10-05-2010
Yes, I had written that also, but still error is coming.
Code:
sqlplus –s $USERID/$PASSWORD << EOF
  #create_table
  EOF

Also let me know, how can I check the DB connection is done or not?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

2. Shell Programming and Scripting

Using variable from shell script in perl file

Hey, So I have a shell script that outputs some variables, call them $a and $b. I know in shell scripting if I wanted to use the variables in another shell script I'd do sh code.sh "$a" "$b" How can I do something similar with perl? (2 Replies)
Discussion started by: viored
2 Replies

3. Shell Programming and Scripting

Problem getting the content of a file in a shell script variable

Hi, I have a text file that has a long multi-line db2 CTE query. Now I want to store all the contents of this file (i.e. the entire query) in a shell script variable. I am trying to achieve it by this: query = `cat /Folder/SomeFile.txt` But when I echo the contents of this file by saying echo... (4 Replies)
Discussion started by: DushyantG
4 Replies

4. Shell Programming and Scripting

Getting category when given the variable from external file to shell script

Hi, I have a script that interacts with a config file in the format: file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt I would like to return the Category, when given the file name. (11 Replies)
Discussion started by: MoreCowbell
11 Replies

5. Shell Programming and Scripting

(solved) Shell scripting to access SQLPLUS using variable

I have shell script which will try to login to SQL Plus and retrieve some data, based on the outcome i will proceed further Below is the content of the file pebblz02% cat test1.ksh #! /bin/ksh dummyvar=`sqlplus -S csm_admin/csm_admin@SIDNAME <<EOF echo hi; exit; EOF` Error message on... (0 Replies)
Discussion started by: kiranlalka
0 Replies

6. Shell Programming and Scripting

how to access variables in a config file inside a shell script

I'm writing a shell script. I want to put the variables in a separate config files and use those inside my script. e.g. the config file (temp.conf)will have the values like mapping=123 file_name=xyz.txt I want to access these variables in temp.conf(i.e. mapping and file_name) from inside the... (7 Replies)
Discussion started by: badrimohanty
7 Replies

7. Shell Programming and Scripting

Shell script to read file into variable

the script i am trying to write will allow my server to give itself an ip address. So far i am up to the following but i'm stuck. tracert -m 1 > traceroute.txt 1 routername (ipaddr) 2.094 ms 1.789 ms 1.243 ms i want to get ipaddr as a variable and use it to write the ifcfg-eth... (7 Replies)
Discussion started by: aspect_p
7 Replies

8. UNIX for Dummies Questions & Answers

accessing shell script variable in file

Hi, I have a shell script in which there is a file conn_$temp where $temp has the pid of the shell script. in this shell script i have an embedded awk script that must read the file while ((getline < "conn_$temp") > 0) However due to the "$temp" in the file name, the awk script is... (6 Replies)
Discussion started by: HIMANI
6 Replies

9. Shell Programming and Scripting

Read variable from file in a C shell script

Hi, I have a 1-line file which looks like " First second third 4 five". I need to extract the number (here 4) in that line and put it in a variable. I will use the variable later to make few tests in my C shell script. Can somebody help me? (2 Replies)
Discussion started by: haouesse
2 Replies

10. Shell Programming and Scripting

shell variable access

Hi I want to do the following: 1. Create a number of Gloabla varibale "ROUTE_IP_xx" based on a counter. As xx sould be from 1-10. ie ROUTE_IP_1 ROUTE_IP_2 . . ROUTE_IP_10 2. I want to initalize all of these variable to 0.0.0.0 ie ROUTE_IP_1='0.0.0.0' 3. I... (2 Replies)
Discussion started by: sabina
2 Replies
Login or Register to Ask a Question