Scope of variables between scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scope of variables between scripts
# 1  
Old 07-27-2010
Scope of variables between scripts

Friends,

I am using ksh under SunoS. This is what I have

In file1.sh

Code:
NOW=$(date +"%b-%d-%y")
LOGFILE="./log-$NOW.log"

I will be using this file through file1.sh as log file.

I have another script file2.sh which is being called inside my file1.sh. I would like to use the same log file to capture the events that are happening inside file2.sh as well. Is it possible? How do I do it?
# 2  
Old 07-27-2010
Hi.

export the variables when you declare them in file1.sh:

Code:
NOW=$(date +"%b-%d-%y")
LOGFILE="./log-$NOW.log"
export NOW LOGFILE

and they will be available to you in file2.sh.
# 3  
Old 07-27-2010
scott thanks for your reply. This works! I wonder why this statement

Code:
echo "Some Warning" | tee -a $LOGFILE

displays the string Some Warning both on the standard output device (monitor) and also writes on the log file.

Is it because I am manually executing the script? Will it be fixed if I run that script in a crontab? or am I doing something wrong?
# 4  
Old 07-27-2010
Quote:
Originally Posted by dahlia84
scott thanks for your reply. This works! I wonder why this statement

Code:
echo "Some Warning" | tee -a $LOGFILE

displays the string Some Warning both on the standard output device (monitor) and also writes on the log file.

Is it because I am manually executing the script? Will it be fixed if I run that script in a crontab? or am I doing something wrong?
Hi.

That's what it's supposed to do.

Code:
tee Command

Purpose
       Displays the output of a program and copies it into a file.

If you only want the output in a file use

Code:
echo "Some Warning" >> $LOGFILE

instead.

If left like this, in a cronjob, you will get the output in your mailbox.
# 5  
Old 07-27-2010
Thanks scott for your time. I got it now.

Code:
echo "Some Warning" >> $LOGFILE

This command does not seem to create the $LOGFILE if $LOGFILE does not exist. That's why I am using | tee.
# 6  
Old 07-27-2010
Quote:
Originally Posted by dahlia84
Thanks scott for your time. I got it now.

Code:
echo "Some Warning" >> $LOGFILE

This command does not seem to create the $LOGFILE if $LOGFILE does not exist. That's why I am using | tee.
Well, provided that LOGFILE is set at the time, it should create the file if it doesn't exist:

Code:
>>word             Use  file    word  as  standard output. If the file exists,
                   then output is appended to it (by first  seeking  to  the
                   end-of-file). Otherwise, the file is created.

otherwise it will just go to the screen.:
Code:
$ unset LOGFILE
$ echo "Some Warning" >> $LOGFILE
blah

This User Gave Thanks to Scott For This Post:
# 7  
Old 07-27-2010
Scott - I lack basics! I search google and ask forum members for help when ever I am stuck. When they give me the solution I feel like I lack the basics... I want to master myself in ksh. I should be able to write date manipulation programs, sending e-mail, etc such real time scripts in ksh. Can you suggest me few books which has some exercises as well, please?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to get out of 2 variables in scripts

Hi All, i have written below script, and out put i am looking for both variable PRIMARY_CONF and $STANDBY_CONF but i am getting below error d1.sh: line 64: ------------------------------ line 64 is: if -a ; then ---------------------------------- please let me know where is the... (9 Replies)
Discussion started by: amar1208
9 Replies

2. Shell Programming and Scripting

while read loop; scope of variables (shell)

If I set a variable within a while-read loop, sometimes it's local to the loop, sometimes it's global, depending on how the loop is set up. I'm testing this on a Debian Lenny system using both bash and dash with the same results. For example: # Pipe command into while-read loop count= ls -1... (2 Replies)
Discussion started by: mjd_tech
2 Replies

3. Shell Programming and Scripting

Variables scope.

Hi , I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file. #!/bin/sh FLAG=0; cat filename | while read data do FLAG=1; done echo $FLAG Should display 1 instead displays 0 (13 Replies)
Discussion started by: dinjo_jo
13 Replies

4. Shell Programming and Scripting

Doubt about variables scope

I call my script with two parameters myscript.sh aaa bbb What is the way to access $1 and $2 values inside a function? I call the function like this myfuntion $1 $1 but inside of the function, $1 and $2 are empty. Any suggestions? thank you in advanced. (1 Reply)
Discussion started by: aristegui
1 Replies

5. Shell Programming and Scripting

Doubt??? [scope of variables]

Heres an example..... <~/abc>$ cat textfile line 1 line 2 line 3 line 4 line 5 <~/abc>$ cat try.sh #/bin/ksh for runs in 1 2 3 do A=$runs echo "Inside A : $A" done echo "Outside A : $A" <- works fine (1 Reply)
Discussion started by: qzv2jm
1 Replies

6. Shell Programming and Scripting

Access Awk Variables Outside Scope

My awk script searches for specified patterns in a text file and stores these values into mem variables. Once this is done I want to Insert these values into a table. How can I avail of the variable values outside the scope of awk script.... One method that I have tried is to write the... (7 Replies)
Discussion started by: Amruta Pitkar
7 Replies

7. UNIX for Dummies Questions & Answers

Passing variables between scripts...

Hey all, I'm wondering how you pass variable's that are defined in one script to another script that's been called by that first script..... Best regards, Jaz (1 Reply)
Discussion started by: Jazmania
1 Replies

8. Shell Programming and Scripting

How to access variables across scripts

Hi All, I have declared a variable in script1 and assign a value for it. In script2 i'll call script1 and then I want the value of variables set in script1. I have tried with export, but in vain. How can I achive this? Below is the two scripts. --script1 #!/usr/bin/ksh echo $1... (1 Reply)
Discussion started by: javaDev
1 Replies

9. UNIX for Dummies Questions & Answers

Variables in scripts

Just a quick question. If I have a script that calls another script while running, is it possible for the second script to reference a variable in the first script and if so, how. Is it scriptname.sh:$VARIABLE for a global variable and can you do scriptname.sh:function $VARIABLE or am I off my... (1 Reply)
Discussion started by: kirkm76
1 Replies

10. UNIX for Advanced & Expert Users

Access Awk Variables Outside Scope

Sorry in the wrong forum. Moving this to right forum. (2 Replies)
Discussion started by: Amruta Pitkar
2 Replies
Login or Register to Ask a Question