accessing shell script variable in file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers accessing shell script variable in file
# 1  
Old 08-04-2007
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 unable to open the file.
How can i fix this problem?
basically i need to append the pid to the file name so that for each session the file names will be different.
Thanks for your help in advance.


Himani
# 2  
Old 08-04-2007
Can you post your script please ?
kamitsin
# 3  
Old 08-04-2007
Hi,
This is the script :

temp=`echo $$`
echo $temp

tempvar1=`some values here`
tempvar2=`somme other values here`

diff conndifffile1_$temp conndifffile2_$temp | egrep '^< |> ' | uniq -u > conndifffile3_$temp

awk -v val1=${tempvar1} -v val2=${tempvar2} -F , 'BEGIN {
while ((getline < "conndifffile3_$temp") > 0)
{
if( substr($1,1,1) == "<" )
{
sub(/</,val1,$1)
sub(/ /," , ",$1)
}
else
{
sub(/>/,val2,$1)
sub(/ /," , ",$1)
}
print $1,",",$2,",",$3,",",$4,",",$5
}
}'


Files conndifffile1_$temp conndifffile2_$temp have some values stored in them.

Thanks,
Himani
# 4  
Old 08-04-2007
Try this,

Code:
temp=`echo $$`
echo $temp

tempvar1=2
tempvar2=3

diff conndifffile1_$temp conndifffile2_$temp | egrep '^< |> ' | uniq -u > conndifffile3_$temp

INPUT_FILE=conndifffile3_$temp

gawk -v val1=${tempvar1} -v val2=${tempvar2} -v val3=${INPUT_FILE} 'BEGIN {
while ((getline < val3 ) > 0)
{
if( substr($1,1,1) == "<" )
{
sub(/</,val1,$1)
sub(/ /," , ",$1)
}
else
{
sub(/>/,val2,$1)
sub(/ /," , ",$1)
}
print $1,",",$2,",",$3,",",$4,",",$5
}
}'

kamitsin
# 5  
Old 08-04-2007
Hi,
Its working !!! thanks.
But i have a doubt .. i was naming the files with the pid so
that if different users login then the files for them would be seperate and not interfere with each other.
If the variable INPUT_FILE is assigned the file3 name then wouldnt that
sometimes make files of different users to be lost?

Thanks,
Himani
# 6  
Old 08-04-2007
Quote:
If the variable INPUT_FILE is assigned the file3 name then wouldnt that sometimes make files of different users to be lost?
- No it won't happen because the PID will always be different
kamitsin
# 7  
Old 08-07-2007
Thanks for ur help Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Accessing a variable from another file

HI all, How can i access a variable that is defined in another file as: $$Name= "abhinav; in my script. The catch is that it has 2 $s behind it... Thnaks ---------- Post updated at 10:36 AM ---------- Previous update was at 10:29 AM ---------- the file from which i have to... (0 Replies)
Discussion started by: abhinav192
0 Replies

2. Shell Programming and Scripting

accessing a variable or array of one script in another

hi all, i've a requirement like this. i want to access the contents of an array declared in one script,which is a bash script, to a second script which is a perl script. actually i want a sort of global variable which can be accessed in other script like environmen variables and also i can... (3 Replies)
Discussion started by: tprayush
3 Replies

3. Shell Programming and Scripting

Accessing variable from awk program in shell

Hi, I want to access a variable outside the awk program. My program is as below:- I can not access the exact value of k (See the last line of the program). #!/usr/bin/sh j=10 k=1 #k is declared outside awk awk ' BEGIN { i=1; j1="'"$j"'" printf("\n ## Value of j1 is %d ##", j1); ... (2 Replies)
Discussion started by: shouvik.mitra
2 Replies

4. Shell Programming and Scripting

Accessing a variable from another script

Hi, Can anyone assist me on how to access a variable in a shell script from another script. for ex, Script-1 ------- #! /bin/sh c=10 Where as, i would like to access the velue of variable c in another script 'Script-2'. Thankyou to all in advance !! :b: :b: (2 Replies)
Discussion started by: little_wonder
2 Replies

5. Programming

Accessing environmet variable in xml file

Hi I have certain environmenet variables defined in .profile: export DBNAME="mydb" export DBHOST="devbox.us.abc.com" export DBPORT="1111" export PASSWORD="mypwd" export MMUSER="myid" I also have an xml file defined like: <CONFIG> <DEFAULTS operator="oraread"> <PROPERTY... (2 Replies)
Discussion started by: neeto
2 Replies

6. Shell Programming and Scripting

Grep to return a code from accessing variable file name

All I want to do is find out if the string 'NO' is in a file by checking for a return code of 0 if there is a match. The grep works fine on the command line directly naming the file but I am obviously not using the correct syntax within the shell script as I consistently get the error message ... (5 Replies)
Discussion started by: SusanDAC
5 Replies

7. Shell Programming and Scripting

Accessing aliases within a shell script

I am not able to access the aliases in my environment within a Python script. If I pass the alias to os.system(), I get a message saying "sh: x: not found". I've tried sourcing my .bashrc file in the script, but this does not work. I would perfer not to source my or any rc file because this... (9 Replies)
Discussion started by: cooldude
9 Replies

8. Shell Programming and Scripting

Accessing the parameters of a shell script

Hi, I have one situation. I am developing a shell script to which parameters will be passed from a Web based User Interface using some Business Process(BP).There are some 6 parameters for which user will enter the values in UI. These values will be passed to script by BP in the form -... (2 Replies)
Discussion started by: The Observer
2 Replies

9. Shell Programming and Scripting

Shell script for accessing a file in the network drive and invoking oracle sql loader

Hi, Please let me know if anybody is having a solution handy for the below tasks... It would be helpful if somebody can resolve my query. I am new to unix and oracle environment and looking for some online reference for completing a task. Task: Check if the network drive exists Check... (0 Replies)
Discussion started by: sayydevara
0 Replies

10. Solaris

Accessing the home directory in shell script

How can I access home directory in a shell script. I am writing a script which will delete the log files in each of the user's directory Krishan (1 Reply)
Discussion started by: krishan
1 Replies
Login or Register to Ask a Question